use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by caskdata.
the class KeyIdentifierCodec method decode.
@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());
try {
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);
} catch (IOException e) {
// As a fallback, try deserializing from JSON format.
try {
return gson.fromJson(new String(data, StandardCharsets.UTF_8), KeyIdentifier.class);
} catch (JsonSyntaxException jse) {
// For backwards compatibility, throw an IOException with the initial exception wrapped.
throw new IOException("Failed to deserialize KeyIdentifier", e);
}
}
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by caskdata.
the class UserIdentityCodec method decode.
@Override
public UserIdentity decode(byte[] data) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
Decoder decoder = new BinaryDecoder(bis);
DatumReader<UserIdentity> reader = readerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
int readVersion = decoder.readInt();
Schema readSchema = UserIdentity.Schemas.getSchemaVersion(readVersion);
if (readSchema == null) {
throw new IOException("Unknown schema version for AccessTokenIdentifier: " + readVersion);
}
return reader.read(decoder, readSchema);
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by caskdata.
the class AccessTokenCodec method decode.
@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);
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class ObjectStoreDataset method decode.
private T decode(byte[] bytes) {
if (bytes == null) {
return null;
}
// decode T using schema
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BinaryDecoder decoder = new BinaryDecoder(bis);
try {
return getReflectionDatumReader().read(decoder, this.schema);
} catch (IOException e) {
// SHOULD NEVER happen
throw new DataSetException("Failed to decode read object: " + e.getMessage(), e);
}
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class ASMDatumCodecTest method testReferenceArray.
@Test
public void testReferenceArray() throws IOException, UnsupportedTypeException {
TypeToken<String[]> type = new TypeToken<String[]>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
String[] writeValue = new String[] { "1", "2", null, "3" };
DatumWriter<String[]> writer = getWriter(type);
writer.encode(writeValue, new BinaryEncoder(os));
ReflectionDatumReader<String[]> reader = new ReflectionDatumReader<>(getSchema(type), type);
String[] value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertArrayEquals(writeValue, value);
}
Aggregations