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

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#getString() . 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: MessageChannelCodecFilter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private Map<String, String> decodeProperties ( final IoSession session, final IoBuffer data ) throws CharacterCodingException
{
    final int count = data.getInt ();

    final Map<String, String> result = new HashMap<String, String> ( count );

    final CharsetDecoder decoder = getCharsetDecoder ( session );

    for ( int i = 0; i < count; i++ )
    {
        final String key = data.getString ( decoder );
        final String value = data.getString ( decoder );
        result.put ( key, value );
    }

    return result;
}
 
Example 2
Source File: AbstractProxyIoHandler.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public void messageSent(IoSession session, Object message) throws Exception
{
	logger.debug("messageSent: {}", getClass().getSimpleName());
	IoBuffer rb = (IoBuffer) message;
	String msg = rb.getString(CHARSET.newDecoder());
	logger.debug("Message content: {}", msg);
}
 
Example 3
Source File: FixedLengthStringAccessor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String get ( final IoBuffer data, final int index )
{
    try
    {
        return data.getString ( this.length, this.charset.newDecoder () );
    }
    catch ( final CharacterCodingException e )
    {
        throw new RuntimeException ( e );
    }
}
 
Example 4
Source File: MessageUtil.java    From CXTouch with GNU General Public License v3.0 4 votes vote down vote up
public static String readString(IoBuffer buffer, LengthType lenType, CharsetDecoder decoder) throws NoArrayException {
    int length;
    int remain = buffer.remaining();
    buffer.mark();
    if (lenType == LengthType.INT) {
        if (remain < 4) {
            buffer.reset();
            throw new NoArrayException();
        }
        length = buffer.getInt();
    } else if(lenType == LengthType.SHORT) {
        if (remain < 2) {
            buffer.reset();
            throw new NoArrayException();
        }
        length = buffer.getShort();
    } else if (lenType == LengthType.BYTE) {
        if (remain < 1) {
            buffer.reset();
            throw new NoArrayException();
        }
        length = buffer.get();
    } else {
        buffer.reset();
        throw new IllegalArgumentException("Illegal type:" + lenType);
    }

    if (length < 0) {
        return null;
    } if (length == 0) {
        return "";
    } else {
        if (buffer.remaining() < length) {
            buffer.reset();
            throw new NoArrayException();
        }
        try {
            return buffer.getString(length, decoder);
        } catch (CharacterCodingException e) {
            throw new RuntimeException("Reading string failed:" + e.getMessage(), e);
        }
    }
}
 
Example 5
Source File: MyDataDecoder.java    From QuickerAndroid with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 返回值含义:
 * 1、当内容刚好时,返回false,告知父类接收下一批内容
 * 2、内容不够时需要下一批发过来的内容,此时返回false,这样父类 CumulativeProtocolDecoder
 * 会将内容放进IoSession中,等下次来数据后就自动拼装再交给本类的doDecode
 * 3、当内容多时,返回true,因为需要再将本批数据进行读取,父类会将剩余的数据再次推送本类的doDecode方法
 */
@Override
public boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
        throws Exception {
    Log.d(TAG, "解码消息... len = " + in.remaining());

    /**
     * 假定消息格式为:消息头(int类型:表示消息体的长度、short类型:表示事件号)+消息体
     */
    if (in.remaining() < 12)
    {
        return false;
    }

        //以便后继的reset操作能恢复position位置
        in.mark();

        int headFlag = in.getInt();
        int msgType = in.getInt();
        int msgLength = in.getInt();

        if (  in.remaining() >= (msgLength + 4) ) {
            String msgJson = in.getString(msgLength, Charset.forName("UTF-8").newDecoder());
            int end = in.getInt();

            MessageBase msg = deserializeMsg(msgType, msgJson);
            out.write(msg);

            if (in.hasRemaining()) {
                return true;
            }else {
                return false;
            }
        }else {
            in.reset();

            // 消息不完整
            return false;
        }


}
 
Example 6
Source File: MessageChannelCodecFilter.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private CloseMessage decodeCloseFrame ( final IoSession session, final IoBuffer data ) throws CharacterCodingException
{
    return new CloseMessage ( data.getString ( getCharsetDecoder ( session ) ), data.getInt () );
}