Java Code Examples for org.apache.mina.core.buffer.IoBuffer#putInt()

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#putInt() . 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: DataCodecEncoder.java    From oim-fx with MIT License 6 votes vote down vote up
@Override
public void encode(IoSession session, Object object, ProtocolEncoderOutput out) throws Exception {
	try {
		if (null != object) {
			String xml = object.toString();
			byte[] xmlByte = xml.getBytes(charset);
			IoBuffer io = IoBuffer.allocate(xmlByte.length + 4).setAutoExpand(true);
			io.putInt(xmlByte.length);
			io.put(xmlByte);
			io.flip();
			out.write(io);
		}
	} catch (Exception e) {
		logger.error("",e);
		e.printStackTrace();
	}
}
 
Example 2
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void encodeIntCollection ( final IoBuffer buffer, final byte fieldNumber, final Collection<Integer> data ) throws Exception
{
    buffer.put ( fieldNumber );
    if ( data != null )
    {
        buffer.put ( TYPE_INT_LIST );
        buffer.putInt ( data.size () );
        for ( final Integer entry : data )
        {
            buffer.putInt ( entry );
        }
    }
    else
    {
        buffer.put ( TYPE_NULL );
    }
}
 
Example 3
Source File: MyDataEncoder.java    From QuickerAndroid with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void encode(IoSession session, Object message,
                   ProtocolEncoderOutput out) throws Exception {
    if (message instanceof MessageBase) {
        Gson gson = new Gson();
        String msgJson = gson.toJson(message);
        byte[] msgBytes = msgJson.getBytes("utf-8");

        IoBuffer buffer = IoBuffer.allocate(msgBytes.length + 16);
        buffer.putInt(0xFFFFFFFF);
        buffer.putInt(((MessageBase) message).getMessageType());
        buffer.putInt(msgBytes.length);
        buffer.put(msgBytes);
        buffer.putInt(0);
        buffer.flip();

        out.write(buffer);
    }

}
 
Example 4
Source File: Request.java    From TestClient with Apache License 2.0 6 votes vote down vote up
public IoBuffer entireMsg() {
	
		byte[] body = output.toByteArray();
		if(body==null){
			body = new byte[0];
		}
		/* 标志 byte 长度short */
		int length = MsgProtocol.flagSize+MsgProtocol.lengthSize+MsgProtocol.msgCodeSize+ body.length;
		IoBuffer buf = IoBuffer.allocate(length);
		buf.put(MsgProtocol.defaultFlag);//flag
		buf.putInt(body.length+MsgProtocol.msgCodeSize);//lengh
		buf.putInt(msgCode);
		buf.put(body);
		buf.flip();
		return buf;
}
 
Example 5
Source File: MessageChannelCodecFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private Frame encodeCloseMessage ( final IoSession session, final CloseMessage message ) throws CharacterCodingException
{
    final IoBuffer data = IoBuffer.allocate ( 0 );
    data.setAutoExpand ( true );
    data.putString ( message.getMessage (), getCharsetEncoder ( session ) );
    data.put ( (byte)0x00 );
    data.putInt ( message.getCode () );

    data.flip ();

    return new Frame ( FrameType.CLOSE, data );
}
 
Example 6
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void beginWriteStructureList ( final IoBuffer buffer, final byte fieldNumber, final Collection<?> values ) throws Exception
{
    buffer.put ( fieldNumber );
    if ( values != null )
    {
        buffer.put ( TYPE_STRUCTURE_LIST );
        buffer.putInt ( values.size () );
    }
    else
    {
        buffer.put ( TYPE_NULL );
    }
}
 
Example 7
Source File: ArduinoCodec.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void encodeHeader ( final IoBuffer data, final CommonMessage message )
{
    data.putShort ( (short)1202 );
    data.put ( (byte)0x01 );
    data.putInt ( message.getSequence () );
    data.put ( message.getCommandCode ().getCommandCode () );
}
 
Example 8
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void encodePrimitiveInt ( final IoBuffer buffer, final byte fieldNumber, final int data ) throws Exception
{
    buffer.put ( fieldNumber );
    buffer.put ( TYPE_INT );
    buffer.putInt ( data );
}
 
