com.caucho.hessian.io.AbstractHessianOutput Java Examples

The following examples show how to use com.caucho.hessian.io.AbstractHessianOutput. 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: GenericArraySerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }
    GenericArray genericArray;
    if (obj instanceof GenericObject[]) {
        genericArray = new GenericArray(Object.class.getName());
        genericArray.setObjects((GenericObject[]) obj);
    } else {
        genericArray = (GenericArray) obj;
    }

    boolean hasEnd = out.writeListBegin(genericArray.getLength(), genericArray.getType());

    for (int i = 0; i < genericArray.getLength(); i++) {
        out.writeObject(genericArray.get(i));
    }

    if (hasEnd) {
        out.writeListEnd();
    }
}
 
Example #2
Source File: Java8TimeSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
        return;
    }

    T handle = null;
    try {
        Constructor<T> constructor = handleType.getConstructor(Object.class);
        handle = constructor.newInstance(obj);
    } catch (Exception e) {
        throw new RuntimeException("the class :" + handleType.getName() + " construct failed:" + e.getMessage(), e);
    }

    out.writeObject(handle);
}
 
Example #3
Source File: GenericClassSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    GenericClass genericClass = (GenericClass) obj;

    if (genericClass == null) {
        out.writeNull();
    } else if (out.addRef(obj)) {
        return;
    } else {
        int ref = out.writeObjectBegin("java.lang.Class");

        if (ref < -1) {
            out.writeString("name");
            out.writeString(genericClass.getClazzName());
            out.writeMapEnd();
        } else {
            if (ref == -1) {
                out.writeInt(1);
                out.writeString("name");
                out.writeObjectBegin("java.lang.Class");
            }

            out.writeString(genericClass.getClazzName());
        }
    }
}
 
Example #4
Source File: Java8TimeSerializer.java    From jvm-sandbox-repeater with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out)
        throws IOException {
    if (obj == null) {
        out.writeNull();
        return;
    }
    T handle;
    try {
        Constructor<T> constructor = this.handleType.getConstructor(Object.class);
        handle = constructor.newInstance(obj);
    } catch (Exception e) {
        throw new RuntimeException("the class :" + this.handleType.getName() + " construct failed:" + e.getMessage(), e);
    }
    out.writeObject(handle);
}
 
Example #5
Source File: GenericMapSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {

        if (out.addRef(obj)) {
            return;
        }

        GenericMap genericMap = (GenericMap) obj;

        out.writeMapBegin(genericMap.getType());

        Iterator iter = genericMap.getMap().entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();

            out.writeObject(entry.getKey());
            out.writeObject(entry.getValue());
        }
        out.writeMapEnd();
    }
 
Example #6
Source File: GenericCollectionSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }

    GenericCollection genericCollection = (GenericCollection) obj;
    Collection collection = genericCollection.getCollection();

    boolean hasEnd;
    hasEnd = out.writeListBegin(collection.size(), genericCollection.getType());

    Iterator iter = collection.iterator();
    while (iter.hasNext()) {
        Object value = iter.next();
        out.writeObject(value);
    }

    if (hasEnd) {
        out.writeListEnd();
    }
}
 
Example #7
Source File: GenericObjectSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
private void writeInstance(GenericObject obj, ObjectDefinition definition,
                           AbstractHessianOutput out) throws IOException {
    String[] _fieldNames = definition.getFieldNames();

    FieldSerializer[] _fieldSerializers = new FieldSerializer[_fieldNames.length];
    for (int i = 0; i < _fieldNames.length; i++) {
        Object field = obj.getField(_fieldNames[i]);
        Class<?> fieldType = field == null ? null : field.getClass();
        _fieldSerializers[i] = getFieldSerializer(fieldType);
    }

    for (int i = 0; i < _fieldNames.length; i++) {
        String fieldName = _fieldNames[i];
        _fieldSerializers[i].serialize(out, obj, fieldName);
    }
}
 
Example #8
Source File: SoftReferenceSerializer.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }
    @SuppressWarnings("unchecked")
    SoftReference<Object> data = (SoftReference<Object>) obj;

    int refV = out.writeObjectBegin(SoftReference.class.getName());

    if (refV == -1) {
        out.writeInt(1);
        out.writeString("ref");
        out.writeObjectBegin(SoftReference.class.getName());
    }
    if (data != null) {
        Object ref = data.get();
        if (null != ref) {
            out.writeObject(ref);
        } else {
            out.writeNull();
        }
    } else {
        out.writeNull();
    }
}
 
Example #9
Source File: WeakReferenceSerializer.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }
    @SuppressWarnings("unchecked")
    WeakReference<Object> data = (WeakReference<Object>) obj;

    int refV = out.writeObjectBegin(WeakReference.class.getName());

    if (refV == -1) {
        out.writeInt(1);
        out.writeString("ref");
        out.writeObjectBegin(WeakReference.class.getName());
    }
    if (data != null) {
        Object ref = data.get();
        if (null != ref) {
            out.writeObject(ref);
        } else {
            out.writeNull();
        }
    } else {
        out.writeNull();
    }
}
 
