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

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#getPrefixedString() . 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: PrefixedStringDecoder.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    if (in.prefixedDataAvailable(prefixLength, maxDataLength)) {
        String msg = in.getPrefixedString(prefixLength, charset.newDecoder());
        out.write(msg);
        return true;
    }

    return false;
}
 
Example 2
Source File: ProtocolDecoderImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private String decodeString ( final IoSession session, final IoBuffer data ) throws CharacterCodingException
{
    final String result = data.getPrefixedString ( Sessions.getCharsetDecoder ( session ) );
    if ( result.isEmpty () )
    {
        return null;
    }
    else
    {
        return result;
    }
}
 
Example 3
Source File: ProtocolDecoderImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private BrowseAdded.Entry decodeBrowserAddEntry ( final IoBuffer data, final IoSession session ) throws ProtocolCodecException
{
    final short register = (short)data.getUnsignedShort ();
    // FIXME: validate if short works

    final byte b = data.get ();
    final DataType dataType = DataType.fromByte ( b );

    if ( dataType == null )
    {
        throw new ProtocolCodecException ( String.format ( "Data type %s is unkown", b ) );
    }

    final Set<BrowseAdded.Entry.Flags> flags = data.getEnumSet ( BrowseAdded.Entry.Flags.class );

    final CharsetDecoder decoder = Sessions.getCharsetDecoder ( session );

    try
    {
        final String name = data.getPrefixedString ( decoder );
        final String description = data.getPrefixedString ( decoder );
        final String unit = data.getPrefixedString ( decoder );
        return new BrowseAdded.Entry ( register, name, description, unit, dataType, flags );
    }
    catch ( final CharacterCodingException e )
    {
        throw new ProtocolCodecException ( e );
    }

}
 
Example 4
Source File: ProtocolDecoderImpl.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private boolean processWelcome ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
    final int len = messageLength ( data );
    if ( len < 0 )
    {
        return false;
    }

    final EnumSet<Welcome.Features> features = data.getEnumSetShort ( Welcome.Features.class );

    logger.debug ( "Features: {}", features );

    final CharsetDecoder decoder = this.defaultCharset.newDecoder ();

    final int count = data.getUnsignedShort ();
    final Map<String, String> properties = new HashMap<String, String> ( count );

    for ( int i = 0; i < count; i++ )
    {
        try
        {
            final String key = data.getPrefixedString ( decoder );
            final String value = data.getPrefixedString ( decoder );

            properties.put ( key, value );
        }
        catch ( final CharacterCodingException e )
        {
            throw new ProtocolCodecException ( e );
        }
    }

    if ( features.contains ( Welcome.Features.LITTLE_ENDIAN ) )
    {
        logger.info ( "Setting little endian" );
        Sessions.setLittleEndian ( session );
    }

    out.write ( new Welcome ( features, properties ) );

    return true;
}