Example usage for org.apache.commons.lang SerializationUtils deserialize

List of usage examples for org.apache.commons.lang SerializationUtils deserialize

Introduction

In this page you can find the example usage for org.apache.commons.lang SerializationUtils deserialize.

Prototype

public static Object deserialize(byte[] objectData) 

Source Link

Document

Deserializes a single Object from an array of bytes.

Usage

From source file:org.apache.directory.server.ldap.handlers.extended.StoredProcedureExtendedOperationHandler.java

public void handleExtendedOperation(LdapSession session, StoredProcedureRequest req) throws Exception {
    String procedure = req.getProcedureSpecification();
    Entry spUnit = manager.findStoredProcUnit(session.getCoreSession(), procedure);
    StoredProcEngine engine = manager.getStoredProcEngineInstance(spUnit);

    List<Object> valueList = new ArrayList<>(req.size());

    for (int ii = 0; ii < req.size(); ii++) {
        byte[] serializedValue = (byte[]) req.getParameterValue(ii);
        Object value = SerializationUtils.deserialize(serializedValue);

        if (value.getClass().equals(LdapContextParameter.class)) {
            String paramCtx = ((LdapContextParameter) value).getValue();
            value = session.getCoreSession().lookup(new Dn(paramCtx));
        }//from  ww w  . ja v  a 2s  .  co m

        valueList.add(value);
    }

    Object[] values = valueList.toArray(EMPTY_CLASS_ARRAY);
    Object response = engine.invokeProcedure(session.getCoreSession(), procedure, values);
    byte[] serializedResponse = SerializationUtils.serialize((Serializable) response);
    StoredProcedureResponse resp = LdapApiServiceFactory.getSingleton()
            .newExtendedResponse(req.getRequestName(), req.getMessageId(), serializedResponse);
    session.getIoSession().write(resp);
}

From source file:org.apache.flink.streaming.api.streamcomponent.AbstractStreamComponent.java

@SuppressWarnings("unchecked")
protected static <T> T deserializeObject(byte[] serializedObject) throws IOException, ClassNotFoundException {
    return (T) SerializationUtils.deserialize(serializedObject);
}

