Search in sources :

Example 1 with SerializationException

use of voldemort.serialization.SerializationException in project voldemort by voldemort.

the class AvroVersionedGenericSerializer method toBytes.

/*
     * Serialize a given object using a non latest schema With auto rebootstrap
     * the client gets the latest schema updated on the server However an
     * application may still create objects using an old schema this lets us
     * serialize those objects without an exception
     */
private byte[] toBytes(Object object, Schema writer, Integer writerVersion) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(output);
    GenericDatumWriter<Object> datumWriter = null;
    output.write(writerVersion.byteValue());
    try {
        datumWriter = new GenericDatumWriter<Object>(writer);
        datumWriter.write(object, encoder);
        encoder.flush();
    } catch (IOException e) {
        throw new SerializationException(e);
    } catch (SerializationException sE) {
        throw sE;
    } finally {
        SerializationUtils.close(output);
    }
    return output.toByteArray();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with SerializationException

use of voldemort.serialization.SerializationException in project voldemort by voldemort.

the class AvroVersionedGenericSerializer method toBytes.

public byte[] toBytes(Object object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(output);
    GenericDatumWriter<Object> datumWriter = null;
    output.write(newestVersion.byteValue());
    try {
        datumWriter = new GenericDatumWriter<Object>(typeDef);
        datumWriter.write(object, encoder);
        encoder.flush();
    } catch (SerializationException sE) {
        throw sE;
    } catch (IOException e) {
        throw new SerializationException(e);
    } catch (Exception aIOBE) {
        // probably the object sent to us was not created using the latest
        // schema
        // We simply check the old version number and serialize it using the
        // old schema version
        Schema writer = ((GenericContainer) object).getSchema();
        Integer writerVersion = getSchemaVersion(writer);
        return toBytes(object, writer, writerVersion);
    } finally {
        SerializationUtils.close(output);
    }
    return output.toByteArray();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) SerializationException(voldemort.serialization.SerializationException)

Example 3 with SerializationException

use of voldemort.serialization.SerializationException in project voldemort by voldemort.

the class AvroVersionedGenericSerializer method toObject.

public Object toObject(byte[] bytes) {
    Integer version = Integer.valueOf(bytes[0]);
    if (version > newestVersion)
        throw new SerializationException("Client needs to rebootstrap! \n Writer's schema version greater than Reader");
    Schema typeDefWriter = Schema.parse(typeDefVersions.get(version));
    Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, 1, bytes.length - 1, null);
    GenericDatumReader<Object> reader = null;
    try {
        reader = new GenericDatumReader<Object>(typeDefWriter, typeDef);
        // writer's schema
        reader.setSchema(typeDefWriter);
        // Reader's schema
        reader.setExpected(typeDef);
        return reader.read(null, decoder);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(voldemort.serialization.SerializationException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder)

Example 4 with SerializationException

use of voldemort.serialization.SerializationException in project voldemort by voldemort.

the class JsonReader method readUnicodeLiteral.

private char readUnicodeLiteral() {
    int value = 0;
    for (int i = 0; i < 4; i++) {
        next();
        value <<= 4;
        if (Character.isDigit(current()))
            value += current() - '0';
        else if ('a' <= current() && 'f' >= current())
            value += 10 + current() - 'a';
        else if ('A' <= current() && 'F' >= current())
            value += 10 + current() - 'A';
        else
            throw new SerializationException("Invalid character in unicode sequence on line " + getCurrentLineNumber() + ": " + currentChar());
    }
    return (char) value;
}
Also used : SerializationException(voldemort.serialization.SerializationException)

Example 5 with SerializationException

use of voldemort.serialization.SerializationException in project voldemort by voldemort.

the class JsonReader method next.

private int next() {
    try {
        // read a character
        this.current = this.reader.read();
        // increment the character count and maybe line number
        this.charsRead++;
        this.lineOffset++;
        if (this.current == this.lineBreak) {
            this.line++;
            this.lineOffset = 1;
        }
        // add to context buffer
        this.contextBuffer[this.contextOffset] = (char) this.current;
        this.contextOffset = (contextOffset + 1) % this.contextBuffer.length;
        return this.current;
    } catch (IOException e) {
        throw new SerializationException("Error reading from JSON stream.", e);
    }
}
Also used : SerializationException(voldemort.serialization.SerializationException) IOException(java.io.IOException)

Aggregations

SerializationException (voldemort.serialization.SerializationException)22 IOException (java.io.IOException)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 BinaryEncoder (org.apache.avro.io.BinaryEncoder)5 Encoder (org.apache.avro.io.Encoder)5 Decoder (org.apache.avro.io.Decoder)4 Schema (org.apache.avro.Schema)3 ByteArray (voldemort.utils.ByteArray)3 Versioned (voldemort.versioning.Versioned)3 BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TException (org.apache.thrift.TException)2 TProtocol (org.apache.thrift.protocol.TProtocol)2 Node (voldemort.cluster.Node)2 Serializer (voldemort.serialization.Serializer)2 SerializerDefinition (voldemort.serialization.SerializerDefinition)2