/** * Returns a ByteString that consists of a given range. * * @param start Start of range * @param end Position after end of range * @return Substring */ public ByteString substring(int start, int end) { byte[] bytes = Arrays.copyOfRange(this.bytes, start, end); return new ByteString(bytes, false); }
/** * Returns a ByteString that consists of a given range. * * @param start Start of range * @param end Position after end of range * @return Substring */ public ByteString substring(int start, int end) { byte[] bytes = Arrays.copyOfRange(this.bytes, start, end); return new ByteString(bytes, false); }
/** * Returns a ByteString that consists of a given range. * * @param start Start of range * @param end Position after end of range * @return Substring */ public ByteString substring(int start, int end) { byte[] bytes = Arrays.copyOfRange(this.bytes, start, end); return new ByteString(bytes, false); }
/** * Creates a byte string from a Base64 string. * * @param string Base64 string * @return Byte string */ public static ByteString ofBase64(String string) { final byte[] bytes = parseBase64(string); return new ByteString(bytes, false); }
/** * Creates a byte string from a Base64 string. * * @param string Base64 string * @return Byte string */ public static ByteString ofBase64(String string) { final byte[] bytes = parseBase64(string); return new ByteString(bytes, false); }
/** * Creates a byte string from a Base64 string. * * @param string Base64 string * @return Byte string */ public static ByteString ofBase64(String string) { final byte[] bytes = parseBase64(string); return new ByteString(bytes, false); }
/** * Creates a byte string from a hexadecimal or binary string. * * <p>For example, <code>of("DEAD", 16)</code> * returns the same as {@code ByteString(new byte[] {0xDE, 0xAD})}. * * @param string Array of bytes * @param base Base (2 or 16) * @return String */ public static ByteString of(String string, int base) { final byte[] bytes = parse(string, base); return new ByteString(bytes, false); }
/** * Creates a byte string from a hexadecimal or binary string. * * <p>For example, <tt>of("DEAD", 16)</tt> * returns the same as {@code ByteString(new byte[] {0xDE, 0xAD})}. * * @param string Array of bytes * @param base Base (2 or 16) * @return String */ public static ByteString of(String string, int base) { final byte[] bytes = parse(string, base); return new ByteString(bytes, false); }
/** * Creates a byte string from a hexadecimal or binary string. * * <p>For example, <code>of("DEAD", 16)</code> * returns the same as {@code ByteString(new byte[] {0xDE, 0xAD})}. * * @param string Array of bytes * @param base Base (2 or 16) * @return String */ public static ByteString of(String string, int base) { final byte[] bytes = parse(string, base); return new ByteString(bytes, false); }
/** Returns a byte-string padded with zero bytes to make it at least a given * length, */ private static ByteString padRight(ByteString s, int length) { if (s.length() >= length) { return s; } return new ByteString(Arrays.copyOf(s.getBytes(), length)); } }
/** Returns a byte-string padded with zero bytes to make it at least a given * length, */ private static ByteString padRight(ByteString s, int length) { if (s.length() >= length) { return s; } return new ByteString(Arrays.copyOf(s.getBytes(), length)); } }
/** Helper for CAST(... AS BINARY(maxLength)). */ public static ByteString truncateOrPad(ByteString s, int maxLength) { if (s == null) { return null; } else { final int length = s.length(); if (length > maxLength) { return s.substring(0, maxLength); } else if (length < maxLength) { return s.concat(new ByteString(new byte[maxLength - length])); } else { return s; } } }
/** Helper for CAST(... AS BINARY(maxLength)). */ public static ByteString truncateOrPad(ByteString s, int maxLength) { if (s == null) { return null; } else { final int length = s.length(); if (length > maxLength) { return s.substring(0, maxLength); } else if (length < maxLength) { return s.concat(new ByteString(new byte[maxLength - length])); } else { return s; } } }
/** Helper for CAST(... AS BINARY(maxLength)). */ public static ByteString truncateOrPad(ByteString s, int maxLength) { if (s == null) { return null; } else { final int length = s.length(); if (length > maxLength) { return s.substring(0, maxLength); } else if (length < maxLength) { return s.concat(new ByteString(new byte[maxLength - length])); } else { return s; } } }
@Test public void testByteString() { serializeAndEqualityCheck( TypedValue.ofLocal(Rep.BYTE_STRING, new ByteString("qwertyasdf".getBytes(UTF_8)))); }
@Test public void testByteString() { serializeAndEqualityCheck( TypedValue.ofLocal(Rep.BYTE_STRING, new ByteString("qwertyasdf".getBytes(StandardCharsets.UTF_8)))); }
@Test public void testByteString() { serializeAndEqualityCheck( TypedValue.ofLocal(Rep.BYTE_STRING, new ByteString("qwertyasdf".getBytes(UTF_8)))); }
private void thereAndBack(byte[] bytes) { final ByteString byteString = new ByteString(bytes); final byte[] bytes2 = byteString.getBytes(); assertThat(bytes, equalTo(bytes2)); final String base64String = byteString.toBase64String(); final ByteString byteString1 = ByteString.ofBase64(base64String); assertThat(byteString, equalTo(byteString1)); }
private void thereAndBack(byte[] bytes) { final ByteString byteString = new ByteString(bytes); final byte[] bytes2 = byteString.getBytes(); assertThat(bytes, equalTo(bytes2)); final String base64String = byteString.toBase64String(); final ByteString byteString1 = ByteString.ofBase64(base64String); assertThat(byteString, equalTo(byteString1)); }
@Test public void testBase64() { byte[] bytes = "qwertyasdf".getBytes(UTF_8); // Plain bytes get put into protobuf for simplicitly Common.TypedValue proto = Common.TypedValue.newBuilder().setBytesValue( com.google.protobuf.ByteString.copyFrom(bytes)) .setType(Common.Rep.BYTE_STRING).build(); // But we should get back a b64-string to make sure TypedValue doesn't get confused. Object deserializedObj = TypedValue.getSerialFromProto(proto); assertThat(deserializedObj, is(instanceOf(String.class))); assertEquals(new ByteString(bytes).toBase64String(), (String) deserializedObj); // But we should get a non-b64 byte array as the JDBC representation deserializedObj = TypedValue.protoToJdbc(proto, DateTimeUtils.calendar()); assertThat(deserializedObj, is(instanceOf(byte[].class))); assertArrayEquals(bytes, (byte[]) deserializedObj); }