From source file:org.apache.hama.ml.ann.NeuralNetwork.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from  w w w. ja v a 2 s .  c o  m*/
public void readFields(DataInput input) throws IOException {
    // read model type
    this.modelType = WritableUtils.readString(input);
    // read learning rate
    this.learningRate = input.readDouble();
    // read model path
    this.modelPath = WritableUtils.readString(input);

    if (this.modelPath.equals("null")) {
        this.modelPath = null;
    }

    // read feature transformer
    int bytesLen = input.readInt();
    byte[] featureTransformerBytes = new byte[bytesLen];
    for (int i = 0; i < featureTransformerBytes.length; ++i) {
        featureTransformerBytes[i] = input.readByte();
    }

    Class<? extends FeatureTransformer> featureTransformerCls = (Class<? extends FeatureTransformer>) SerializationUtils
            .deserialize(featureTransformerBytes);

    Constructor[] constructors = featureTransformerCls.getDeclaredConstructors();
    Constructor constructor = constructors[0];

    try {
        this.featureTransformer = (FeatureTransformer) constructor.newInstance(new Object[] {});
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.hama.ml.perception.SmallMultiLayerPerceptron.java

@SuppressWarnings("rawtypes")
@Override/*from  www .  j  a  v a 2 s .c o m*/
public void readFields(DataInput input) throws IOException {
    this.MLPType = WritableUtils.readString(input);
    this.learningRate = input.readDouble();
    this.regularization = input.readDouble();
    this.momentum = input.readDouble();
    this.numberOfLayers = input.readInt();
    this.squashingFunctionName = WritableUtils.readString(input);
    this.costFunctionName = WritableUtils.readString(input);

    this.squashingFunction = FunctionFactory.createDoubleFunction(this.squashingFunctionName);
    this.costFunction = FunctionFactory.createDoubleDoubleFunction(this.costFunctionName);

    // read the number of neurons for each layer
    this.layerSizeArray = new int[this.numberOfLayers];
    for (int i = 0; i < numberOfLayers; ++i) {
        this.layerSizeArray[i] = input.readInt();
    }
    this.weightMatrice = new DenseDoubleMatrix[this.numberOfLayers - 1];
    for (int i = 0; i < numberOfLayers - 1; ++i) {
        this.weightMatrice[i] = (DenseDoubleMatrix) MatrixWritable.read(input);
    }

    // read feature transformer
    int bytesLen = input.readInt();
    byte[] featureTransformerBytes = new byte[bytesLen];
    for (int i = 0; i < featureTransformerBytes.length; ++i) {
        featureTransformerBytes[i] = input.readByte();
    }
    Class featureTransformerCls = (Class) SerializationUtils.deserialize(featureTransformerBytes);
    Constructor constructor = featureTransformerCls.getConstructors()[0];
    try {
        this.featureTransformer = (FeatureTransformer) constructor.newInstance(new Object[] {});
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.horn.core.AbstractNeuralNetwork.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from  w ww  .ja va  2  s .com*/
public void readFields(DataInput input) throws IOException {
    // read model type
    this.modelType = WritableUtils.readString(input);
    // read learning rate
    this.learningRate = input.readFloat();
    // read model path
    this.modelPath = WritableUtils.readString(input);

    if (this.modelPath.equals("null")) {
        this.modelPath = null;
    }

    // read feature transformer
    int bytesLen = input.readInt();
    byte[] featureTransformerBytes = new byte[bytesLen];
    for (int i = 0; i < featureTransformerBytes.length; ++i) {
        featureTransformerBytes[i] = input.readByte();
    }

    Class<? extends FloatFeatureTransformer> featureTransformerCls = (Class<? extends FloatFeatureTransformer>) SerializationUtils
            .deserialize(featureTransformerBytes);

    Constructor[] constructors = featureTransformerCls.getDeclaredConstructors();
    Constructor constructor = constructors[0];

    try {
        this.featureTransformer = (FloatFeatureTransformer) constructor.newInstance(new Object[] {});
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.kylin.storage.hbase.ii.coprocessor.endpoint.IIEndpoint.java

private Scan prepareScan(IIProtos.IIRequest request, HRegion region) throws IOException {
    Scan scan = new Scan();

    scan.addColumn(IIDesc.HBASE_FAMILY_BYTES, IIDesc.HBASE_QUALIFIER_BYTES);
    scan.addColumn(IIDesc.HBASE_FAMILY_BYTES, IIDesc.HBASE_DICTIONARY_BYTES);

    if (request.hasTsRange()) {
        Range<Long> tsRange = (Range<Long>) SerializationUtils
                .deserialize(HBaseZeroCopyByteString.zeroCopyGetBytes(request.getTsRange()));
        byte[] regionStartKey = region.getStartKey();
        if (!ArrayUtils.isEmpty(regionStartKey)) {
            shard = BytesUtil.readUnsigned(regionStartKey, 0, IIKeyValueCodec.SHARD_LEN);
        } else {/*w  w  w .jav a2s  .c  om*/
            shard = 0;
        }
        logger.info("Start key of the region is: " + BytesUtil.toReadableText(regionStartKey)
                + ", making shard to be :" + shard);

        if (tsRange.hasLowerBound()) {
            //differentiate GT and GTE seems not very beneficial
            Preconditions.checkArgument(shard != -1, "Shard is -1!");
            long tsStart = tsRange.lowerEndpoint();
            logger.info("ts start is " + tsStart);

            byte[] idealStartKey = new byte[IIKeyValueCodec.SHARD_LEN + IIKeyValueCodec.TIMEPART_LEN];
            BytesUtil.writeUnsigned(shard, idealStartKey, 0, IIKeyValueCodec.SHARD_LEN);
            BytesUtil.writeLong(tsStart, idealStartKey, IIKeyValueCodec.SHARD_LEN,
                    IIKeyValueCodec.TIMEPART_LEN);
            logger.info("ideaStartKey is(readable) :" + BytesUtil.toReadableText(idealStartKey));
            Result result = region.getClosestRowBefore(idealStartKey, IIDesc.HBASE_FAMILY_BYTES);
            if (result != null) {
                byte[] actualStartKey = Arrays.copyOf(result.getRow(),
                        IIKeyValueCodec.SHARD_LEN + IIKeyValueCodec.TIMEPART_LEN);
                scan.setStartRow(actualStartKey);
                logger.info("The start key is set to " + BytesUtil.toReadableText(actualStartKey));
            } else {
                logger.info("There is no key before ideaStartKey so ignore tsStart");
            }
        }

        if (tsRange.hasUpperBound()) {
            //differentiate LT and LTE seems not very beneficial
            Preconditions.checkArgument(shard != -1, "Shard is -1");
            long tsEnd = tsRange.upperEndpoint();
            logger.info("ts end is " + tsEnd);

            byte[] actualEndKey = new byte[IIKeyValueCodec.SHARD_LEN + IIKeyValueCodec.TIMEPART_LEN];
            BytesUtil.writeUnsigned(shard, actualEndKey, 0, IIKeyValueCodec.SHARD_LEN);
            BytesUtil.writeLong(tsEnd + 1, actualEndKey, IIKeyValueCodec.SHARD_LEN,
                    IIKeyValueCodec.TIMEPART_LEN);//notice +1 here
            scan.setStopRow(actualEndKey);
            logger.info("The stop key is set to " + BytesUtil.toReadableText(actualEndKey));
        }
    }

    return scan;
}

From source file:org.apache.lens.api.serialize.SerializationTest.java

public void verifySerializationAndDeserialization(final Serializable originalSerializable) {

    byte[] bytes = SerializationUtils.serialize(originalSerializable);

    T objectCreatedFromBytes = (T) SerializationUtils.deserialize(bytes);

    assertEquals(objectCreatedFromBytes, originalSerializable);
}

From source file:org.apache.metamodel.util.HdfsResourceTest.java

public void testSerialization() throws Exception {
    final HdfsResource res1 = new HdfsResource("hdfs://localhost:9000/home/metamodel.txt");
    final byte[] bytes = SerializationUtils.serialize(res1);
    final Object res2 = SerializationUtils.deserialize(bytes);
    assertEquals(res1, res2);// w ww. j  a  v  a  2  s  . c  o  m
}

From source file:org.apache.metron.parsers.integration.ParserDriver.java

public ProcessorResult<List<byte[]>> run(Iterable<byte[]> in) {
    ShimParserBolt bolt = new ShimParserBolt(new ArrayList<>());
    byte[] b = SerializationUtils.serialize(bolt);
    ShimParserBolt b2 = (ShimParserBolt) SerializationUtils.deserialize(b);
    OutputCollector collector = mock(OutputCollector.class);
    bolt.prepare(null, null, collector);
    for (byte[] record : in) {
        bolt.execute(toTuple(record));//from ww  w  .j  a  va2  s  .com
    }
    return bolt.getResults();
}

From source file:org.apache.ojb.broker.accesslayer.conversions.Object2ByteArrUncompressedFieldConversion.java

public Object sqlToJava(Object source) {
    if (source == null)
        return null;
    try {//w  w  w. j  av  a 2s. c  o m
        return SerializationUtils.deserialize((byte[]) source);
    } catch (Throwable t) {
        throw new ConversionException(t);
    }
}