Java Code Examples for org.red5.io.object.Deserializer#deserialize()

The following examples show how to use org.red5.io.object.Deserializer#deserialize() . 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: AMF3IOTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testVectorIntInput() {
    log.debug("\n Testing Vector<int>");
    //0D090000000002000007D07FFFFFFF80000000
    byte[] v = new byte[] { (byte) 0x0D, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0xD0, (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00 };

    in = new Input(IoBuffer.wrap(v));
    List<Object> vectorOut = Deserializer.deserialize(in, null);
    //[2, 2000, 2147483647, -2147483648]
    assertNotNull(vectorOut);
    assertEquals(vectorOut.size(), 4);
    for (int i = 0; i < vectorOut.size(); i++) {
        System.err.println("Vector: " + vectorOut.get(i));
    }
    resetOutput();
}
 
Example 2
Source File: RTMPProtocolDecoder.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Sets incoming connection parameters and / or returns encoded parameters for use in a call.
 * 
 * @param in
 * @param notify
 * @param input
 * @return parameters array
 */
private Object[] handleParameters(IoBuffer in, Notify notify, Input input) {
    Object[] params = new Object[] {};
    List<Object> paramList = new ArrayList<>();
    final Object obj = Deserializer.deserialize(input, Object.class);
    if (obj instanceof Map) {
        // Before the actual parameters we sometimes (connect) get a map of parameters, this is usually null, but if set should be
        // passed to the connection object.
        @SuppressWarnings("unchecked")
        final Map<String, Object> connParams = (Map<String, Object>) obj;
        notify.setConnectionParams(connParams);
    } else if (obj != null) {
        paramList.add(obj);
    }
    while (in.hasRemaining()) {
        paramList.add(Deserializer.deserialize(input, Object.class));
    }
    params = paramList.toArray();
    if (log.isDebugEnabled()) {
        log.debug("Num params: {}", paramList.size());
        for (int i = 0; i < params.length; i++) {
            log.debug(" > {}: {}", i, params[i]);
        }
    }
    return params;
}
 
Example 3
Source File: AMFIOTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testAMF0Connect() {
    log.debug("\ntestAMF0Connect");
    IoBuffer data = IoBuffer.wrap(IOUtils.hexStringToByteArray(
            "020007636f6e6e656374003ff00000000000000300036170700200086f666c6144656d6f0008666c61736856657202000e4c4e582032302c302c302c323836000673776655726c020029687474703a2f2f6c6f63616c686f73743a353038302f64656d6f732f6f666c615f64656d6f2e7377660005746355726c02001972746d703a2f2f6c6f63616c686f73742f6f666c6144656d6f0004667061640100000c6361706162696c697469657300406de00000000000000b617564696f436f646563730040abee0000000000000b766964656f436f6465637300406f800000000000000d766964656f46756e6374696f6e003ff000000000000000077061676555726c02002a687474703a2f2f6c6f63616c686f73743a353038302f64656d6f732f6f666c615f64656d6f2e68746d6c000009"));
    Input in0 = new Input(data);
    // action string
    Assert.assertEquals(DataTypes.CORE_STRING, in0.readDataType());
    String action = in0.readString();
    Assert.assertEquals("connect", action);
    // invoke trasaction id
    log.trace("Before reading number type: {}", data.position());
    byte type = in0.readDataType();
    log.trace("After reading number type({}): {}", type, data.position());
    Assert.assertEquals(DataTypes.CORE_NUMBER, type);
    Number transactionId = in0.readNumber();
    System.out.printf("Number - i: %d d: %f%n", transactionId.intValue(), transactionId.doubleValue());
    Map<String, Object> obj1 = Deserializer.deserialize(in0, Map.class);
    assertNotNull("Connection parameters should be valid", obj1);
    log.debug("Parameters: {}", obj1.toString());
    assertEquals("Application does not match", "oflaDemo", obj1.get("app"));
}
 
Example 4
Source File: RemotingClient.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Decode response received from remoting server.
 * 
 * @param data
 *            Result data to decode
 * @return Object deserialized from byte buffer data
 */
