Java Code Examples for org.red5.io.object.Serializer#serialize()

The following examples show how to use org.red5.io.object.Serializer#serialize() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
Source File: MessageSerializationTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
private <T> T serializeAndDeserialize(T obj, Class<T> type) {
    IoBuffer data = IoBuffer.allocate(0);
    data.setAutoExpand(true);

    Output output = new Output(data);
    output.enforceAMF3();
    Serializer.serialize(output, obj);

    Input input = new Input(data.flip());
    input.enforceAMF3();
    Object result = Deserializer.deserialize(input, type);

    return type.cast(result);
}
 
Example 9
Source File: MetaService.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeMetaData(IMetaData<?, ?> metaData) {
    IMetaCue meta = (MetaCue<?, ?>) metaData;
    Output out = new Output(IoBuffer.allocate(1000));
    Serializer.serialize(out, "onCuePoint");
    Serializer.serialize(out, meta);
}
 
Example 10
Source File: MetaService.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * Injects metadata (Cue Points) into a tag
 * 
 * @param meta
 *            Metadata (cue points)
 * @param tag
 *            Tag
 * @return ITag tag New tag with injected metadata
 */
private static ITag injectMetaCue(IMetaCue meta, ITag tag) {
    // IMeta meta = (MetaCue) cue;
    Output out = new Output(IoBuffer.allocate(1000));
    Serializer.serialize(out, "onCuePoint");
    Serializer.serialize(out, meta);

    IoBuffer tmpBody = out.buf().flip();
    int tmpBodySize = out.buf().limit();
    int tmpPreviousTagSize = tag.getPreviousTagSize();
    int tmpTimestamp = getTimeInMilliseconds(meta);

    return new Tag(IoConstants.TYPE_METADATA, tmpTimestamp, tmpBodySize, tmpBody, tmpPreviousTagSize);
}
 
Example 11
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 12
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 13
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testArray() {
    log.debug("\ntestArray");
    String[] strArrIn = new String[] { "This", "Is", "An", "Array", "Of", "Strings" };
    Serializer.serialize(out, strArrIn);
    dumpOutput();
    Object[] objArrayOut = Deserializer.deserialize(in, Object[].class);
    for (int i = 0; i < strArrIn.length; i++) {
        assertEquals(strArrIn[i], objArrayOut[i]);
    }
    resetOutput();
}
 
Example 14
Source File: AMF3IOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnum() {
    log.debug("\n Testing Enum");
    Serializer.serialize(out, StreamAction.CONNECT);
    dumpOutput();
    Object object = Deserializer.deserialize(in, StreamAction.class);
    log.debug("Enums - {} {}", object.getClass().getName(), StreamAction.CONNECT.getClass().getName());
    assertEquals(object.getClass().getName(), StreamAction.CONNECT.getClass().getName());
    resetOutput();
}
 
Example 15
Source File: AbstractIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testString() {
    log.debug("\ntestString");
    String inStr = "hello world \u00A3";
    Serializer.serialize(out, inStr);
    dumpOutput();
    String outStr = Deserializer.deserialize(in, String.class);
    assertEquals(inStr, outStr);
    resetOutput();
}
 
Example 16
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 17
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 18
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 19
Source File: RTMPClientProtocolEncoder.java    From red5-client with Apache License 2.0 4 votes vote down vote up
/**
 * Encode notification event and fill given byte buffer.
 *
 * @param out
 *            Byte buffer to fill
 * @param command
 *            Notification event
 */
@Override
protected void encodeCommand(IoBuffer out, ICommand command) {
    log.debug("encodeCommand - command: {}", command);
    RTMPConnection conn = (RTMPConnection) Red5.getConnectionLocal();
    Output output = new org.red5.io.amf.Output(out);
    final IServiceCall call = command.getCall();
    final boolean isPending = (call.getStatus() == Call.STATUS_PENDING);
    log.debug("Call: {} pending: {}", call, isPending);
    if (!isPending) {
        log.debug("Call has been executed, send result");
        Serializer.serialize(output, call.isSuccess() ? "_result" : "_error");
    } else {
        log.debug("This is a pending call, send request");
        // for request we need to use AMF3 for client mode if the connection is AMF3
        if (conn.getEncoding() == Encoding.AMF3) {
            output = new org.red5.io.amf3.Output(out);
        }
        final String action = (call.getServiceName() == null) ? call.getServiceMethodName() : call.getServiceName() + '.' + call.getServiceMethodName();
        Serializer.serialize(output, action);
    }
    if (command instanceof Invoke) {
        Serializer.serialize(output, Integer.valueOf(command.getTransactionId()));
        Serializer.serialize(output, command.getConnectionParams());
    }
    if (call.getServiceName() == null && "connect".equals(call.getServiceMethodName())) {
        // response to initial connect, always use AMF0
        output = new org.red5.io.amf.Output(out);
    } else {
        if (conn.getEncoding() == Encoding.AMF3) {
            output = new org.red5.io.amf3.Output(out);
        } else {
            output = new org.red5.io.amf.Output(out);
        }
    }
    if (!isPending && (command instanceof Invoke)) {
        IPendingServiceCall pendingCall = (IPendingServiceCall) call;
        if (!call.isSuccess()) {
            log.debug("Call was not successful");
            StatusObject status = generateErrorResult(StatusCodes.NC_CALL_FAILED, call.getException());
            pendingCall.setResult(status);
        }
        Object res = pendingCall.getResult();
        log.debug("Writing result: {}", res);
        Serializer.serialize(output, res);
    } else {
        log.debug("Writing params");
        final Object[] args = call.getArguments();
        if (args != null) {
            for (Object element : args) {
                Serializer.serialize(output, element);
            }
        }
    }
    if (command.getData() != null) {
        out.setAutoExpand(true);
        out.put(command.getData());
    }
}
 
Example 20
Source File: StatusObjectService.java    From red5-server-common with Apache License 2.0 2 votes vote down vote up
/**
 * Serializes status object
 * 
 * @param out
 *            Byte buffer for output object
 * @param statusObject
 *            Status object to serialize
 */
public void serializeStatusObject(IoBuffer out, StatusObject statusObject) {
    Map<?, ?> statusMap = new BeanMap(statusObject);
    Output output = new Output(out);
    Serializer.serialize(output, statusMap);
}