@Override protected void writeString(Encoder encoder, String val) throws IOException { encoder.writeString(val); }
@Override public Encoder writeString(String s) throws IOException { return encoder.writeString(s); }
@Override protected void writeString(Encoder encoder, String val) throws IOException { encoder.writeString(val); }
@Override public Encoder writeString(String s) throws IOException { return encoder.writeString(s); }
@Override protected void encodeEnum(Encoder encoder, Schema enumSchema, Object value) throws IOException { String enumValue = value instanceof Enum ? ((Enum) value).name() : value.toString(); encoder.writeString(enumValue); }
@Override protected void encodeEnum(Encoder encoder, Schema enumSchema, Object value) throws IOException { String enumValue = value instanceof Enum ? ((Enum) value).name() : value.toString(); encoder.writeString(enumValue); }
/** * Encodes a map. */ static void encodeMap(Map<String, String> map, Encoder encoder) throws IOException { encoder.writeInt(map.size()); for (Map.Entry<String, String> entry : map.entrySet()) { String value = entry.getValue(); encoder.writeString(entry.getKey()) .writeInt(value == null ? 1 : 0) .writeString(entry.getValue()); } if (!map.isEmpty()) { encoder.writeInt(0); } }
public static byte[] serializeHashKeys(Map<String, Integer> hashKeys) throws IOException { // many entries will have no hash keys. Reuse a static value for that if (hashKeys == null || hashKeys.isEmpty()) { return SERIALIZED_EMPTY_HASH_KEYS; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); Encoder encoder = new BinaryEncoder(bos); encoder.writeInt(hashKeys.size()); for (Map.Entry<String, Integer> entry : hashKeys.entrySet()) { encoder.writeString(entry.getKey()).writeInt(entry.getValue()); } encoder.writeInt(0); // per Avro spec, end with a (block of length) zero return bos.toByteArray(); }
/** * Encodes the given {@link StreamEventData} using the {@link Encoder}. * * @param data The data to encode * @param encoder The encoder * @throws IOException If there is any IO error during encoding. */ public static void encode(StreamEventData data, Encoder encoder) throws IOException { // The schema is sorted by name, hence it is {body, header}. // Writes the body encoder.writeBytes(data.getBody()); // Writes the headers Map<String, String> headers = data.getHeaders(); encoder.writeInt(headers.size()); for (Map.Entry<String, String> entry : headers.entrySet()) { String value = entry.getValue(); encoder.writeString(entry.getKey()) .writeInt(value == null ? 1 : 0) .writeString(entry.getValue()); } if (!headers.isEmpty()) { encoder.writeInt(0); } }
break; case STRING: encoder.writeString((String) value); break; case ENUM:
break; case STRING: encoder.writeString((String) value); break; case ENUM:
Assert.assertEquals(Double.MIN_VALUE, decoder.readDouble(), 0.0000001f); encoder.writeString("This is a testing message"); Assert.assertEquals("This is a testing message", decoder.readString()); String str = Character.toString((char) 200) + Character.toString((char) 20000) + Character.toString((char) 40000); encoder.writeString(str); Assert.assertEquals(str, decoder.readString());