private Object decodeResult(IoBuffer data) {
    log.debug("decodeResult - data limit: {}", (data != null ? data.limit() : 0));
    processHeaders(data);
    int count = data.getUnsignedShort();
    if (count != 1) {
        throw new RuntimeException("Expected exactly one result but got " + count);
    }
    Input input = new Input(data);
    String target = input.getString(); // expect "/onResult"
    log.debug("Target: {}", target);
    String nullString = input.getString(); // expect "null"
    log.debug("Null string: {}", nullString);
    // Read return value
    return Deserializer.deserialize(input, Object.class);
}
 
Example 5
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 6
Source File: SharedObject.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void deserialize(Input input) throws IOException {
    log.debug("deserialize");
    name = Deserializer.deserialize(input, String.class);
    log.trace("Name: {}", name);
    persistent = true;
    Map<String, Object> map = Deserializer.<Map> deserialize(input, Map.class);
    if (log.isTraceEnabled()) {
        log.trace("Attributes: {}", map);
    }
    super.setAttributes(map);
    ownerMessage.setName(name);
    ownerMessage.setPersistent(persistent);
}
 
Example 7
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();
}
 
Example 8
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 9
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 10
Source File: AMF3IOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
@Test
public void testVectorStringInput() {
    log.debug("\n Testing Vector<String>");
    //[Paul, ]
    byte[] v = new byte[] { (byte) 0x10, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06, (byte) 0x01 };
    //[Paul, Paul]
    byte[] v1 = new byte[] { (byte) 0x10, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06, (byte) 0x00 };
    //[Paul, Paul, Paul]
    byte[] v2 = new byte[] { (byte) 0x10, (byte) 0x07, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06, (byte) 0x00, (byte) 0x06, (byte) 0x00 };
    //[Paul, Tawnya]
    byte[] v3 = new byte[] { (byte) 0x10, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x06, (byte) 0x09, (byte) 0x50, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x06, (byte) 0x0d, (byte) 0x54, (byte) 0x61, (byte) 0x77, (byte) 0x6e, (byte) 0x79, (byte) 0x61 };

    //[1.0, 3.0, aaa, 5.0, aaa, aaa, 5.0, bb, bb]
    byte[] v4 = new byte[] { (byte) 0x10, (byte) 0x13, (byte) 0x00, (byte) 0x01, (byte) 0x04, (byte) 0x01, (byte) 0x04, (byte) 0x03, (byte) 0x06, (byte) 0x07, (byte) 0x61, (byte) 0x61, (byte) 0x61, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x05, (byte) 0x62, (byte) 0x62, (byte) 0x06, (byte) 0x02 };
    //[1.0, 3.0, aaa, [1, 2]]
    byte[] v5 = new byte[] { (byte) 0x10, (byte) 0x09, (byte) 0x00, (byte) 0x01, (byte) 0x04, (byte) 0x01, (byte) 0x04, (byte) 0x03, (byte) 0x06, (byte) 0x07, (byte) 0x61, (byte) 0x61, (byte) 0x61, (byte) 0x0d, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 };

    in = new Input(IoBuffer.wrap(v5));
    List<Object> vectorOut = Deserializer.deserialize(in, null);
    //[Paul, ]
    assertNotNull(vectorOut);
    //assertEquals(vectorOut.size(), 4);
    for (int i = 0; i < vectorOut.size(); i++) {
        System.err.println("Vector: " + vectorOut.get(i));
    }
    resetOutput();
}
 
Example 11
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 12
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 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: MetaService.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
// TODO need to fix
@Override
public MetaData<?, ?> readMetaData(IoBuffer buffer) {
    MetaData<?, ?> retMeta = new MetaData<String, Object>();
    Input input = new Input(buffer);
    String metaType = Deserializer.deserialize(input, String.class);
    log.debug("Metadata type: {}", metaType);
    Map<String, ?> m = Deserializer.deserialize(input, Map.class);
    retMeta.putAll(m);
    return retMeta;
}
 
Example 15
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 16
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 17
Source File: DSRemotingClient.java    From red5-client with Apache License 2.0 4 votes vote down vote up
/**
 * Process any headers sent in the response.
 * 
 * @param in
 *            Byte buffer with response data
 */
