import javax.xml.bind.DatatypeConverter; public static String toHexString(byte[] array) { return DatatypeConverter.printHexBinary(array); } public static byte[] toByteArray(String s) { return DatatypeConverter.parseHexBinary(s); }
/** * Turns a hex encoded string into a byte array. It is specifically meant * to "reverse" the toHex(byte[]) method. * * @param hex a hex encoded String to transform into a byte array. * @return a byte array representing the hex String[ */ public static byte[] decodeHex(String hex) { return DatatypeConverter.parseHexBinary(hex); }
public byte[] unmarshal(String s) { if(s==null) return null; return DatatypeConverter.parseHexBinary(s); }
/** * Creates byte array from a hex represented string. */ public static byte[] fromHex(String s) { return DatatypeConverter.parseHexBinary(s); // fast! }
@Override public byte[] decodeKey(String src) { return DatatypeConverter.parseHexBinary(src); }
public static byte[] fromHex(String hex) { return DatatypeConverter.parseHexBinary(hex); }
private static final byte[] CDRIVES = javax.xml.bind.DatatypeConverter.parseHexBinary("e04fd020ea3a6910a2d808002b30309d")
import javax.xml.bind.DatatypeConverter; public static String toHexString(byte[] array) { return DatatypeConverter.printHexBinary(array); } public static byte[] toByteArray(String s) { return DatatypeConverter.parseHexBinary(s); }
/** * Get payload field from GenericRecord and convert to byte array */ public byte[] getPayload(GenericRecord inputRecord, String payloadFieldName) { ByteBuffer bb = (ByteBuffer) inputRecord.get(payloadFieldName); byte[] payloadBytes; if (bb.hasArray()) { payloadBytes = bb.array(); } else { payloadBytes = new byte[bb.remaining()]; bb.get(payloadBytes); } String hexString = new String(payloadBytes, StandardCharsets.UTF_8); return DatatypeConverter.parseHexBinary(hexString); }
private byte[] getMessageKey(final FlowFile flowFile, final ProcessContext context) { final String uninterpretedKey; if (context.getProperty(KEY).isSet()) { uninterpretedKey = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue(); } else { uninterpretedKey = flowFile.getAttribute(KafkaProcessorUtils.KAFKA_KEY); } if (uninterpretedKey == null) { return null; } final String keyEncoding = context.getProperty(KEY_ATTRIBUTE_ENCODING).getValue(); if (UTF8_ENCODING.getValue().equals(keyEncoding)) { return uninterpretedKey.getBytes(StandardCharsets.UTF_8); } return DatatypeConverter.parseHexBinary(uninterpretedKey); } }
private byte[] getMessageKey(final FlowFile flowFile, final ProcessContext context) { final String uninterpretedKey; if (context.getProperty(KEY).isSet()) { uninterpretedKey = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue(); } else { uninterpretedKey = flowFile.getAttribute(KafkaProcessorUtils.KAFKA_KEY); } if (uninterpretedKey == null) { return null; } final String keyEncoding = context.getProperty(KEY_ATTRIBUTE_ENCODING).getValue(); if (UTF8_ENCODING.getValue().equals(keyEncoding)) { return uninterpretedKey.getBytes(StandardCharsets.UTF_8); } return DatatypeConverter.parseHexBinary(uninterpretedKey); } }
private byte[] getMessageKey(final FlowFile flowFile, final ProcessContext context) { final String uninterpretedKey; if (context.getProperty(KEY).isSet()) { uninterpretedKey = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue(); } else { uninterpretedKey = flowFile.getAttribute(KafkaProcessorUtils.KAFKA_KEY); } if (uninterpretedKey == null) { return null; } final String keyEncoding = context.getProperty(KEY_ATTRIBUTE_ENCODING).getValue(); if (UTF8_ENCODING.getValue().equals(keyEncoding)) { return uninterpretedKey.getBytes(StandardCharsets.UTF_8); } return DatatypeConverter.parseHexBinary(uninterpretedKey); } }
private byte[] getMessageKey(final FlowFile flowFile, final ProcessContext context) { final String uninterpretedKey; if (context.getProperty(KEY).isSet()) { uninterpretedKey = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue(); } else { uninterpretedKey = flowFile.getAttribute(KafkaProcessorUtils.KAFKA_KEY); } if (uninterpretedKey == null) { return null; } final String keyEncoding = context.getProperty(KEY_ATTRIBUTE_ENCODING).getValue(); if (UTF8_ENCODING.getValue().equals(keyEncoding)) { return uninterpretedKey.getBytes(StandardCharsets.UTF_8); } return DatatypeConverter.parseHexBinary(uninterpretedKey); } }
protected List<SchemaAndValueField> schemaAndValuesForPostgisTypes() { Schema geomSchema = Geometry.builder().optional().build(); Schema geogSchema = Geography.builder().optional().build(); return Arrays.asList( // geometries are encoded here as HexEWKB new SchemaAndValueField("p", geomSchema, // 'SRID=3187;POINT(174.9479 -36.7208)'::postgis.geometry Geometry.createValue(geomSchema, DatatypeConverter.parseHexBinary("0101000020730C00001C7C613255DE6540787AA52C435C42C0"), 3187)), new SchemaAndValueField("ml", geogSchema, // 'MULTILINESTRING((169.1321 -44.7032, 167.8974 -44.6414))'::postgis.geography Geography.createValue(geogSchema, DatatypeConverter.parseHexBinary("0105000020E610000001000000010200000002000000A779C7293A2465400B462575025A46C0C66D3480B7FC6440C3D32B65195246C0"), 4326)) ); }
@Test public void shouldConvertMySqlBytesToGeomCollection() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("730C00000107000000020000000101000000000000000000F03F000000000000F03F01020000000200000000000000000000000000000000000000000000000000F03F000000000000F03F"); //This represents 'SRID=3187;GEOMETRYCOLLECTION(POINT(1 1), LINESTRING(0 0, 1 1))' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertFalse(geom.isPoint()); assertEquals(geom.getSrid(), Integer.valueOf(3187)); assertEquals("0107000000020000000101000000000000000000F03F000000000000F03F01020000000200000000000000000000000000000000000000000000000000F03F000000000000F03F", DatatypeConverter.printHexBinary(geom.getWkb())); }
@Test public void shouldConvertMySqlBytesToLine() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("E6100000010200000002000000E3A59BC420F01B4015A143A69D38324000000000000000000000000000000000"); //This represents 'SRID=4326;LINESTRING(6.9845 18.22115554, 0 0)' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertFalse(geom.isPoint()); assertEquals(Integer.valueOf(4326), geom.getSrid()); assertEquals("010200000002000000E3A59BC420F01B4015A143A69D38324000000000000000000000000000000000", DatatypeConverter.printHexBinary(geom.getWkb())); }
@Test public void shouldConvertMySqlBytesToPolygon() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("E61000000103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000"); //This represents 'SRID=4326;POLYGON((0 0, 1 1, 1 0, 0 0))' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertFalse(geom.isPoint()); assertEquals(geom.getSrid(), Integer.valueOf(4326)); assertEquals("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000", DatatypeConverter.printHexBinary(geom.getWkb())); }
protected List<SchemaAndValueField> schemaAndValuesForPostgisArrayTypes() { Schema geomSchema = Geometry.builder() .optional() .build(); List<Struct> values = Arrays.asList( // 'GEOMETRYCOLLECTION EMPTY'::postgis.geometry Geometry.createValue(geomSchema, DatatypeConverter.parseHexBinary("010700000000000000"), null), // 'POLYGON((166.51 -46.64, 178.52 -46.64, 178.52 -34.45, 166.51 -34.45, 166.51 -46.64))'::postgis.geometry Geometry.createValue(geomSchema, DatatypeConverter.parseHexBinary("01030000000100000005000000B81E85EB51D0644052B81E85EB5147C0713D0AD7A350664052B81E85EB5147C0713D0AD7A35066409A999999993941C0B81E85EB51D064409A999999993941C0B81E85EB51D0644052B81E85EB5147C0"), null) ); return Arrays.asList( // geometries are encoded here as HexEWKB new SchemaAndValueField("ga", SchemaBuilder.array(geomSchema).optional().build(), values ), new SchemaAndValueField("gann", SchemaBuilder.array(geomSchema).build(), values ) ); }
@Test public void shouldConvertMySqlBytesToMultiGeometry() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("000000000104000000020000000101000000000000000000F03F000000000000F03F010100000000000000000000400000000000000040"); //This represents 'MULTIPOINT(1 1, 2 2)'' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertFalse(geom.isPoint()); assertEquals(geom.getSrid(), null); assertEquals("0104000000020000000101000000000000000000F03F000000000000F03F010100000000000000000000400000000000000040", DatatypeConverter.printHexBinary(geom.getWkb())); } }
@Test public void shouldConvertMySqlBytesToPoint() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("000000000101000000e3a59bc420f01b4015a143a69d383240"); //This represents 'POINT(6.9845 18.22115554)' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertTrue(geom.isPoint()); assertEquals(geom.getSrid(), null); double[] coords = Point.parseWKBPoint(geom.getWkb()); assertEquals(coords[0], 6.9845, 0.0001); assertEquals(coords[1], 18.22115554, 0.0001); }