Example usage for org.apache.thrift TDeserializer deserialize

List of usage examples for org.apache.thrift TDeserializer deserialize

Introduction

In this page you can find the example usage for org.apache.thrift TDeserializer deserialize.

Prototype

public void deserialize(TBase base, byte[] bytes) throws TException 

Source Link

Document

Deserialize the Thrift object from a byte array.

Usage

From source file:adept.mappers.thrift.MappingTests.java

License:Apache License

private void SerializationTests(final adept.common.HltContentContainer hltAdept)
        throws TException, IOException {
    final XMLSerializer xmlSerializer = new XMLSerializer(SerializationType.XML);
    final TSerializer thriftSerializer = new TSerializer(new TBinaryProtocol.Factory());
    final TDeserializer tdeserializer = new TDeserializer(new TBinaryProtocol.Factory());
    final ThriftAdeptMapper mapper = ThriftAdeptMapper.getInstance();

    // thrift round tripping through serialization
    final thrift.adept.common.HltContentContainer hltThrift = mapper.convert(hltAdept);
    final byte[] thriftBytes = thriftSerializer.serialize(hltThrift);
    final File thriftFile = File.createTempFile("thriftBytes", "thrift");
    thriftFile.deleteOnExit();//from  w w w.ja v  a 2 s  . c o  m
    Files.asByteSink(thriftFile).write(thriftBytes);
    final thrift.adept.common.HltContentContainer serializationTrippedThrift = new thrift.adept.common.HltContentContainer();
    tdeserializer.deserialize(serializationTrippedThrift, Files.asByteSource(thriftFile).read());

    // adept fun times
    final String adeptString = xmlSerializer.serializeAsString(hltAdept);
    final File adeptFile = File.createTempFile("adeptString", "xml");
    adeptFile.deleteOnExit();
    Files.asCharSink(adeptFile, Charsets.UTF_8).write(adeptString);
    final adept.common.HltContentContainer serializedTrippedAdept = (adept.common.HltContentContainer) xmlSerializer
            .deserializeString(Files.asCharSource(adeptFile, Charsets.UTF_8).read(),
                    adept.common.HltContentContainer.class);

    assertTrue(areTheSameByLimitedThriftScope(hltAdept, hltThrift));
    // check that to/from serialization does the right thing
    assertTrue(areTheSameByLimitedThriftScope(serializedTrippedAdept, hltThrift));
    assertTrue(areTheSameByLimitedThriftScope(hltAdept, serializationTrippedThrift));
    assertTrue(areTheSameByLimitedThriftScope(serializedTrippedAdept, serializationTrippedThrift));
}

From source file:andromache.config.CassandraConfigHelper.java

License:Apache License

private static KeyRange keyRangeFromString(String st) {
    assert st != null;
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    KeyRange keyRange = new KeyRange();
    try {/*  w  w  w  .j ava  2  s  .com*/
        deserializer.deserialize(keyRange, Hex.hexToBytes(st));
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    return keyRange;
}

From source file:andromache.config.CassandraConfigHelper.java

License:Apache License

private static SlicePredicate predicateFromString(String st) {
    assert st != null;
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    SlicePredicate predicate = new SlicePredicate();
    try {/*from   w w  w  .  j a v  a2 s  . c o m*/
        deserializer.deserialize(predicate, Hex.hexToBytes(st));
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    return predicate;
}

From source file:backtype.storm.utils.LocalState.java

License:Apache License

private TBase deserialize(ThriftSerializedObject obj, TDeserializer td) {
    try {/*from   w  w  w  .  j a  v a2s.c  o m*/
        Class<?> clazz = Class.forName(obj.get_name());
        TBase instance = (TBase) clazz.newInstance();
        td.deserialize(instance, obj.get_bits());
        return instance;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:backtype.storm.utils.LocalState.java

License:Apache License

private Map<String, ThriftSerializedObject> partialDeserializeLatestVersion(TDeserializer td) {
    try {//from   www .j a  va  2s .  c  om
        String latestPath = _vs.mostRecentVersionPath();
        Map<String, ThriftSerializedObject> result = new HashMap<String, ThriftSerializedObject>();
        if (latestPath != null) {
            byte[] serialized = FileUtils.readFileToByteArray(new File(latestPath));
            if (serialized.length == 0) {
                LOG.warn("LocalState file '{}' contained no data, resetting state", latestPath);
            } else {
                if (td == null) {
                    td = new TDeserializer();
                }
                LocalStateData data = new LocalStateData();
                td.deserialize(data, serialized);
                result = data.get_serialized_parts();
            }
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.usi.da.dlog.message.Command.java

License:Open Source License

public static Command fromByteArray(byte[] b) {
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    Cmd c = new Cmd();
    try {/*from  w w w.  j  a  v  a 2 s .co m*/
        deserializer.deserialize(c, b);
        if (c.type == null) {
            return null;
        }
    } catch (TException e) {
        return null;
    }
    return toCommand(c);
}

From source file:ch.usi.da.dlog.message.Message.java

License:Open Source License

public static Message fromByteArray(byte[] b) {
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    ch.usi.da.dlog.thrift.gen.Message m = new ch.usi.da.dlog.thrift.gen.Message();
    try {/* w w w.jav  a 2  s .  c om*/
        deserializer.deserialize(m, b);
        if (m.to == null) {
            return null;
        }
    } catch (TException e) {
        return null;
    }
    List<Command> cmds = new ArrayList<Command>();
    for (Cmd c : m.getCommands()) {
        cmds.add(Command.toCommand(c));
    }
    return new Message(m.getId(), m.getFrom(), m.getTo(), cmds);
}

From source file:ch.usi.da.smr.message.Message.java

License:Open Source License

public static Message fromByteArray(byte[] b) {
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    ch.usi.da.smr.thrift.gen.Message m = new ch.usi.da.smr.thrift.gen.Message();
    try {//from  www .  j ava2s  .  c  o  m
        deserializer.deserialize(m, b);
        if (m.to == null) {
            return null;
        }
    } catch (TException e) {
        return null;
    }
    List<Command> cmds = new ArrayList<Command>();
    for (Cmd c : m.getCommands()) {
        cmds.add(Command.toCommand(c));
    }
    return new Message(m.getId(), m.getFrom(), m.getTo(), cmds);
}

From source file:co.cask.tephra.TransactionCodec.java

License:Apache License

public Transaction decode(byte[] encoded) throws IOException {
    TTransaction thriftTx = new TTransaction();
    TDeserializer deserializer = new TDeserializer();
    try {/*from   w w w . j  av  a  2  s .c om*/
        deserializer.deserialize(thriftTx, encoded);
        return TransactionConverterUtils.unwrap(thriftTx);
    } catch (TException te) {
        throw new IOException(te);
    }
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

License:Apache License

public static void deserialize(TDeserializer deserializer, TBase struct, DataInput in) throws IOException {
    assert deserializer != null;
    assert struct != null;
    assert in != null;
    byte[] bytes = new byte[in.readInt()];
    in.readFully(bytes);//from w w  w  .java 2s .co  m
    try {
        deserializer.deserialize(struct, bytes);
    } catch (TException ex) {
        throw new IOException(ex);
    }
}