@Override public byte[] getBytes() { Object obj = getObject(); try { final ByteString o = (ByteString) obj; return o == null ? null : o.getBytes(); } catch (Exception ex) { return obj == null ? null : (byte[]) obj; } }
public String load(@Nonnull Pair<ByteString, Charset> key) { final Charset charset = key.right; final CharsetDecoder decoder = charset.newDecoder(); final byte[] bytes = key.left.getBytes(); final ByteBuffer buffer = ByteBuffer.wrap(bytes); try { return decoder.decode(buffer).toString(); } catch (CharacterCodingException ex) { throw RESOURCE.charsetEncoding( //CHECKSTYLE: IGNORE 1 new String(bytes, Charset.defaultCharset()), charset.name()).ex(); } } });
/** 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)); } }
@Override public byte[] getBytes() throws SQLException { Object obj = getObject(); if (null == obj) { return null; } if (obj instanceof ByteString) { return ((ByteString) obj).getBytes(); } else if (obj instanceof String) { // Need to unwind the base64 for JSON return ByteString.parseBase64((String) obj); } else if (obj instanceof byte[]) { // Protobuf would have a byte array return (byte[]) obj; } else { throw new RuntimeException("Cannot handle " + obj.getClass() + " as bytes"); } }
@Override public byte[] getBytes() throws SQLException { Object obj = getObject(); if (null == obj) { return null; } if (obj instanceof ByteString) { return ((ByteString) obj).getBytes(); } else if (obj instanceof String) { // Need to unwind the base64 for JSON return ByteString.parseBase64((String) obj); } else if (obj instanceof byte[]) { // Protobuf would have a byte array return (byte[]) obj; } else { throw new RuntimeException("Cannot handle " + obj.getClass() + " as bytes"); } }
/** * Validate if value can be decoded by given charset. * * @param value nls string in byte array * @param charset charset * @throws RuntimeException If the given value cannot be represented in the * given charset */ public static void validateCharset(ByteString value, Charset charset) { if (charset == StandardCharsets.UTF_8) { final byte[] bytes = value.getBytes(); if (!Utf8.isWellFormed(bytes)) { //CHECKSTYLE: IGNORE 1 final String string = new String(bytes, charset); throw RESOURCE.charsetEncoding(string, charset.name()).ex(); } } }
private static Object serialToJdbc(ColumnMetaData.Rep type, Object value, Calendar calendar) { switch (type) { case BYTE_STRING: return ByteString.ofBase64((String) value).getBytes(); case JAVA_UTIL_DATE: return new java.util.Date(adjust((Number) value, calendar)); case JAVA_SQL_DATE: return new java.sql.Date( adjust(((Number) value).longValue() * DateTimeUtils.MILLIS_PER_DAY, calendar)); case JAVA_SQL_TIME: return new java.sql.Time(adjust((Number) value, calendar)); case JAVA_SQL_TIMESTAMP: return new java.sql.Timestamp(adjust((Number) value, calendar)); default: return serialToLocal(type, value); } }
static public Object convertToAvroObject(Object relObj, Schema schema) { if (relObj == null) { return null; } switch(schema.getType()) { case RECORD: return convertToGenericRecord((SamzaSqlRelRecord) relObj, getNonNullUnionSchema(schema)); case ARRAY: List<Object> avroList = ((List<Object>) relObj).stream() .map(o -> convertToAvroObject(o, getNonNullUnionSchema(schema).getElementType())) .collect(Collectors.toList()); return avroList; case MAP: return ((Map<String, ?>) relObj).entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> convertToAvroObject(e.getValue(), getNonNullUnionSchema(schema).getValueType()))); case UNION: return convertToAvroObject(relObj, getNonNullUnionSchema(schema)); case ENUM: return new GenericData.EnumSymbol(schema, (String) relObj); case FIXED: return new GenericData.Fixed(schema, ((ByteString) relObj).getBytes()); case BYTES: return ByteBuffer.wrap(((ByteString) relObj).getBytes()); default: return relObj; } }
public Object convertToAvroObject(Object relObj, Schema schema) { if (relObj == null) { return null; } switch(schema.getType()) { case RECORD: return convertToGenericRecord((SamzaSqlRelRecord) relObj, getNonNullUnionSchema(schema)); case ARRAY: List<Object> avroList = ((List<Object>) relObj).stream() .map(o -> convertToAvroObject(o, getNonNullUnionSchema(schema).getElementType())) .collect(Collectors.toList()); return avroList; case MAP: return ((Map<String, ?>) relObj).entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> convertToAvroObject(e.getValue(), getNonNullUnionSchema(schema).getValueType()))); case UNION: return convertToAvroObject(relObj, getNonNullUnionSchema(schema)); case ENUM: return new GenericData.EnumSymbol(schema, (String) relObj); case FIXED: return new GenericData.Fixed(schema, ((ByteString) relObj).getBytes()); case BYTES: return ByteBuffer.wrap(((ByteString) relObj).getBytes()); default: return relObj; } }
case BINARY: if (clazz == byte[].class) { return clazz.cast(((ByteString) value).getBytes());
switch (type) { case BYTE_STRING: return ByteString.ofBase64((String) value).getBytes(); case JAVA_UTIL_DATE: return new java.util.Date(adjust((Number) value, calendar));
switch (type) { case BYTE_STRING: return ByteString.ofBase64((String) value).getBytes(); case JAVA_UTIL_DATE: return new java.util.Date(adjust((Number) value, calendar));
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)); }
thereAndBack(emptyByteString.getBytes()); thereAndBack(new byte[]{10, 0, 29, -80});
thereAndBack(emptyByteString.getBytes()); thereAndBack(new byte[]{10, 0, 29, -80});