use of io.cdap.cdap.common.io.Encoder 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.Encoder 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.Encoder 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();
}
use of io.cdap.cdap.common.io.Encoder in project cdap by cdapio.
the class DatumWriterGenerator method encodeArray.
/**
* Generates method body for encoding array value. The logic is similar to the one in
* {@link #encodeCollection}, with collection size replaced with array length.
*
* @param mg
* @param componentType
* @param componentSchema
* @param value
* @param encoder
* @param schemaLocal
* @param seenRefs
*/
private void encodeArray(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value, int encoder, int schemaLocal, int seenRefs) {
// Encode and store the array length locally
mg.loadArg(value);
mg.arrayLength();
int length = mg.newLocal(Type.INT_TYPE);
mg.storeLocal(length);
mg.loadArg(encoder);
mg.loadLocal(length);
mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
mg.pop();
// Store the component schema
mg.loadArg(schemaLocal);
mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema"));
int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class));
mg.storeLocal(componentSchemaLocal);
// for (int idx = 0; idx < array.length; idx++)
mg.push(0);
int idx = mg.newLocal(Type.INT_TYPE);
mg.storeLocal(idx);
Label beginFor = mg.mark();
Label endFor = mg.newLabel();
mg.loadLocal(idx);
mg.loadLocal(length);
mg.ifICmp(GeneratorAdapter.GE, endFor);
// Call encode method to encode array[idx]
mg.loadThis();
mg.loadArg(value);
mg.loadLocal(idx);
TypeToken<?> callTypeToken = getCallTypeToken(componentType, componentSchema);
mg.arrayLoad(Type.getType(callTypeToken.getRawType()));
mg.loadArg(encoder);
mg.loadLocal(componentSchemaLocal);
mg.loadArg(seenRefs);
mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));
mg.iinc(idx, 1);
mg.goTo(beginFor);
mg.mark(endFor);
// if length > 0, write out 0 at the end of array.
Label zeroLength = mg.newLabel();
mg.loadLocal(length);
mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
encodeInt(mg, 0, encoder);
mg.mark(zeroLength);
}
use of io.cdap.cdap.common.io.Encoder in project cdap by cdapio.
the class DatumWriterGenerator method encodeSimple.
/**
* Generates method body for encoding simple schema type by calling corresponding write method in Encoder.
* @param mg Method body generator
* @param type Data type to encode
* @param encodeMethod Name of the encode method to invoke on the given encoder.
* @param value Argument index of the value to encode.
* @param encoder Method argument index of the encoder
*/
private void encodeSimple(GeneratorAdapter mg, TypeToken<?> type, Schema schema, String encodeMethod, int value, int encoder) {
// encoder.writeXXX(value);
TypeToken<?> encodeType = type;
mg.loadArg(encoder);
mg.loadArg(value);
boolean isPrimitiveBoxType = Primitives.isWrapperType(encodeType.getRawType());
if (encodeType.getRawType().isPrimitive() || isPrimitiveBoxType) {
// Unwrap it if it is boxed type, otherwise it won't change the encodeType.
encodeType = TypeToken.of(Primitives.unwrap(encodeType.getRawType()));
if (isPrimitiveBoxType) {
mg.unbox(Type.getType(encodeType.getRawType()));
}
// A special case since INT type represents (byte, char, short and int).
if (schema.getType() == Schema.Type.INT && !int.class.equals(encodeType.getRawType())) {
encodeType = TypeToken.of(int.class);
}
} else if (schema.getType() == Schema.Type.STRING && !String.class.equals(encodeType.getRawType())) {
// For non-string object that has a String schema, invoke toString().
mg.invokeVirtual(Type.getType(encodeType.getRawType()), getMethod(String.class, "toString"));
encodeType = TypeToken.of(String.class);
} else if (schema.getType() == Schema.Type.BYTES && UUID.class.equals(encodeType.getRawType())) {
// Special case UUID, encode as byte array
// ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES * 2)
// .putLong(uuid.getMostSignificantBits())
// .putLong(uuid.getLeastSignificantBits());
// encoder.writeBytes((ByteBuffer) buf.flip());
Type byteBufferType = Type.getType(ByteBuffer.class);
Type uuidType = Type.getType(UUID.class);
mg.push(Longs.BYTES * 2);
mg.invokeStatic(byteBufferType, getMethod(ByteBuffer.class, "allocate", int.class));
mg.swap();
mg.invokeVirtual(uuidType, getMethod(long.class, "getMostSignificantBits"));
mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class));
mg.loadArg(value);
mg.invokeVirtual(uuidType, getMethod(long.class, "getLeastSignificantBits"));
mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class));
mg.invokeVirtual(Type.getType(Buffer.class), getMethod(Buffer.class, "flip"));
mg.checkCast(byteBufferType);
encodeType = TypeToken.of(ByteBuffer.class);
}
mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, encodeMethod, encodeType.getRawType()));
mg.pop();
}
Aggregations