org.red5.io.object.Serializer Java Examples

The following examples show how to use org.red5.io.object.Serializer. 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: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeObject(Map<Object, Object> map) {
    log.debug("writeObject: {}", map);
    writeAMF3();
    buf.put(AMF3.TYPE_OBJECT);
    if (hasReference(map)) {
        putInteger(getReferenceId(map) << 1);
        return;
    }
    storeReference(map);
    // we have an inline class that is not a reference, store the properties using key/value pairs
    int type = AMF3.TYPE_OBJECT_VALUE << 2 | 1 << 1 | 1;
    putInteger(type);
    // no classname
    putString("");
    // store key/value pairs
    amf3_mode += 1;
    for (Map.Entry<Object, Object> entry : map.entrySet()) {
        putString(entry.getKey().toString());
        Serializer.serialize(this, entry.getValue());
    }
    amf3_mode -= 1;
    // end of object marker
    putString("");
}
 
Example #2
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeRecordSet(RecordSet recordset) {
    if (!checkWriteReference(recordset)) {
        storeReference(recordset);
        // Write out start of object marker
        buf.put(AMF.TYPE_CLASS_OBJECT);
        putString("RecordSet");
        // Serialize
        Map<String, Object> info = recordset.serialize();
        // Write out serverInfo key
        putString("serverInfo");
        // Serialize
        Serializer.serialize(this, info);
        // Write out end of object marker
        buf.put(AMF.END_OF_OBJECT_SEQUENCE);
    }
}
 
Example #3
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeMap(Collection<?> array) {
    if (!checkWriteReference(array)) {
        storeReference(array);
        buf.put(AMF.TYPE_MIXED_ARRAY);
        buf.putInt(array.size() + 1);
        int idx = 0;
        for (Object item : array) {
            if (item != null) {
                putString(String.valueOf(idx++));
                Serializer.serialize(this, item);
            } else {
                idx++;
            }
        }
        putString("length");
        Serializer.serialize(this, array.size() + 1);
        buf.put(AMF.END_OF_OBJECT_SEQUENCE);
    }
}
 
Example #4
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected boolean serializeField(Class<?> objectClass, String keyName, Field field, Method getter) {
    // to prevent, NullPointerExceptions, get the element first and check if it's null
    Element element = getSerializeCache().get(objectClass);
    Map<String, Boolean> serializeMap = (element == null ? null : (Map<String, Boolean>) element.getObjectValue());
    if (serializeMap == null) {
        serializeMap = new HashMap<>();
        getSerializeCache().put(new Element(objectClass, serializeMap));
    }
    boolean serialize;
    if (getSerializeCache().isKeyInCache(keyName)) {
        serialize = serializeMap.get(keyName);
    } else {
        serialize = Serializer.serializeField(keyName, field, getter);
        serializeMap.put(keyName, serialize);
    }
    return serialize;
}
 
Example #5
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeArray(Object array) {
    if (array != null) {
        if (!checkWriteReference(array)) {
            storeReference(array);
            buf.put(AMF.TYPE_ARRAY);
            buf.putInt(Array.getLength(array));
            for (int i = 0; i < Array.getLength(array); i++) {
                Serializer.serialize(this, Array.get(array, i));
            }
        }
    } else {
        writeNull();
    }
}
 
Example #6
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeArray(Object[] array) {
    log.debug("writeArray - array: {}", array);
    if (array != null) {
        if (!checkWriteReference(array)) {
            storeReference(array);
            buf.put(AMF.TYPE_ARRAY);
            buf.putInt(array.length);
            for (Object item : array) {
                Serializer.serialize(this, item);
            }
        }
    } else {
        writeNull();
    }
}
 
Example #7
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeObject(Map<Object, Object> map) {
    if (!checkWriteReference(map)) {
        storeReference(map);
        buf.put(AMF.TYPE_OBJECT);
        boolean isBeanMap = (map instanceof BeanMap);
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            if (isBeanMap && "class".equals(entry.getKey())) {
                continue;
            }
            putString(entry.getKey().toString());
            Serializer.serialize(this, entry.getValue());
        }
        buf.put(AMF.END_OF_OBJECT_SEQUENCE);
    }
}
 
