com.datastax.driver.core.utils.Bytes类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(125)

本文整理了Java中com.datastax.driver.core.utils.Bytes类的一些代码示例,展示了Bytes类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytes类的具体详情如下:
包路径:com.datastax.driver.core.utils.Bytes
类名称:Bytes

Bytes介绍

[英]Simple utility methods to make working with bytes (blob) easier.
[中]简单实用的方法使处理字节(blob)更容易。

代码示例

代码示例来源:origin: kaaproject/kaa

/**
 * Convert key hash to string.
 *
 * @param endpointKeyHash is key hash to convert
 * @return string
 */
public static String convertKeyHashToString(byte[] endpointKeyHash) {
 String id = null;
 if (endpointKeyHash != null) {
  id = Bytes.toHexString(endpointKeyHash);
 }
 return id;
}

代码示例来源:origin: kaaproject/kaa

/**
  * This method convert string representation of endpoint key hash to ByteBuffer object
  * if id eq null, than return null.
  *
  * @return the ByteBuffer object
  */
 public static ByteBuffer convertStringToKeyHash(String id) {
  ByteBuffer endpointKeyHash = null;
  if (id != null && id.length() != 0) {
   endpointKeyHash = Bytes.fromHexString(id);
  }
  return endpointKeyHash;
 }
}

代码示例来源:origin: kaaproject/kaa

/**
 * This method convert ByteBuffer object to byte array.
 *
 * @return the byte array or null
 */
public static byte[] getBytes(ByteBuffer byteBuffer) {
 byte[] array = null;
 if (byteBuffer != null) {
  array = Bytes.getArray(byteBuffer);
 }
 return array;
}

代码示例来源:origin: kaaproject/kaa

@Test
public void testBytesToStringConversation() {
 byte[] array = new byte[]{-16, 7, 51, -98, -75, -19, -82, 119, -51, 122, -125, -14, 22, 44, -28, -56, 26, 111, 115, 2};
 String hash = Bytes.toHexString(array);
 LOG.info("---> hash is {}", hash);
 byte[] converted = Bytes.fromHexString(hash).array();
 Assert.assertArrayEquals(array, converted);
 Assert.assertEquals(hash, Bytes.toHexString(converted));
}

代码示例来源:origin: prestodb/presto

