

package gov.va.med.cds.socket.server.handler;


import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;


public class NewLineReplacementFilter
    extends
        IoFilterAdapter
{
    @Override
    public void messageReceived( NextFilter nextFilter, IoSession session, Object message )
        throws Exception
    {
        if ( !( message instanceof IoBuffer ) )
        {
            nextFilter.messageReceived( session, message );
            return;
        }

        IoBuffer in = ( IoBuffer )message;

        byte[] byteArray = new byte[in.limit()];

        int index = 0;

        while ( in.hasRemaining() )
        {
            byteArray[index] = ( byte )in.get();

            if ( byteArray[index] == '\r' )
            {
                System.out.println( "Character : " + byteArray[index] );

                byteArray[index] = '\n';
            }

            index++ ;
        }

        System.out.println( new String( byteArray ) );

        // TODO Auto-generated method stub
        super.messageReceived( nextFilter, session, IoBuffer.wrap( byteArray ) );
    }
}