Example #8
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap() {
    log.debug("\ntestMap");
    Map<String, Object> mapIn = new HashMap<>();
    mapIn.put("testNumber", 34d); //numbers are stored as double
    mapIn.put("testString", "wicked awesome");
    mapIn.put("testBean", new SimpleJavaBean());
    mapIn.put("21.0.1", "version");
    mapIn.put("2.1", "version2");
    Serializer.serialize(out, mapIn);
    dumpOutput();
    Map<?, ?> mapOut = Deserializer.deserialize(in, Map.class);
    log.info("mapOut: {}", mapOut);
    assertNotNull(mapOut);
    assertEquals(mapIn.size(), mapOut.size());
    for (Map.Entry<String, Object> entry : mapIn.entrySet()) {
        String key = entry.getKey();
        Object iVal = entry.getValue();
        Object oVal = mapOut.get(key);
        assertNotNull(oVal);
        assertEquals(iVal, oVal);
    }
    resetOutput();
}
 
Example #9
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Write a Vector&lt;Object&gt;.
 * 
 * @param vector
 *            vector
 */
@Override
public void writeVectorObject(Vector<Object> vector) {
    log.debug("writeVectorObject: {}", vector);
    buf.put(AMF3.TYPE_VECTOR_OBJECT);
    if (hasReference(vector)) {
        putInteger(getReferenceId(vector) << 1);
        return;
    }
    storeReference(vector);
    putInteger(vector.size() << 1 | 1);
    putInteger(0);
    buf.put((byte) 0x01);
    for (Object v : vector) {
        Serializer.serialize(this, v);
    }
}
 
Example #10
Source File: AMF3IOTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testByteArray() {
    log.debug("\n Testing ByteArray");
    // just some ones and such
    ByteArray baIn = new ByteArray();
    baIn.writeBytes(new byte[] { (byte) 0, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x1, (byte) 0x42, (byte) 0x99 });
    Serializer.serialize(out, baIn);
    dumpOutput();
    ByteArray baOut = Deserializer.deserialize(in, ByteArray.class);
    assertNotNull(baOut);
    assertEquals(baIn.length(), baOut.length());
    for (int i = 0; i < baOut.length(); i++) {
        System.err.println("Byte: " + baOut.readByte());
    }
    resetOutput();
}
 
Example #11
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeMap(Collection<?> array) {
    writeAMF3();
    buf.put(AMF3.TYPE_ARRAY);
    if (hasReference(array)) {
        putInteger(getReferenceId(array) << 1);
        return;
    }
    storeReference(array);
    // TODO: we could optimize this by storing the first integer
    //       keys after the key-value pairs
    amf3_mode += 1;
    putInteger(1);
    int idx = 0;
    for (Object item : array) {
        if (item != null) {
            putString(String.valueOf(idx));
            Serializer.serialize(this, item);
        }
        idx++;
    }
    amf3_mode -= 1;
    putString("");
}
 
Example #12
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Use the general ARRAY type, writing the primitive array as an array of objects (the boxed primitives) instead.
 */
private void writePrimitiveArrayFallback(Object array) {
    writeAMF3();
    buf.put(AMF3.TYPE_ARRAY);
    if (hasReference(array)) {
        putInteger(getReferenceId(array) << 1);
        return;
    }
    storeReference(array);
    amf3_mode += 1;
    int count = Array.getLength(array);
    putInteger(count << 1 | 1);
    putString("");
    for (int i = 0; i < count; i++) {
        Serializer.serialize(this, Array.get(array, i));
    }
    amf3_mode -= 1;
}
 
Example #13
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeArray(Object[] array) {
    writeAMF3();
    buf.put(AMF3.TYPE_ARRAY);
    if (hasReference(array)) {
        putInteger(getReferenceId(array) << 1);
        return;
    }
    storeReference(array);
    amf3_mode += 1;
    int count = array.length;
    putInteger(count << 1 | 1);
    putString("");
    for (Object item : array) {
        Serializer.serialize(this, item);
    }
    amf3_mode -= 1;
}
 
Example #14
Source File: Output.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeArray(Collection<?> array) {
    writeAMF3();
    buf.put(AMF3.TYPE_ARRAY);
    if (hasReference(array)) {
        putInteger(getReferenceId(array) << 1);
        return;
    }
    storeReference(array);
    amf3_mode += 1;
    int count = array.size();
    putInteger(count << 1 | 1);
    putString("");
    for (Object item : array) {
        Serializer.serialize(this, item);
    }
    amf3_mode -= 1;
}
 
Example #15
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testLongString() {
    log.debug("\ntestLongString");
    byte[] rndStr = new byte[AMF.LONG_STRING_LENGTH];
    Arrays.fill(rndStr, (byte) 0x65);
    //Random rnd = new Random();
    //rnd.nextBytes(rndStr);
    String inStr = new String(rndStr, StandardCharsets.UTF_8);
    //String inStr = RandomStringUtils.random(AMF.LONG_STRING_LENGTH);
    //log.trace(inStr);
    Serializer.serialize(out, inStr);
    dumpOutput();
    String outStr = Deserializer.deserialize(in, String.class);
    assertEquals(inStr, outStr);
    resetOutput();
}
 
