@Override protected int readInt(Decoder decoder) throws IOException { return decoder.readInt(); }
@Override protected int readInt(Decoder decoder) throws IOException { return decoder.readInt(); }
private int readLength() throws IOException { return decoder.readInt(); }
/** * Decodes an array with array element of a given {@link Schema}. This method reads an integer as the size of * a chunk, followed by reading that many array element using the element schema, following by reading the size * of the next chunk, until it reads {@code 0}. * Sub-class can override this to have different behavior. * * @param decoder The decode to decode value from * @param elementSchema The {@link Schema} of the array elements * @return A {@link Collection} containing all array elements * @throws IOException If failed to decode */ protected Collection<?> decodeArray(Decoder decoder, Schema elementSchema) throws IOException { int size = decoder.readInt(); List<Object> array = new ArrayList<>(size); while (size != 0) { for (int i = 0; i < size; i++) { array.add(decode(decoder, elementSchema)); } size = decoder.readInt(); } return array; }
/** * Decodes a enum value based on the given {@link Schema}. This method reads an integer as the index in the * enum schema. * Sub-class can override this to have different behavior. * * @param decoder The decode to decode value from * @param schema The {@link Schema} of the enum * @return A {@link String} representation of the enum value * @throws IOException If failed to decode */ protected String decodeEnum(Decoder decoder, Schema schema) throws IOException { return schema.getEnumValue(decoder.readInt()); }
/** * Decodes a enum value based on the given {@link Schema}. This method reads an integer as the index in the * enum schema. * Sub-class can override this to have different behavior. * * @param decoder The decode to decode value from * @param schema The {@link Schema} of the enum * @return A {@link String} representation of the enum value * @throws IOException If failed to decode */ protected String decodeEnum(Decoder decoder, Schema schema) throws IOException { return schema.getEnumValue(decoder.readInt()); }
/** * Decodes an array with array element of a given {@link Schema}. This method reads an integer as the size of * a chunk, followed by reading that many array element using the element schema, following by reading the size * of the next chunk, until it reads {@code 0}. * Sub-class can override this to have different behavior. * * @param decoder The decode to decode value from * @param elementSchema The {@link Schema} of the array elements * @return A {@link Collection} containing all array elements * @throws IOException If failed to decode */ protected Collection<?> decodeArray(Decoder decoder, Schema elementSchema) throws IOException { int size = decoder.readInt(); List<Object> array = new ArrayList<>(size); while (size != 0) { for (int i = 0; i < size; i++) { array.add(decode(decoder, elementSchema)); } size = decoder.readInt(); } return array; }
/** * Decode a map. */ static Map<String, String> decodeMap(Decoder decoder) throws IOException { ImmutableMap.Builder<String, String> map = ImmutableMap.builder(); int len = decoder.readInt(); while (len != 0) { for (int i = 0; i < len; i++) { String key = decoder.readString(); String value = decoder.readInt() == 0 ? decoder.readString() : (String) decoder.readNull(); map.put(key, value); } len = decoder.readInt(); } return map.build(); }
@Override protected Object readEnum(Decoder decoder, Schema sourceSchema, Schema targetSchema, TypeToken<?> targetTypeToken) throws IOException { String enumValue = sourceSchema.getEnumValue(decoder.readInt()); check(targetSchema.getEnumValues().contains(enumValue), "Enum value '%s' missing in target.", enumValue); try { return targetTypeToken.getRawType().getMethod("valueOf", String.class).invoke(null, enumValue); } catch (Exception e) { throw new IOException(e); } }
/** * Decodes a map. This method reads an integer as the size of a chunk, followed by reading that many * key and value pairs using the key and value schema, following by reading the size * of the next chunk, until it reads {@code 0}. * Sub-class can override this to have different behavior. * * @param decoder The decode to decode value from * @param keySchema The {@link Schema} of the map key * @param valueSchema The {@link Schema} of the map value * @return A {@link Map} containing all map entries * @throws IOException If failed to decode */ protected Map<?, ?> decodeMap(Decoder decoder, Schema keySchema, Schema valueSchema) throws IOException { int size = decoder.readInt(); Map<Object, Object> map = new LinkedHashMap<>(size); while (size != 0) { for (int i = 0; i < size; i++) { map.put(decode(decoder, keySchema), decode(decoder, valueSchema)); } size = decoder.readInt(); } return map; }
@Override protected Object readEnum(Decoder decoder, Schema sourceSchema, Schema targetSchema, TypeToken<?> targetTypeToken) throws IOException { String enumValue = sourceSchema.getEnumValue(decoder.readInt()); check(targetSchema.getEnumValues().contains(enumValue), "Enum value '%s' missing in target.", enumValue); try { return targetTypeToken.getRawType().getMethod("valueOf", String.class).invoke(null, enumValue); } catch (Exception e) { throw new IOException(e); } }
/** * Decodes a value from a union. This method first decode an integer as the index in the union schema for the * value schema, followed decoding the value using that schema. * * @param decoder The decode to decode value from * @param schema The {@link Schema} of the union * @return The value decoded * @throws IOException If failed to decode */ protected Object decodeUnion(Decoder decoder, Schema schema) throws IOException { return decode(decoder, schema.getUnionSchema(decoder.readInt())); } }
/** * Decodes a value from a union. This method first decode an integer as the index in the union schema for the * value schema, followed decoding the value using that schema. * * @param decoder The decode to decode value from * @param schema The {@link Schema} of the union * @return The value decoded * @throws IOException If failed to decode */ protected Object decodeUnion(Decoder decoder, Schema schema) throws IOException { return decode(decoder, schema.getUnionSchema(decoder.readInt())); } }
@Override public AccessTokenIdentifier decode(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); Decoder decoder = new BinaryDecoder(bis); DatumReader<AccessTokenIdentifier> reader = readerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, AccessTokenIdentifier.Schemas.getCurrentSchema()); int readVersion = decoder.readInt(); Schema readSchema = AccessTokenIdentifier.Schemas.getSchemaVersion(readVersion); if (readSchema == null) { throw new IOException("Unknown schema version for AccessTokenIdentifier: " + readVersion); } return reader.read(decoder, readSchema); }
@Override public AccessToken decode(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); Decoder decoder = new BinaryDecoder(bis); DatumReader<AccessToken> reader = readerFactory.create(ACCESS_TOKEN_TYPE, AccessToken.Schemas.getCurrentSchema()); int readVersion = decoder.readInt(); Schema readSchema = AccessToken.Schemas.getSchemaVersion(readVersion); if (readSchema == null) { throw new IOException("Unknown schema version for AccessToken: " + readVersion); } return reader.read(decoder, readSchema); } }
@Override public KeyIdentifier decode(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); Decoder decoder = new BinaryDecoder(bis); DatumReader<KeyIdentifier> reader = readerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema()); int readVersion = decoder.readInt(); Schema readSchema = KeyIdentifier.Schemas.getSchemaVersion(readVersion); if (readSchema == null) { throw new IOException("Unknown schema version for KeyIdentifier: " + readVersion); } return reader.read(decoder, readSchema); } }