@Test
public void testPartitionKeyPredicate()
{
  String sql = "SELECT *" +
      " FROM " + TABLE_ALL_TYPES_PARTITION_KEY +
      " WHERE key = 'key 7'" +
      " AND typeuuid = '00000000-0000-0000-0000-000000000007'" +
      " AND typeinteger = 7" +
      " AND typelong = 1007" +
      " AND typebytes = from_hex('" + toRawHexString(ByteBuffer.wrap(toByteArray(7))) + "')" +
      " AND typetimestamp = TIMESTAMP '1969-12-31 23:04:05'" +
      " AND typeansi = 'ansi 7'" +
      " AND typeboolean = false" +
      " AND typedecimal = 128.0" +
      " AND typedouble = 16384.0" +
      " AND typefloat = REAL '2097152.0'" +
      " AND typeinet = '127.0.0.1'" +
      " AND typevarchar = 'varchar 7'" +
      " AND typevarint = '10000000'" +
      " AND typetimeuuid = 'd2177dd0-eaa2-11de-a572-001b779c76e7'" +
      " AND typelist = '[\"list-value-17\",\"list-value-27\"]'" +
      " AND typemap = '{7:8,9:10}'" +
      " AND typeset = '[false,true]'" +
      "";
  MaterializedResult result = execute(sql);
  assertEquals(result.getRowCount(), 1);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Parse an hex string representing a CQL blob.
 *
 * <p>The input should be a valid representation of a CQL blob, i.e. it must start by "0x"
 * followed by the hexadecimal representation of the blob bytes.
 *
 * @param str the CQL blob string representation to parse.
 * @return the bytes corresponding to {@code str}. If {@code str} is {@code null}, this method
 *     returns {@code null}.
 * @throws IllegalArgumentException if {@code str} is not a valid CQL blob string.
 */
public static ByteBuffer fromHexString(String str) {
 if ((str.length() & 1) == 1)
  throw new IllegalArgumentException(
    "A CQL blob string must have an even length (since one byte is always 2 hexadecimal character)");
 if (str.charAt(0) != '0' || str.charAt(1) != 'x')
  throw new IllegalArgumentException("A CQL blob string must start with \"0x\"");
 return ByteBuffer.wrap(fromRawHexString(str, 2));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

private static String toRawHexString(ByteBuffer bytes, char[] array, int offset) {
 int size = bytes.remaining();
 int bytesOffset = bytes.position();
 assert array.length >= offset + 2 * size;
 for (int i = 0; i < size; i++) {
  int bint = bytes.get(i + bytesOffset);
  array[offset + i * 2] = byteToChar[(bint & 0xf0) >> 4];
  array[offset + 1 + i * 2] = byteToChar[bint & 0x0f];
 }
 return wrapCharArray(array);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Converts a blob to its CQL hex string representation.
 *
 * <p>A CQL blob string representation consist of the hexadecimal representation of the blob bytes
 * prefixed by "0x".
 *
 * @param bytes the blob/bytes to convert to a string.
 * @return the CQL string representation of {@code bytes}. If {@code bytes} is {@code null}, this
 *     method returns {@code null}.
 */
public static String toHexString(ByteBuffer bytes) {
 if (bytes == null) return null;
 if (bytes.remaining() == 0) return "0x";
 char[] array = new char[2 * (bytes.remaining() + 1)];
 array[0] = '0';
 array[1] = 'x';
 return toRawHexString(bytes, array, 2);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Create a PagingState object from a string previously generated with {@link #toString()}.
 *
 * @param string the string value.
 * @return the PagingState object created.
 * @throws PagingStateException if the string does not have the correct format.
 */
public static PagingState fromString(String string) {
 try {
  byte[] complete = Bytes.fromRawHexString(string, 0);
  return new PagingState(complete);
 } catch (Exception e) {
  throw new PagingStateException(
    "Cannot deserialize paging state, invalid format. "
      + "The serialized form was corrupted, or not initially generated from a PagingState object.",
    e);
 }
}

代码示例来源:origin: com.facebook.presto.cassandra/cassandra-driver

private static String toRawHexString(ByteBuffer bytes, char[] array, int offset) {
  int size = bytes.remaining();
  int bytesOffset = bytes.position();
  assert array.length >= offset + 2 * size;
  for (int i = 0; i < size; i++) {
    int bint = bytes.get(i + bytesOffset);
    array[offset + i * 2] = byteToChar[(bint & 0xf0) >> 4];
    array[offset + 1 + i * 2] = byteToChar[bint & 0x0f];
  }
  return wrapCharArray(array);
}

代码示例来源:origin: kaaproject/kaa

/**
 * This method convert ByteBuffer object to string representation,
 * if endpointKeyHash eq null, than return null.
 *
 * @return the String representation of endpoint key hash
 */
public static String convertKeyHashToString(ByteBuffer endpointKeyHash) {
 String id = null;
 if (endpointKeyHash != null) {
  id = Bytes.toHexString(endpointKeyHash);
 }
 return id;
}

代码示例来源:origin: kaaproject/kaa

/**
 * Id consist of endpoint key hash and last modify time, we get these data and assign values on
 * fields <code>endpointKeyHash</code> and <code>lastModifyTime</code>.
 *
 * @param id is id to parsing
 */
public void parseStringId(String id) {
 String[] ids = parseId(id);
 if (ids != null && ids.length == 2) {
  endpointKeyHash = Bytes.fromHexString(ids[0]);
  lastModifyTime = new Date(Long.valueOf(ids[1]));
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Returns the "raw" paging state of the query.
 *
 * <p>Contrary to {@link #getPagingState()}, there will be no validation when this is later
 * reinjected into a statement.
 *
 * @return the paging state or null if there is no next page.
 * @see Statement#setPagingStateUnsafe(byte[])
 */
public byte[] getPagingStateUnsafe() {
 if (this.pagingState == null) return null;
 return Bytes.getArray(this.pagingState);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Converts a blob to its CQL hex string representation.
 *
 * <p>A CQL blob string representation consist of the hexadecimal representation of the blob
 * bytes.
 *
 * @param bytes the blob/bytes to convert to a string.
 * @return the CQL string representation of {@code bytes}. If {@code bytes} is {@code null}, this
 *     method returns {@code null}.
 */
public static String toRawHexString(ByteBuffer bytes) {
 if (bytes == null) return null;
 if (bytes.remaining() == 0) {
  return "";
 }
 char[] array = new char[2 * (bytes.remaining())];
 return toRawHexString(bytes, array, 0);
}

代码示例来源:origin: com.facebook.presto.cassandra/cassandra-driver

/**
 * Parse an hex string representing a CQL blob.
 * <p/>
 * The input should be a valid representation of a CQL blob, i.e. it
 * must start by "0x" followed by the hexadecimal representation of the
 * blob bytes.
 *
 * @param str the CQL blob string representation to parse.
 * @return the bytes corresponding to {@code str}. If {@code str}
 * is {@code null}, this method returns {@code null}.
 * @throws IllegalArgumentException if {@code str} is not a valid CQL
 *                                  blob string.
 */
public static ByteBuffer fromHexString(String str) {
  if ((str.length() & 1) == 1)
    throw new IllegalArgumentException("A CQL blob string must have an even length (since one byte is always 2 hexadecimal character)");
  if (str.charAt(0) != '0' || str.charAt(1) != 'x')
    throw new IllegalArgumentException("A CQL blob string must start with \"0x\"");
  return ByteBuffer.wrap(fromRawHexString(str, 2));
}

代码示例来源:origin: io.prestosql.cassandra/cassandra-driver

private static String toRawHexString(ByteBuffer bytes, char[] array, int offset) {
  int size = bytes.remaining();
  int bytesOffset = bytes.position();
  assert array.length >= offset + 2 * size;
  for (int i = 0; i < size; i++) {
    int bint = bytes.get(i + bytesOffset);
    array[offset + i * 2] = byteToChar[(bint & 0xf0) >> 4];
    array[offset + 1 + i * 2] = byteToChar[bint & 0x0f];
  }
  return wrapCharArray(array);
}

代码示例来源:origin: kaaproject/kaa

/**
 * Generate new id using <code>endpointKeyHash</code> and <code>lastModifyTime</code>.
 *
 * @return id
 */
public String generateId() {
 String id = null;
 if (endpointKeyHash != null) {
  StringBuilder builder = new StringBuilder(Bytes.toHexString(endpointKeyHash));
  builder.append(CassandraModelConstants.KEY_DELIMITER).append(lastModifyTime.getTime());
  id = builder.toString();
 }
 return id;
}

代码示例来源:origin: prestodb/presto

@Test(groups = CASSANDRA)
public void testAllDataTypes()
{
  // NOTE: DECIMAL is treated like DOUBLE
  QueryResult query = query(format(
      "SELECT a, b, bl, bo, d, do, f, fr, i, integer, l, m, s, t, ti, tu, u, v, vari FROM %s.%s.%s",
      CONNECTOR_NAME, KEY_SPACE, CASSANDRA_ALL_TYPES.getName()));
  assertThat(query)
      .hasColumns(VARCHAR, BIGINT, VARBINARY, BOOLEAN, DOUBLE, DOUBLE, REAL, VARCHAR, VARCHAR,
          INTEGER, VARCHAR, VARCHAR, VARCHAR, VARCHAR, TIMESTAMP, VARCHAR, VARCHAR,
          VARCHAR, VARCHAR)
      .containsOnly(
          row("\0", Long.MIN_VALUE, Bytes.fromHexString("0x00").array(), false, 0f, Double.MIN_VALUE,
              Float.MIN_VALUE, "[0]", "0.0.0.0", Integer.MIN_VALUE, "[0]", "{\"\\u0000\":-2147483648,\"a\":0}",
              "[0]", "\0", Timestamp.valueOf(LocalDateTime.of(1970, 1, 1, 0, 0)),
              "d2177dd0-eaa2-11de-a572-001b779c76e3", "01234567-0123-0123-0123-0123456789ab",
              "\0", String.valueOf(Long.MIN_VALUE)),
          row("the quick brown fox jumped over the lazy dog", 9223372036854775807L, "01234".getBytes(),
              true, new Double("99999999999999999999999999999999999999"), Double.MAX_VALUE,
              Float.MAX_VALUE, "[4,5,6,7]", "255.255.255.255", Integer.MAX_VALUE, "[4,5,6]",
              "{\"a\":1,\"b\":2}", "[4,5,6]", "this is a text value", Timestamp.valueOf(LocalDateTime.of(9999, 12, 31, 23, 59, 59)),
              "d2177dd0-eaa2-11de-a572-001b779c76e3", "01234567-0123-0123-0123-0123456789ab",
              "abc", String.valueOf(Long.MAX_VALUE)),
          row("def", null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/** @return A new ByteBuffer from the input Buffer with any trailing 0-bytes stripped off. */
private static ByteBuffer stripTrailingZeroBytes(ByteBuffer b) {
 byte result[] = Bytes.getArray(b);
 int zeroIndex = result.length;
 for (int i = result.length - 1; i > 0; i--) {
  if (result[i] == 0) {
   zeroIndex = i;
  } else {
   break;
  }
 }
 return ByteBuffer.wrap(result, 0, zeroIndex);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Override
public String toString() {
 return Bytes.toRawHexString(generateCompleteOutput());
}

相关文章