Example #16
Source File: StatusObject.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
public void serialize(Output output) {
    output.putString("level");
    output.writeString(getLevel());
    output.putString("code");
    output.writeString(getCode());
    output.putString("description");
    output.writeString(getDescription());
    if (application != null) {
        output.putString("application");
        Serializer.serialize(output, application);
    }
    if (additional != null) {
        // Add additional parameters
        for (Map.Entry<String, Object> entry : additional.entrySet()) {
            output.putString(entry.getKey());
            Serializer.serialize(output, entry.getValue());
        }
    }
}
 
Example #17
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testBoolean() {
    log.debug("\ntestBoolean");
    Serializer.serialize(out, Boolean.TRUE);
    dumpOutput();
    Boolean val = Deserializer.deserialize(in, Boolean.class);
    assertEquals(Boolean.TRUE, val);
    resetOutput();
    Serializer.serialize(out, Boolean.FALSE);
    dumpOutput();
    val = Deserializer.deserialize(in, Boolean.class);
    assertEquals(Boolean.FALSE, val);
    resetOutput();
}
 
Example #18
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testDate() {
    log.debug("\ntestDate");
    Date dateIn = new Date();
    Serializer.serialize(out, dateIn);
    dumpOutput();
    Date dateOut = Deserializer.deserialize(in, Date.class);
    assertEquals(dateIn, dateOut);
    resetOutput();
}
 
Example #19
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes" })
public void testJavaBean() {
    log.debug("\ntestJavaBean");
    TestJavaBean beanIn = new TestJavaBean();
    beanIn.setTestString("test string here");
    beanIn.setTestBoolean((System.currentTimeMillis() % 2 == 0) ? true : false);
    beanIn.setTestBooleanObject((System.currentTimeMillis() % 2 == 0) ? Boolean.TRUE : Boolean.FALSE);
    beanIn.setTestNumberObject(Integer.valueOf((int) System.currentTimeMillis() / 1000));
    Serializer.serialize(out, beanIn);
    dumpOutput();
    Object mapOrBean = Deserializer.deserialize(in, Object.class);
    assertEquals(beanIn.getClass().getName(), mapOrBean.getClass().getName());
    Map<?, ?> map = (mapOrBean instanceof Map) ? (Map<?, ?>) mapOrBean : new BeanMap(mapOrBean);
    Set<?> entrySet = map.entrySet();
    Iterator<?> it = entrySet.iterator();
    Map beanInMap = new BeanMap(beanIn);
    assertEquals(beanInMap.size(), map.size());
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        String propOut = (String) entry.getKey();
        Object valueOut = entry.getValue();
        assertTrue(beanInMap.containsKey(propOut));
        assertEquals(valueOut, beanInMap.get(propOut));
    }
    resetOutput();
}
 
Example #20
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testNull() {
    log.debug("\ntestNull");
    Serializer.serialize(out, null);
    dumpOutput();
    Object val = Deserializer.deserialize(in, Object.class);
    assertEquals(val, null);
    resetOutput();
}
 
Example #21
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircularReference() {
    log.debug("\ntestCircularReference");
    CircularRefBean beanIn = new CircularRefBean();
    beanIn.setRefToSelf(beanIn);
    Serializer.serialize(out, beanIn);

    dumpOutput();
    CircularRefBean beanOut = Deserializer.deserialize(in, CircularRefBean.class);
    assertNotNull(beanOut);
    assertEquals(beanOut, beanOut.getRefToSelf());
    assertEquals(beanIn.getNameOfBean(), beanOut.getNameOfBean());
    resetOutput();
}
 
Example #22
Source File: Output.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * Writes an arbitrary object to the output.
 *
 * @param object
 *            Object to write
 */
protected void writeArbitraryObject(Object object) {
    log.debug("writeObject");
    // If we need to serialize class information...
    Class<?> objectClass = object.getClass();
    if (!objectClass.isAnnotationPresent(Anonymous.class)) {
        // Write out start object marker for class name
        buf.put(AMF.TYPE_CLASS_OBJECT);
        putString(buf, Serializer.getClassName(objectClass));
    } else {
        // Write out start object marker without class name
        buf.put(AMF.TYPE_OBJECT);
    }
    // Iterate thru fields of an object to build "name-value" map from it
    for (Field field : objectClass.getFields()) {
        String fieldName = field.getName();
        log.debug("Field: {} class: {}", field, objectClass);
        // Check if the Field corresponding to the getter/setter pair is transient
        if (!serializeField(objectClass, fieldName, field, null)) {
            continue;
        }
        Object value;
        try {
            // Get field value
            value = field.get(object);
        } catch (IllegalAccessException err) {
            // Swallow on private and protected properties access exception
            continue;
        }
        // Write out prop name
        putString(buf, fieldName);
        // Write out
        Serializer.serialize(this, field, null, object, value);
    }
    // write out end of object marker
    buf.put(AMF.END_OF_OBJECT_SEQUENCE);
}
 
