Skip to content

Commit b8474df

Browse files
committed
JAVA-2336: Expose byte utility methods in the public API
1 parent 321696b commit b8474df

3 files changed

Lines changed: 95 additions & 5 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.2.0 (in progress)
66

7+
- [improvement] JAVA-2336: Expose byte utility methods in the public API
78
- [improvement] JAVA-2338: Revisit toString() for data container types
89
- [bug] JAVA-2367: Fix column names in EntityHelper.updateByPrimaryKey
910
- [bug] JAVA-2358: Fix list of reserved CQL keywords
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.api.core.data;
17+
18+
import com.datastax.oss.protocol.internal.util.Bytes;
19+
import java.nio.ByteBuffer;
20+
21+
/**
22+
* A set of static utility methods to work with byte buffers (associated with CQL type {@code
23+
* blob}).
24+
*/
25+
public class ByteUtils {
26+
27+
// Implementation note: this is just a gateway to the internal `Bytes` class in native-protocol.
28+
// The difference is that this one is part of the public API.
29+
30+
/**
31+
* Converts a blob to its CQL hex string representation.
32+
*
33+
* <p>A CQL blob string representation consists of the hexadecimal representation of the blob
34+
* bytes prefixed by "0x".
35+
*
36+
* @param bytes the blob/bytes to convert to a string.
37+
* @return the CQL string representation of {@code bytes}. If {@code bytes} is {@code null}, this
38+
* method returns {@code null}.
39+
*/
40+
public static String toHexString(ByteBuffer bytes) {
41+
return Bytes.toHexString(bytes);
42+
}
43+
44+
/**
45+
* Converts a blob to its CQL hex string representation.
46+
*
47+
* <p>A CQL blob string representation consists of the hexadecimal representation of the blob
48+
* bytes prefixed by "0x".
49+
*
50+
* @param byteArray the blob/bytes array to convert to a string.
51+
* @return the CQL string representation of {@code bytes}. If {@code bytes} is {@code null}, this
52+
* method returns {@code null}.
53+
*/
54+
public static String toHexString(byte[] byteArray) {
55+
return Bytes.toHexString(byteArray);
56+
}
57+
58+
/**
59+
* Parses a hex string representing a CQL blob.
60+
*
61+
* <p>The input should be a valid representation of a CQL blob, i.e. it must start by "0x"
62+
* followed by the hexadecimal representation of the blob bytes.
63+
*
64+
* @param str the CQL blob string representation to parse.
65+
* @return the bytes corresponding to {@code str}. If {@code str} is {@code null}, this method
66+
* returns {@code null}.
67+
* @throws IllegalArgumentException if {@code str} is not a valid CQL blob string.
68+
*/
69+
public static ByteBuffer fromHexString(String str) {
70+
return Bytes.fromHexString(str);
71+
}
72+
73+
/**
74+
* Extracts the content of the provided {@code ByteBuffer} as a byte array.
75+
*
76+
* <p>This method works with any type of {@code ByteBuffer} (direct and non-direct ones), but when
77+
* the buffer is backed by an array, it will try to avoid copy when possible. As a consequence,
78+
* changes to the returned byte array may or may not reflect into the initial buffer.
79+
*
80+
* @param bytes the buffer whose contents to extract.
81+
* @return a byte array with the contents of {@code bytes}. That array may be the array backing
82+
* {@code bytes} if this can avoid a copy.
83+
*/
84+
public static byte[] getArray(ByteBuffer bytes) {
85+
return Bytes.getArray(bytes);
86+
}
87+
88+
private ByteUtils() {}
89+
}

examples/src/main/java/com/datastax/oss/driver/examples/datatypes/Blobs.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.datastax.oss.driver.api.core.cql.BoundStatement;
2020
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
2121
import com.datastax.oss.driver.api.core.cql.Row;
22-
import com.datastax.oss.protocol.internal.util.Bytes;
22+
import com.datastax.oss.driver.api.core.data.ByteUtils;
2323
import java.io.File;
2424
import java.io.FileInputStream;
2525
import java.io.FileOutputStream;
@@ -143,7 +143,7 @@ private static void retrieveSimpleColumn(CqlSession session) {
143143
// - even then, the backing array might be larger than the buffer's contents
144144
//
145145
// The driver provides a utility method that handles those details for you:
146-
byte[] array = Bytes.getArray(buffer);
146+
byte[] array = ByteUtils.getArray(buffer);
147147
assert array.length == 16;
148148
for (byte b : array) {
149149
assert b == (byte) 0xFF;
@@ -175,7 +175,7 @@ private static void insertConcurrent(CqlSession session) {
175175
session.prepare("INSERT INTO examples.blobs (k, b) VALUES (1, :b)");
176176

177177
// This is another convenient utility provided by the driver. It's useful for tests.
178-
ByteBuffer buffer = Bytes.fromHexString("0xffffff");
178+
ByteBuffer buffer = ByteUtils.fromHexString("0xffffff");
179179

180180
// When you pass a byte buffer to a bound statement, it creates a shallow copy internally with
181181
// the buffer.duplicate() method.
@@ -188,7 +188,7 @@ private static void insertConcurrent(CqlSession session) {
188188
session.execute(boundStatement);
189189
Row row = session.execute("SELECT b FROM examples.blobs WHERE k = 1").one();
190190
assert row != null;
191-
assert Objects.equals(Bytes.toHexString(row.getByteBuffer("b")), "0xffffff");
191+
assert Objects.equals(ByteUtils.toHexString(row.getByteBuffer("b")), "0xffffff");
192192

193193
buffer.flip();
194194

@@ -199,7 +199,7 @@ private static void insertConcurrent(CqlSession session) {
199199
session.execute(boundStatement);
200200
row = session.execute("SELECT b FROM examples.blobs WHERE k = 1").one();
201201
assert row != null;
202-
assert Objects.equals(Bytes.toHexString(row.getByteBuffer("b")), "0xaaffff");
202+
assert Objects.equals(ByteUtils.toHexString(row.getByteBuffer("b")), "0xaaffff");
203203

204204
// This will also happen if you use the async API, e.g. create the bound statement, call
205205
// executeAsync() on it and reuse the buffer immediately.

0 commit comments

Comments
 (0)