use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class RecordWithString method testCollection.
@Test
public void testCollection() throws UnsupportedTypeException, IOException {
List<String> list = Lists.newArrayList("1", "2", "3");
Schema sourceSchema = new ReflectionSchemaGenerator().generate(new TypeToken<List<String>>() {
}.getType());
Schema targetSchema = new ReflectionSchemaGenerator().generate(new TypeToken<Set<String>>() {
}.getType());
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
Set<String> set = new ReflectionDatumReader<>(targetSchema, new TypeToken<Set<String>>() {
}).read(new BinaryDecoder(input), sourceSchema);
Assert.assertEquals(Sets.newHashSet("1", "2", "3"), set);
targetSchema = new ReflectionSchemaGenerator().generate(String[].class);
new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
String[] array = new ReflectionDatumReader<>(targetSchema, new TypeToken<String[]>() {
}).read(new BinaryDecoder(input), sourceSchema);
Assert.assertArrayEquals(new String[] { "1", "2", "3" }, array);
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class UserIdentityCodec method encode.
@Override
public byte[] encode(UserIdentity identifier) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(bos);
encoder.writeInt(UserIdentity.Schemas.getVersion());
DatumWriter<UserIdentity> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
writer.encode(identifier, encoder);
return bos.toByteArray();
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class AccessTokenCodec method encode.
@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();
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.
the class KeyIdentifierCodec method encode.
@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();
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.
the class UserIdentityCodec method encode.
@Override
public byte[] encode(UserIdentity identifier) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(bos);
encoder.writeInt(UserIdentity.Schemas.getVersion());
DatumWriter<UserIdentity> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
writer.encode(identifier, encoder);
return bos.toByteArray();
}
Aggregations