use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class CodecTest method testCodec.
@Test
public void testCodec() throws IOException {
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
Encoder encoder = new BinaryEncoder(output);
Decoder decoder = new BinaryDecoder(input);
encoder.writeNull();
Assert.assertNull(decoder.readNull());
encoder.writeBool(true);
Assert.assertTrue(decoder.readBool());
encoder.writeBool(false);
Assert.assertFalse(decoder.readBool());
encoder.writeInt(0);
Assert.assertEquals(0, decoder.readInt());
encoder.writeInt(-1);
Assert.assertEquals(-1, decoder.readInt());
encoder.writeInt(1234);
Assert.assertEquals(1234, decoder.readInt());
encoder.writeInt(-1234);
Assert.assertEquals(-1234, decoder.readInt());
encoder.writeInt(Short.MAX_VALUE);
Assert.assertEquals(Short.MAX_VALUE, decoder.readInt());
encoder.writeInt(Short.MIN_VALUE);
Assert.assertEquals(Short.MIN_VALUE, decoder.readInt());
encoder.writeInt(Integer.MAX_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, decoder.readInt());
encoder.writeInt(Integer.MIN_VALUE);
Assert.assertEquals(Integer.MIN_VALUE, decoder.readInt());
encoder.writeLong(0);
Assert.assertEquals(0, decoder.readLong());
encoder.writeLong(-20);
Assert.assertEquals(-20, decoder.readLong());
encoder.writeLong(30000);
Assert.assertEquals(30000, decoder.readLong());
encoder.writeLong(-600000);
Assert.assertEquals(-600000, decoder.readLong());
encoder.writeLong(Integer.MAX_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, decoder.readLong());
encoder.writeLong(Integer.MIN_VALUE);
Assert.assertEquals(Integer.MIN_VALUE, decoder.readLong());
encoder.writeLong(Long.MAX_VALUE);
Assert.assertEquals(Long.MAX_VALUE, decoder.readLong());
encoder.writeLong(Long.MIN_VALUE);
Assert.assertEquals(Long.MIN_VALUE, decoder.readLong());
encoder.writeFloat(3.14f);
Assert.assertEquals(3.14f, decoder.readFloat(), 0.0000001f);
encoder.writeFloat(Short.MAX_VALUE);
Assert.assertEquals(Short.MAX_VALUE, decoder.readFloat(), 0.0000001f);
encoder.writeFloat(Integer.MIN_VALUE);
Assert.assertEquals(Integer.MIN_VALUE, decoder.readFloat(), 0.0000001f);
encoder.writeFloat((long) Integer.MAX_VALUE * Short.MAX_VALUE);
Assert.assertEquals((long) Integer.MAX_VALUE * Short.MAX_VALUE, decoder.readFloat(), 0.0000001f);
encoder.writeFloat(Float.MAX_VALUE);
Assert.assertEquals(Float.MAX_VALUE, decoder.readFloat(), 0.0000001f);
encoder.writeFloat(Float.MIN_VALUE);
Assert.assertEquals(Float.MIN_VALUE, decoder.readFloat(), 0.0000001f);
encoder.writeDouble(Math.E);
Assert.assertEquals(Math.E, decoder.readDouble(), 0.0000001f);
encoder.writeDouble(Integer.MAX_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, decoder.readDouble(), 0.0000001f);
encoder.writeDouble(Long.MIN_VALUE);
Assert.assertEquals(Long.MIN_VALUE, decoder.readDouble(), 0.0000001f);
encoder.writeDouble((long) Integer.MAX_VALUE * Short.MAX_VALUE);
Assert.assertEquals((long) Integer.MAX_VALUE * Short.MAX_VALUE, decoder.readDouble(), 0.0000001f);
encoder.writeDouble(Double.MAX_VALUE);
Assert.assertEquals(Double.MAX_VALUE, decoder.readDouble(), 0.0000001f);
encoder.writeDouble(Double.MIN_VALUE);
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());
ByteBuffer buf = ByteBuffer.allocate(12);
buf.asIntBuffer().put(10).put(1024).put(9999999);
encoder.writeBytes(buf);
IntBuffer inBuf = decoder.readBytes().asIntBuffer();
Assert.assertEquals(10, inBuf.get());
Assert.assertEquals(1024, inBuf.get());
Assert.assertEquals(9999999, inBuf.get());
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class RecordWithString method testEmptyValue.
// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@Test
public void testEmptyValue() throws UnsupportedTypeException, IOException {
Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class);
TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class);
DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema);
@SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>(schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType()));
RecordWithString record = new RecordWithString();
record.setA(42);
record.setTheString("");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryEncoder encoder = new BinaryEncoder(bos);
datumWriter.encode(record, encoder);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BinaryDecoder decoder = new BinaryDecoder(bis);
RecordWithString rec = datumReader.read(decoder, schema);
Assert.assertEquals(record.getA(), rec.getA());
Assert.assertEquals(record.getTheString(), rec.getTheString());
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class RecordWithString method testEnum.
@Test
public void testEnum() throws IOException, UnsupportedTypeException {
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
Schema schema = new ReflectionSchemaGenerator().generate(TestEnum.class);
ReflectionDatumWriter<TestEnum> writer = new ReflectionDatumWriter<>(schema);
BinaryEncoder encoder = new BinaryEncoder(output);
writer.encode(TestEnum.VALUE1, encoder);
writer.encode(TestEnum.VALUE3, encoder);
writer.encode(TestEnum.VALUE2, encoder);
BinaryDecoder decoder = new BinaryDecoder(input);
Schema readSchema = Schema.parseJson(schema.toString());
ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(readSchema, TypeToken.of(TestEnum.class));
Assert.assertEquals(TestEnum.VALUE1, reader.read(decoder, readSchema));
Assert.assertEquals(TestEnum.VALUE3, reader.read(decoder, readSchema));
Assert.assertEquals(TestEnum.VALUE2, reader.read(decoder, readSchema));
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
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 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);
}
}
}
Aggregations