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

The following examples show how to use com.caucho.hessian.io.AbstractHessianOutput#writeString() . 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: 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 2
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 3
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 4
Source File: LocalDateTimeSerializer.java    From jvm-sandbox-repeater 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 {
        Class cl = obj.getClass();

        if (out.addRef(obj)) {
            return;
        }
        // ref 返回-2 便是开始写Map
        int ref = out.writeObjectBegin(cl.getName());

        if (ref < -1) {
            out.writeString("value");
            out.writeUTCDate(((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli());
            out.writeMapEnd();
        } else {
            if (ref == -1) {
                out.writeInt(1);
                out.writeString("value");
                out.writeObjectBegin(cl.getName());
            }
            out.writeUTCDate(((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli());
        }
    }
}
 
Example 5
Source File: GenericObjectSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
private void writeDefinition(ObjectDefinition definition, AbstractHessianOutput out)
    throws IOException {
    String[] _fieldNames = definition.getFieldNames();

    out.writeClassFieldLength(_fieldNames.length);

    for (int i = 0; i < _fieldNames.length; i++) {
        String fieldName = _fieldNames[i];

        out.writeString(fieldName);
    }
}
 
Example 6
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 7
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 8
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 9
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 10
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 11
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object1_2() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  out.writeString("threedr3am");
  out.close();
  return bos.toByteArray();
}
 
Example 12
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 13
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 14
Source File: GenericObjectSerializer.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
void serialize(AbstractHessianOutput out, GenericObject obj, String field)
    throws IOException {
    String value = (String) obj.getField(field);
    out.writeString(value);
}