Example #10
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 #11
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }

    Class cl = obj.getClass();
    int ref = out.writeObjectBegin(cl.getName());

    if (ref < -1) {
        writeObject10(obj, out);
    } else {
        if (ref == -1) {
            writeDefinition20(out);
            out.writeObjectBegin(cl.getName());
        }

        writeInstance(obj, out);
    }
}
 
Example #12
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);
  out.writeObject(new A());
  out.close();
  return bos.toByteArray();
}
 
Example #13
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);
  out.writeObject(new B(new A()));
  out.close();
  return bos.toByteArray();
}
 
Example #14
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 #15
Source File: ZonedDateTimeSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZonedDateTimeHandle((ZonedDateTime) obj));
    }
}
 
Example #16
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);
  Map<String, String> map = new HashMap<>();
  map.put("test", "test");
  map.put("foo", "foo");
  out.writeObject(map);
  out.close();
  return bos.toByteArray();
}
 
Example #17
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 #18
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);
  Map<String, String> map = new HashMap<>();
  map.put("test", "test");
  out.writeObject(map);
  out.close();
  return bos.toByteArray();
}
 
Example #19
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 #20
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 #21
Source File: TranscribableSerialiser.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, final AbstractHessianOutput out) throws IOException {
	try {
		if (out.addRef(obj)) {
			return;
		}

           Transcribable transcribable = (Transcribable) obj;
           ServiceVersion serviceVersion = transcribable.getServiceVersion();
		Class clazz = obj.getClass();

           String classNameToWrite = clazz.getName();
           // if we're talking to an older version we need to migrate vMajor to vMajor_Minor
           if (!transcriptionParams.contains(TranscribableParams.MajorOnlyPackageNaming)) {
               // look for vMajor
               classNameToWrite = ClassnameCompatibilityMapper.toMajorMinorPackaging(clazz, serviceVersion);
           }
		int ref = out.writeObjectBegin(classNameToWrite);

           // true if the object has already been defined
		if (ref >= 0) {
			transcribe(out,transcribable, client);
		}
		else  {
			Parameter[] parameters =  transcribable.getParameters();
			out.writeInt(parameters.length);
			for (int i=0; i < parameters.length; i++) {
				out.writeString(parameters[i].getName());
			}
			out.writeObjectBegin(classNameToWrite);
			transcribe(out,transcribable, client);
		}
	}
	catch (Exception e) {
		throw new IOException(e);
	}


}
 
Example #22
Source File: TranscribableEnumSerializer.java    From cougar with Apache License 2.0 5 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }

    Class cl = obj.getClass();

    String name = null;
    try {
        name = (String) _name.invoke(obj, (Object[]) null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    // if we're talking to an older version we need to migrate vMajor to vMajor_Minor
    String className = cl.getName();
    if (!transcriptionParams.contains(TranscribableParams.MajorOnlyPackageNaming)) {
        className = ClassnameCompatibilityMapper.toMajorMinorPackaging(cl, _serviceVersion);
    }
    int ref = out.writeObjectBegin(className);

    if (ref < -1) {
        out.writeString("name");
        out.writeString(name);
        out.writeMapEnd();
    }
    else {
        if (ref == -1) {
            out.writeClassFieldLength(1);
            out.writeString("name");
            out.writeObjectBegin(className);
        }

        out.writeString(name);
    }
}
 
Example #23
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 #24
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
private void writeDefinition20(AbstractHessianOutput out) throws IOException {
    out.writeClassFieldLength(_fields.length + 1);

    for (int i = 0; i < _fields.length; i++) {
        Field field = _fields[i];
        out.writeString(field.getName());
    }
    out.writeString("_list_content");
}
 
Example #25
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);
  out.writeString("test");
  out.close();
  return bos.toByteArray();
}
 
Example #26
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 #27
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
protected void writeObject10(Object obj, AbstractHessianOutput out) throws IOException {
    for (int i = 0; i < _fields.length; i++) {
        Field field = _fields[i];
        out.writeString(field.getName());
        _fieldSerializers[i].serialize(out, obj, field);
    }

    // writeList 内容
    out.writeString("_list_content");
    ArrayList list = new ArrayList((Collection) obj);
    out.writeObject(list);
}
 
Example #28
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field) throws IOException {
    String value = null;

    try {
        value = (String) field.get(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeString(value);
}
 
Example #29
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field) throws IOException {
    Object value = null;

    try {
        value = field.get(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeObject(value);
}
 
Example #30
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public void writeInstance(Object obj, AbstractHessianOutput out) throws IOException {
    for (int i = 0; i < _fields.length; i++) {
        Field field = _fields[i];
        _fieldSerializers[i].serialize(out, obj, field);
    }

    ArrayList list = new ArrayList((Collection) obj);
    out.writeObject(list);
}