Example 9
Source File: IOUtilsTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteReverseInt() {
    final int ok = -771489792; // reversed int of 1234
    int source = 1234;
    IoBuffer out = IoBuffer.allocate(4);
    IOUtils.writeReverseInt(out, source);
    out.flip();
    int result = out.getInt();
    System.out.printf("Results - source: %d result: %d\n", source, result);
    out.flip();
    assertTrue(ok == result);
    // old method
    out.mark();
    byte[] bytes = new byte[4];
    IoBuffer rev = IoBuffer.allocate(4);
    rev.putInt(source);
    rev.flip();
    bytes[3] = rev.get();
    bytes[2] = rev.get();
    bytes[1] = rev.get();
    bytes[0] = rev.get();
    out.put(bytes);
    rev.free();
    rev = null;
    out.reset();
    result = out.getInt();
    System.out.printf("Result #1 - result: %d\n", result);
    out.flip();
    assertTrue(ok == result);
    // optimized
    out.mark();
    out.putInt((int) ((source & 0xFF) << 24 | ((source >> 8) & 0x00FF) << 16 | ((source >>> 16) & 0x000000FF) << 8 | ((source >>> 24) & 0x000000FF)));
    out.reset();
    result = out.getInt();
    System.out.printf("Result #2 - result: %d\n", result);
    out.flip();
    out.free();
    assertTrue(ok == result);
}
 
Example 10
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected void inlineEncodeProperties ( final IoBuffer buffer, final Map<String, String> data ) throws Exception
{
    buffer.putInt ( data.size () );
    for ( final Map.Entry<String, String> entry : data.entrySet () )
    {
        buffer.putPrefixedString ( entry.getKey (), STRING_PREFIX_LEN, this.encoder );
        buffer.putPrefixedString ( entry.getValue (), STRING_PREFIX_LEN, this.encoder );
    }
}
 
Example 11
Source File: IOUtilsTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadReverseInt() {
    final int ok = -771489792;
    int source = 1234;
    IoBuffer in = IoBuffer.allocate(4);
    in.putInt(source);
    in.flip();
    int result = IOUtils.readReverseInt(in);
    System.out.printf("Results - source: %d result: %d\n", source, result);
    assertTrue(ok == result);
    // older method
    in.flip();
    byte[] bytes = new byte[4];
    in.get(bytes);
    int value = 0;
    value += bytes[3] * 256 * 256 * 256;
    value += bytes[2] * 256 * 256;
    value += bytes[1] * 256;
    value += bytes[0];
    System.out.printf("Results #1 - result: %d\n", value);
    assertTrue(ok == value);
    // optimized 
    in.flip();
    value = in.getInt();
    value = ((value & 0xFF) << 24 | ((value >> 8) & 0x00FF) << 16 | ((value >>> 16) & 0x000000FF) << 8 | ((value >>> 24) & 0x000000FF));
    System.out.printf("Results #2 - result: %d\n", value);
    assertTrue(ok == value);
}
 
Example 12
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void inlineEncodeVariant ( final IoBuffer buffer, final Variant variant ) throws Exception
{
    final VariantType type = variant.getType ();
    buffer.putEnum ( type );

    switch ( type )
    {
        case BOOLEAN:
            buffer.put ( variant.asBoolean () ? (byte)0xFF : (byte)0x00 );
            break;
        case DOUBLE:
            buffer.putDouble ( variant.asDouble () );
            break;
        case INT32:
            buffer.putInt ( variant.asInteger () );
            break;
        case INT64:
            buffer.putLong ( variant.asLong () );
            break;
        case STRING:
            buffer.putPrefixedString ( variant.asString (), STRING_PREFIX_LEN, this.encoder );
            break;
        case NULL:
            break;
        case UNKNOWN:
            break;
    }
}
 
Example 13
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected void inlineEncodeVariantMap ( final IoBuffer buffer, final Map<String, Variant> data ) throws Exception
{
    buffer.putInt ( data.size () );
    for ( final Map.Entry<String, Variant> entry : data.entrySet () )
    {
        buffer.putPrefixedString ( entry.getKey (), STRING_PREFIX_LEN, this.encoder );
        inlineEncodeVariant ( buffer, entry.getValue () );
    }
}
 
Example 14
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void encodeInt ( final IoBuffer buffer, final byte fieldNumber, final Integer data ) throws Exception
{
    buffer.put ( fieldNumber );
    if ( data != null )
    {
        buffer.put ( TYPE_INT );
        buffer.putInt ( data );
    }
    else
    {
        buffer.put ( TYPE_NULL );
    }
}
 
