Java源码示例:com.caucho.hessian.io.AbstractHessianOutput

示例1
@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);
}
 
示例2
@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);
}
 
示例3
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());
        }
    }
}
 
示例4
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();
    }
 
示例5
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();
    }
}
 
示例6
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);
    }
}
 
示例7
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();
    }
}
 
示例8
@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);
    }
}
 
示例9
@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();
    }
}
 
示例10
@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();
    }
}
 
示例11
@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;
}
 
示例12
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new OffsetDateTimeHandle((OffsetDateTime) obj));
    }
}
 
示例13
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZonedDateTimeHandle((ZonedDateTime) obj));
    }
}
 
示例14
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new OptionalHandle((Optional<?>) obj));
    }
}
 
示例15
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new YearMonthHandle((YearMonth) obj));
    }
}
 
示例16
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new InstantHandle((Instant) obj));
    }
}
 
示例17
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new OffsetTimeHandle((OffsetTime) obj));
    }
}
 
示例18
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new LocalDateTimeHandle((LocalDateTime) obj));
    }
}
 
示例19
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new DurationHandle((Duration) obj));
    }
}
 
示例20
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZoneIdHandle((ZoneId) obj));
    }
}
 
示例21
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new MonthDayHandle((MonthDay) obj));
    }
}
 
示例22
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new LocalTimeHandle((LocalTime) obj));
    }
}
 
示例23
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new PeriodHandle((Period) obj));
    }
}
 
示例24
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZoneOffsetHandle((ZoneOffset) obj));
    }
}
 
示例25
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new LocalDateHandle((LocalDate) obj));
    }
}
 
示例26
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new YearHandle((Year) obj));
    }
}
 
示例27
@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());
        }
    }
}
 
示例28
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZoneIdHandle(obj));
    }
}
 
示例29
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);
    }
}
 
示例30
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);
}