Search in sources :

Example 36 with BinaryDecoder

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);
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.cdap.cdap.api.data.schema.Schema) IOException(java.io.IOException) Decoder(io.cdap.cdap.common.io.Decoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder)

Example 37 with BinaryDecoder

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.cdap.cdap.api.data.schema.Schema) IOException(java.io.IOException) Decoder(io.cdap.cdap.common.io.Decoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder)

Example 38 with BinaryDecoder

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.cdap.cdap.api.data.schema.Schema) IOException(java.io.IOException) Decoder(io.cdap.cdap.common.io.Decoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder)

Example 39 with BinaryDecoder

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);
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder)

Example 40 with BinaryDecoder

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);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Aggregations

BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)52 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)42 Test (org.junit.Test)42 PipedInputStream (java.io.PipedInputStream)40 PipedOutputStream (java.io.PipedOutputStream)40 ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)34 TypeToken (com.google.common.reflect.TypeToken)32 Schema (io.cdap.cdap.api.data.schema.Schema)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ImmutableList (com.google.common.collect.ImmutableList)10 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)10 IOException (java.io.IOException)10 List (java.util.List)10 Decoder (io.cdap.cdap.common.io.Decoder)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)4 Predicate (com.google.common.base.Predicate)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 DataSetException (io.cdap.cdap.api.dataset.DataSetException)2