@Override
protected void processHeaders(IoBuffer in) {
    log.debug("RemotingClient processHeaders - buffer limit: {}", (in != null ? in.limit() : 0));
    int version = in.getUnsignedShort(); // skip
    log.debug("Version: {}", version);
    // the version by now, AMF3 is not yet implemented
    int count = in.getUnsignedShort();
    log.debug("Count: {}", count);
    Input input = new Input(in);
    for (int i = 0; i < count; i++) {
        String name = input.getString();
        log.debug("Name: {}", name);
        boolean required = (in.get() == 0x01);
        log.debug("Required: {}", required);
        Object value = null;
        int len = in.getInt();
        log.debug("Length: {}", len);
        // look for junk bytes ff,ff,ff,ff
        if (len == -1) {
            in.get(); //02
            len = in.getShort();
            log.debug("Corrected length: {}", len);
            value = input.readString(len);
        } else {
            value = Deserializer.deserialize(input, Object.class);
        }
        log.debug("Value: {}", value);
        // XXX: this is pretty much untested!!!
        if (RemotingHeader.APPEND_TO_GATEWAY_URL.equals(name)) {
            // Append string to gateway url
            appendToUrl = (String) value;
        } else if (RemotingHeader.REPLACE_GATEWAY_URL.equals(name)) {
            // Replace complete gateway url
            url = (String) value;
            // XXX: reset the <appendToUrl< here?
        } else if (RemotingHeader.PERSISTENT_HEADER.equals(name)) {
            // Send a new header with each following request
            if (value instanceof Map<?, ?>) {
                @SuppressWarnings("unchecked")
                Map<String, Object> valueMap = (Map<String, Object>) value;
                RemotingHeader header = new RemotingHeader((String) valueMap.get("name"), (Boolean) valueMap.get("mustUnderstand"), valueMap.get("data"));
                headers.put(header.getName(), header);
            } else {
                log.error("Expected Map but received {}", value);
            }
        } else {
            log.warn("Unsupported remoting header \"{}\" received with value \"{}\"", name, value);
        }
    }
}
 
Example 18
Source File: RemotingClient.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/**
 * Process any headers sent in the response.
 * 
 * @param in
 *            Byte buffer with response data
 */
protected void processHeaders(IoBuffer in) {
    log.debug("RemotingClient processHeaders - buffer limit: {}", (in != null ? in.limit() : 0));
    int version = in.getUnsignedShort(); // skip
    log.debug("Version: {}", version);
    // the version by now, AMF3 is not yet implemented
    int count = in.getUnsignedShort();
    log.debug("Count: {}", count);
    Input input = new Input(in);
    for (int i = 0; i < count; i++) {
        String name = input.getString();
        //String name = deserializer.deserialize(input, String.class);
        log.debug("Name: {}", name);
        boolean required = (in.get() == 0x01);
        log.debug("Required: {}", required);
        int len = in.getInt();
        log.debug("Length: {}", len);
        Object value = Deserializer.deserialize(input, Object.class);
        log.debug("Value: {}", value);

        // XXX: this is pretty much untested!!!
        if (RemotingHeader.APPEND_TO_GATEWAY_URL.equals(name)) {
            // Append string to gateway url
            appendToUrl = (String) value;
        } else if (RemotingHeader.REPLACE_GATEWAY_URL.equals(name)) {
            // Replace complete gateway url
            url = (String) value;
            // XXX: reset the <appendToUrl< here?
        } else if (RemotingHeader.PERSISTENT_HEADER.equals(name)) {
            // Send a new header with each following request
            if (value instanceof Map<?, ?>) {
                @SuppressWarnings("unchecked")
                Map<String, Object> valueMap = (Map<String, Object>) value;
                RemotingHeader header = new RemotingHeader((String) valueMap.get("name"), (Boolean) valueMap.get("mustUnderstand"), valueMap.get("data"));
                headers.put(header.name, header);
            } else {
                log.error("Expected Map but received {}", value);
            }
        } else {
            log.warn("Unsupported remoting header \"{}\" received with value \"{}\"", name, value);
        }
    }
}
 
