/** * Encodes the ending of an array. This method writes out {@code 0} to signal the end of the array. * Sub-class can override this to have different behavior * * @param encoder The encoder to use * @param elementSchema The {@link Schema} of the array element * @param size Number of array elements * @throws IOException If failed to encode */ protected void encodeArrayEnd(Encoder encoder, Schema elementSchema, int size) throws IOException { encoder.writeInt(0); }
@Override protected void writeInt(Encoder encoder, int val) throws IOException { encoder.writeInt(val); }
/** * Encodes the beginning of an array. This method will encode with the size of the array. * Sub-class can override this to have different behavior * * @param encoder The encoder to use * @param elementSchema The {@link Schema} of the array element * @param size Number of array elements * @throws IOException If failed to encode */ protected void encodeArrayBegin(Encoder encoder, Schema elementSchema, int size) throws IOException { encoder.writeInt(size); }
/** * Encodes the beginning of an array. This method will encode with the size of the array. * Sub-class can override this to have different behavior * * @param encoder The encoder to use * @param elementSchema The {@link Schema} of the array element * @param size Number of array elements * @throws IOException If failed to encode */ protected void encodeArrayBegin(Encoder encoder, Schema elementSchema, int size) throws IOException { encoder.writeInt(size); }
/** * Encodes the ending of an array. This method writes out {@code 0} to signal the end of the array. * Sub-class can override this to have different behavior * * @param encoder The encoder to use * @param elementSchema The {@link Schema} of the array element * @param size Number of array elements * @throws IOException If failed to encode */ protected void encodeArrayEnd(Encoder encoder, Schema elementSchema, int size) throws IOException { encoder.writeInt(0); }
/** * Encodes the ending of a {@link Map}. This method writes out {@code 0} to signal the end of the map. * Sub-class can override this to have different behavior. * * @param encoder The encoder to use * @param keySchema The {@link Schema} of the map key * @param valueSchema The {@link Schema} of the map value * @param size size of the map * @throws IOException If failed to encode */ protected void encodeMapEnd(Encoder encoder, Schema keySchema, Schema valueSchema, int size) throws IOException { encoder.writeInt(0); }
/** * Encodes the beginning of a {@link Map}. This method will write out the size of the map. * Sub-class can override this to have different behavior. * * @param encoder The encoder to use * @param keySchema The {@link Schema} of the map key * @param valueSchema The {@link Schema} of the map value * @param size size of the map * @throws IOException If failed to encode */ protected void encodeMapBegin(Encoder encoder, Schema keySchema, Schema valueSchema, int size) throws IOException { encoder.writeInt(size); }
@Override protected void writeArray(Encoder encoder, Object array, Schema componentSchema) throws IOException { int size = Array.getLength(array); encoder.writeInt(size); for (int i = 0; i < size; i++) { write(encoder, Array.get(array, i), componentSchema); } if (size > 0) { encoder.writeInt(0); } }
@Override protected void writeArray(Encoder encoder, Object array, Schema componentSchema) throws IOException { int size = Array.getLength(array); encoder.writeInt(size); for (int i = 0; i < size; i++) { write(encoder, Array.get(array, i), componentSchema); } if (size > 0) { encoder.writeInt(0); } }
@Override protected void writeUnion(Encoder encoder, Object val, Schema schema) throws IOException { // Assumption in schema generation that index 0 is the object type, index 1 is null. if (val == null) { encoder.writeInt(1); } else { seenRefs.remove(val); encoder.writeInt(0); write(encoder, val, schema.getUnionSchema(0)); } } }
@Override protected void writeEnum(Encoder encoder, String val, Schema schema) throws IOException { int idx = schema.getEnumIndex(val); if (idx < 0) { throw new IOException("Invalid enum value " + val); } encoder.writeInt(idx); }
@Override protected void writeUnion(Encoder encoder, Object val, Schema schema) throws IOException { // Assumption in schema generation that index 0 is the object type, index 1 is null. if (val == null) { encoder.writeInt(1); } else { seenRefs.remove(val); encoder.writeInt(0); write(encoder, val, schema.getUnionSchema(0)); } } }
private static byte[] serializeEmptyHashKeys() { try { // we don't synchronize here: the worst thing that go wrong here is repeated assignment to the same value ByteArrayOutputStream bos = new ByteArrayOutputStream(); Encoder encoder = new BinaryEncoder(bos); encoder.writeInt(0); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException("encoding empty hash keys went wrong - bailing out: " + e.getMessage(), e); } } private static final byte[] SERIALIZED_EMPTY_HASH_KEYS = serializeEmptyHashKeys();
@Override protected void writeEnum(Encoder encoder, String val, Schema schema) throws IOException { int idx = schema.getEnumIndex(val); if (idx < 0) { throw new IOException("Invalid enum value " + val); } encoder.writeInt(idx); }
/** * Encodes a enum value. This method will encode with the enum index of the enum value according to the Schema. * Sub-class can override this to have different behavior. * * @param encoder The encoder to use * @param enumSchema The {@link Schema} for the enum field * @param value The enum value * @throws IOException If failed to encode */ protected void encodeEnum(Encoder encoder, Schema enumSchema, Object value) throws IOException { String enumValue = value instanceof Enum ? ((Enum) value).name() : value.toString(); encoder.writeInt(enumSchema.getEnumIndex(enumValue)); }
@Override public byte[] encode(AccessToken token) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Encoder encoder = new BinaryEncoder(bos); encoder.writeInt(AccessToken.Schemas.getVersion()); DatumWriter<AccessToken> writer = writerFactory.create(ACCESS_TOKEN_TYPE, AccessToken.Schemas.getCurrentSchema()); writer.encode(token, encoder); return bos.toByteArray(); }
@Override public byte[] encode(KeyIdentifier keyIdentifier) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Encoder encoder = new BinaryEncoder(bos); encoder.writeInt(KeyIdentifier.Schemas.getVersion()); DatumWriter<KeyIdentifier> writer = writerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema()); writer.encode(keyIdentifier, encoder); return bos.toByteArray(); }
@Override public byte[] encode(AccessTokenIdentifier identifier) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Encoder encoder = new BinaryEncoder(bos); encoder.writeInt(AccessTokenIdentifier.Schemas.getVersion()); DatumWriter<AccessTokenIdentifier> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, AccessTokenIdentifier.Schemas.getCurrentSchema()); writer.encode(identifier, encoder); return bos.toByteArray(); }