Example #23
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
public void testNumberLong() {
    log.debug("\ntestNumberLong");
    for (Number n : new Number[] { Long.MIN_VALUE, rnd.nextLong(), -666L, 0L, 666L, Long.MAX_VALUE }) {
        Serializer.serialize(out, n);
        dumpOutput();
        Number rn = Deserializer.deserialize(in, Number.class);
        assertEquals("Deserialized Long should be the same", n, rn.longValue());
        resetOutput();
    }
}
 
Example #24
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumberInteger() {
    log.debug("\ntestNumberInteger");
    for (Number n : new Number[] { Integer.MIN_VALUE, Integer.MAX_VALUE, 1024, rnd.nextInt(Integer.MAX_VALUE) }) {
        Serializer.serialize(out, n);
        dumpOutput();
        Number rn = Deserializer.deserialize(in, Number.class);
        assertEquals("Deserialized Integer should be the same", n, rn.intValue());
        resetOutput();
    }
}
 
Example #25
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumberFloat() {
    log.debug("\ntestNumberFloat");
    for (Number n : new Number[] { Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE, rnd.nextFloat(), 666.6666f }) {
        Serializer.serialize(out, n);
        dumpOutput();
        Number rn = Deserializer.deserialize(in, Number.class);
        assertEquals("Deserialized Float should be the same", (Float) n, (Float) rn.floatValue());
        resetOutput();
    }
}
 
Example #26
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumberDouble() {
    log.debug("\ntestNumberDouble");
    for (Number n : new Number[] { 1.056d, Double.MIN_VALUE, Double.MAX_VALUE, Double.valueOf(899.45678d), rnd.nextDouble() }) {
        Serializer.serialize(out, n);
        dumpOutput();
        Number rn = Deserializer.deserialize(in, Number.class);
        assertEquals("Deserialized number should be the same", n, rn.doubleValue());
        resetOutput();
    }
}
 
Example #27
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumber() {
    log.debug("\ntestNumber");
    int num = 1000;
    Serializer.serialize(out, Integer.valueOf(num));
    dumpOutput();
    Number n = Deserializer.deserialize(in, Number.class);
    assertEquals(n.intValue(), num);
    resetOutput();
}
 
Example #28
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testInteger() {
    log.debug("\ntestInteger");
    int num = 129;
    Serializer.serialize(out, Integer.valueOf(num));
    dumpOutput();
    int n = ((Number) Deserializer.deserialize(in, Number.class)).intValue();
    assertEquals(n, num);
    resetOutput();
}
 
Example #29
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeInteger() {
    log.debug("\ntestNegativeInteger");
    int num = -129;
    Serializer.serialize(out, Integer.valueOf(num));
    dumpOutput();
    int n = ((Number) Deserializer.deserialize(in, Number.class)).intValue();
    log.debug("Integer: {} {}", n, num);
    assertEquals(n, num);
    resetOutput();
}
 
Example #30
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({})
public void testSimpleReference() {
    log.debug("\ntestSimpleReference");
    Map<String, Object> mapIn = new HashMap<String, Object>();
    Object bean = new SimpleJavaBean();
    mapIn.put("thebean", bean);
    mapIn.put("thesamebeanagain", bean);
    // mapIn.put("thismap",mapIn);
    Serializer.serialize(out, mapIn);

    dumpOutput();
    Map<?, ?> mapOut = Deserializer.deserialize(in, Map.class);
    assertNotNull(mapOut);
    assertEquals(mapIn.size(), mapOut.size());

    Set<?> entrySet = mapOut.entrySet();
    Iterator<?> it = entrySet.iterator();
    while (it.hasNext()) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
        String propOut = (String) entry.getKey();
        SimpleJavaBean valueOut = (SimpleJavaBean) entry.getValue();
        assertNotNull("couldn't get output bean", valueOut);
        assertTrue(mapIn.containsKey(propOut));
        SimpleJavaBean valueIn = (SimpleJavaBean) mapIn.get(propOut);
        assertNotNull("couldn't get input bean", valueIn);
        assertEquals(valueOut.getNameOfBean(), valueIn.getNameOfBean());
    }
    resetOutput();
}