Example 19
Source File: RTMPProtocolDecoder.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/**
 * Decode the 'action' for a supplied an Invoke.
 * 
 * @param encoding
 *            AMF encoding
 * @param in
 *            buffer
 * @param header
 *            data header
 * @return notify
 */
private Invoke decodeAction(Encoding encoding, IoBuffer in, Header header) {
    // for response, the action string and invokeId is always encoded as AMF0 we use the first byte to decide which encoding to use
    in.mark();
    byte tmp = in.get();
    in.reset();
    Input input;
    if (encoding == Encoding.AMF3 && tmp == AMF.TYPE_AMF3_OBJECT) {
        input = new org.red5.io.amf3.Input(in);
        ((org.red5.io.amf3.Input) input).enforceAMF3();
    } else {
        input = new org.red5.io.amf.Input(in);
    }
    // get the action
    String action = Deserializer.deserialize(input, String.class);
    if (action == null) {
        throw new RuntimeException("Action was null");
    }
    if (log.isTraceEnabled()) {
        log.trace("Action: {}", action);
    }
    // instance the invoke
    Invoke invoke = new Invoke();
    // set the transaction id
    invoke.setTransactionId(readTransactionId(input));
    // reset and decode parameters
    input.reset();
    // get / set the parameters if there any
    Object[] params = in.hasRemaining() ? handleParameters(in, invoke, input) : new Object[0];
    // determine service information
    final int dotIndex = action.lastIndexOf('.');
    String serviceName = (dotIndex == -1) ? null : action.substring(0, dotIndex);
    // pull off the prefixes since java doesn't allow this on a method name
    if (serviceName != null && (serviceName.startsWith("@") || serviceName.startsWith("|"))) {
        serviceName = serviceName.substring(1);
    }
    String serviceMethod = (dotIndex == -1) ? action : action.substring(dotIndex + 1, action.length());
    // pull off the prefixes since java doesnt allow this on a method name
    if (serviceMethod.startsWith("@") || serviceMethod.startsWith("|")) {
        serviceMethod = serviceMethod.substring(1);
    }
    // create the pending call for invoke
    PendingCall call = new PendingCall(serviceName, serviceMethod, params);
    invoke.setCall(call);
    return invoke;
}
 
Example 20
Source File: DSRemotingClient.java    From red5-client with Apache License 2.0 4 votes vote down vote up
/**
 * Decode response received from remoting server.
 * 
 * @param data
 *            Result data to decode
 * @return Object deserialized from byte buffer data
 */
private Object decodeResult(IoBuffer data) {
    log.debug("decodeResult - data limit: {}", (data != null ? data.limit() : 0));
    processHeaders(data);

    Input input = new Input(data);
    String target = null;

    byte b = data.get();
    //look for SOH
    if (b == 0) {
        log.debug("NUL: {}", b); //0
        log.debug("SOH: {}", data.get()); //1
    } else if (b == 1) {
        log.debug("SOH: {}", b); //1			
    }

    int targetUriLength = data.getShort();
    log.debug("targetUri length: {}", targetUriLength);
    target = input.readString(targetUriLength);

    log.debug("NUL: {}", data.get()); //0

    //should be junk bytes ff, ff, ff, ff
    int count = data.getInt();
    if (count == -1) {
        log.debug("DC1: {}", data.get()); //17
        count = 1;
    } else {
        data.position(data.position() - 4);
        count = data.getShort();
    }

    if (count != 1) {
        throw new RuntimeException("Expected exactly one result but got " + count);
    }

    String[] targetParts = target.split("[/]");
    if (targetParts.length > 1) {
        log.debug("Result sequence number: {}", targetParts[1]);
        target = targetParts[2];
    } else {
        target = target.substring(1);
    }
    log.debug("Target: {}", target);
    if ("onResult".equals(target)) {
        //read return value
        return input.readObject();
    } else if ("onStatus".equals(target)) {
        //read return value
        return input.readObject();
    }
    //read return value
    return Deserializer.deserialize(input, Object.class);
}