|
| 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 | +} |
0 commit comments