Example 15
Source File: StartBrowse.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public IoBuffer encodeMessage ( final BinaryContext context, final Object objectMessage ) throws Exception
{
    final IoBuffer data = IoBuffer.allocate ( 5 );
    data.putInt ( MESSAGE_CODE );
    data.put ( (byte)0 ); // number of fields 

    data.flip ();
    return data;
}
 
Example 16
Source File: RTMPProtocolEncoder.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public IoBuffer encodePing(Ping ping) {
    int len;
    short type = ping.getEventType();
    switch (type) {
        case Ping.CLIENT_BUFFER:
            len = 10;
            break;
        case Ping.PONG_SWF_VERIFY:
            len = 44;
            break;
        default:
            len = 6;
    }
    final IoBuffer out = IoBuffer.allocate(len);
    out.putShort(type);
    switch (type) {
        case Ping.STREAM_BEGIN:
        case Ping.STREAM_PLAYBUFFER_CLEAR:
        case Ping.STREAM_DRY:
        case Ping.RECORDED_STREAM:
        case Ping.PING_CLIENT:
        case Ping.PONG_SERVER:
        case Ping.BUFFER_EMPTY:
        case Ping.BUFFER_FULL:
            out.putInt(ping.getValue2().intValue());
            break;
        case Ping.CLIENT_BUFFER:
            if (ping instanceof SetBuffer) {
                SetBuffer setBuffer = (SetBuffer) ping;
                out.putInt(setBuffer.getStreamId());
                out.putInt(setBuffer.getBufferLength());
            } else {
                out.putInt(ping.getValue2().intValue());
                out.putInt(ping.getValue3());
            }
            break;
        case Ping.PING_SWF_VERIFY:
            break;
        case Ping.PONG_SWF_VERIFY:
            out.put(((SWFResponse) ping).getBytes());
            break;
    }
    // this may not be needed anymore
    if (ping.getValue4() != Ping.UNDEFINED) {
        out.putInt(ping.getValue4());
    }
    return out;
}
 
Example 17
Source File: ProtocolEncoderImpl.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private void encodeVariant ( final IoSession session, final IoBuffer data, final Variant value ) throws ProtocolCodecException
{
    if ( value == null )
    {
        data.put ( DataType.DEAD.getDataType () ); // dead
    }
    else if ( value.isNull () )
    {
        data.put ( DataType.NULL.getDataType () );
    }
    else if ( value.isBoolean () )
    {
        data.put ( DataType.BOOLEAN.getDataType () );
        data.put ( (byte) ( value.asBoolean () ? 0xFF : 0x00 ) );
    }
    else if ( value.isInteger () )
    {
        data.put ( DataType.INT32.getDataType () );
        data.putInt ( value.asInteger ( null ) );
    }
    else if ( value.isLong () )
    {
        data.put ( DataType.INT64.getDataType () );
        data.putLong ( value.asLong ( null ) );
    }
    else if ( value.isDouble () )
    {
        data.put ( DataType.DOUBLE.getDataType () );
        data.putDouble ( value.asDouble ( null ) );
    }
    else if ( value.isString () )
    {
        data.put ( DataType.STRING.getDataType () );
        try
        {
            data.putPrefixedString ( value.asString ( null ), Sessions.getCharsetEncoder ( session ) );
        }
        catch ( final CharacterCodingException e )
        {
            throw new ProtocolCodecException ( e );
        }
    }
}
 
Example 18
Source File: Int32Accessor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void put ( final IoBuffer data, final Integer value )
{
    data.putInt ( value );
}
 
Example 19
Source File: RTMPProtocolEncoder.java    From red5-server-common with Apache License 2.0 3 votes vote down vote up
/**
 * Encode client-side bandwidth event.
 *
 * @param clientBW
 *            Client-side bandwidth event
 * @return Encoded event data
 */
private IoBuffer encodeClientBW(ClientBW clientBW) {
    final IoBuffer out = IoBuffer.allocate(5);
    out.putInt(clientBW.getBandwidth());
    out.put(clientBW.getLimitType());
    return out;
}
 
Example 20
Source File: IOUtils.java    From red5-io with Apache License 2.0 2 votes vote down vote up
/**
 * Writes extended medium integer (equivalent to a regular integer whose most significant byte has been moved to its end, past its least significant byte)
 * 
 * @param out
 *            Output buffer
 * @param value
 *            Integer to write
 */
public final static void writeExtendedMediumInt(IoBuffer out, int value) {
    value = ((value & 0xff000000) >> 24) | (value << 8);
    out.putInt(value);
}