Java Code Examples for com.caucho.hessian.io.AbstractHessianOutput#setSerializerFactory()

The following examples show how to use com.caucho.hessian.io.AbstractHessianOutput#setSerializerFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HessianSerializer.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(final Object obj) throws Exception {
    if (obj == null) {
        return null;
    }

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    AbstractHessianOutput output = new Hessian2Output(outputStream);
    output.setSerializerFactory(SERIALIZER_FACTORY);
    // 将对象写到流里
    output.writeObject(obj);
    output.flush();
    byte[] val = outputStream.toByteArray();
    output.close();
    return val;
}
 
Example 2
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object1() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  out.writeString("test");
  out.close();
  return bos.toByteArray();
}
 
Example 3
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object2() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  out.writeObject(new A());
  out.close();
  return bos.toByteArray();
}
 
Example 4
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object3() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  out.writeObject(new B(new A()));
  out.close();
  return bos.toByteArray();
}
 
Example 5
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object4() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  out.writeObject(new HessianSerialization());
  out.close();
  return bos.toByteArray();
}
 
Example 6
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object5() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  Map<String, String> map = new HashMap<>();
  map.put("test", "test");
  map.put("foo", "foo");
  out.writeObject(map);
  out.close();
  return bos.toByteArray();
}
 
Example 7
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object6() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  Map<String, String> map = new HashMap<>();
  map.put("test", "test");
  out.writeObject(map);
  out.close();
  return bos.toByteArray();
}
 
Example 8
Source File: HessianSerializerUtils.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 将对象序列化为字节数组
 *
 * @param obj 待序列化对象
 * @return
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream ops = new ByteArrayOutputStream();
    AbstractHessianOutput out = new Hessian2Output(ops);
    out.setSerializerFactory(new SerializerFactory());
    try {
        out.writeObject(obj);
        out.close();
    } catch (IOException e) {
        LOGGER.error("Hessian serialize failed.", e);
        throw new RuntimeException("Hessian serialize failed");
    }
    return ops.toByteArray();
}
 
Example 9
Source File: HessianBase.java    From marshalsec with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see marshalsec.MarshallerBase#marshal(java.lang.Object)
 */
@Override
public byte[] marshal ( Object o ) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    AbstractHessianOutput out = createOutput(bos);
    NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
    sf.setAllowNonSerializable(true);
    out.setSerializerFactory(sf);
    out.writeObject(o);
    out.close();
    return bos.toByteArray();
}
 
Example 10
Source File: HessianSkeleton.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
/**
 * Invoke the object with the request from the input stream.
 *
 * @param in the Hessian input stream
 * @param out the Hessian output stream
 */
public void invoke(InputStream is, OutputStream os,
                   SerializerFactory serializerFactory)
    throws Exception
{
    boolean isDebug = false;

    if (isDebugInvoke()) {
        isDebug = true;

        PrintWriter dbg = createDebugPrintWriter();
        HessianDebugInputStream dIs = new HessianDebugInputStream(is, dbg);
        dIs.startTop2();
        is = dIs;
        HessianDebugOutputStream dOs = new HessianDebugOutputStream(os, dbg);
        dOs.startTop2();
        os = dOs;
    }

    HessianInputFactory.HeaderType header = _inputFactory.readHeader(is);

    AbstractHessianInput in;
    AbstractHessianOutput out;

    switch (header) {
        case CALL_1_REPLY_1:
            in = _hessianFactory.createHessianInput(is);
            out = _hessianFactory.createHessianOutput(os);
            break;

        case CALL_1_REPLY_2:
            in = _hessianFactory.createHessianInput(is);
            out = _hessianFactory.createHessian2Output(os);
            break;

        case HESSIAN_2:
            in = _hessianFactory.createHessian2Input(is);
            in.readCall();
            out = _hessianFactory.createHessian2Output(os);
            break;

        default:
            throw new IllegalStateException(header + " is an unknown Hessian call");
    }

    if (serializerFactory != null) {
        in.setSerializerFactory(serializerFactory);
        out.setSerializerFactory(serializerFactory);
    }

    try {
        invoke(_service, in, out);
    } finally {
        in.close();
        out.close();

        if (isDebug)
            os.close();
    }
}