Package org.apache.mina.common

Examples of org.apache.mina.common.ByteBuffer


     *
     * @param listener the {@code FilterListener} on which to send the data
     * @param message the data to filter and forward to the listener
     */
    void filterSend(FilterListener listener, byte[] message) {
        ByteBuffer buffer = ByteBuffer.allocate(message.length + 2, false);
        buffer.putShort((short) message.length);
        buffer.put(message);
        buffer.flip();
        // Don't worry about the listener throwing an exception, since
        // this method has no other side effects.
        listener.sendUnfiltered(buffer);
    }
View Full Code Here


        if (logger.isLoggable(Level.FINEST)) {
            logger.log(Level.FINEST, "recv on {0}: {1}", conn, message);
        }
       
        ByteBuffer buf = (ByteBuffer) message;
        try {
            conn.getFilter().filterReceive(conn, buf);
        } catch (RuntimeException e) {
            logger.logThrow(Level.FINER, e,
                "exception in recv of {0}:", buf);
View Full Code Here

        public ProtocolEncoder getEncoder() throws Exception {
            return new ProtocolEncoder() {
                public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput out)
                    throws Exception {
                    ByteBuffer bb = ByteBuffer.allocate(9).setAutoExpand(true);
                    bb.put("Bye World".getBytes());
                    bb.flip();
                    out.write(bb);
                }

                public void dispose(IoSession ioSession) throws Exception {
                    // do nothing
View Full Code Here

        if (codecFactory == null) {
            codecFactory = new ProtocolCodecFactory() {
                public ProtocolEncoder getEncoder() throws Exception {
                    return new ProtocolEncoder() {
                        public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
                            ByteBuffer buf = toByteBuffer(message);
                            buf.flip();
                            out.write(buf);
                        }

                        public void dispose(IoSession session) throws Exception {
                            // do nothing
View Full Code Here

        addCodecFactory(config, codecFactory);
    }

    protected ByteBuffer toByteBuffer(Object message) throws CharacterCodingException {
        ByteBuffer answer = null;
        try {
            answer = convertTo(ByteBuffer.class, message);
        } catch (NoTypeConversionAvailableException e) {
            String value = convertTo(String.class, message);
            answer = ByteBuffer.allocate(value.length()).setAutoExpand(true);
            answer.putString(value, encoder);
        }
        return answer;
    }
View Full Code Here

        if( !( message instanceof Serializable ) )
        {
            throw new NotSerializableException();
        }

        ByteBuffer buf = ByteBuffer.allocate( 64 );
        buf.setAutoExpand( true );
        buf.putObject( message );
       
        int objectSize = buf.position() - 4;
        if( objectSize > maxObjectSize )
        {
            buf.release();
            throw new IllegalArgumentException( "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')' );
        }
       
        buf.flip();
        out.write( buf );
    }
View Full Code Here

        {
            nextFilter.messageReceived( session, message );
            return;
        }

        ByteBuffer in = ( ByteBuffer ) message;
        ProtocolDecoder decoder = getDecoder( session );
        ProtocolDecoderOutput decoderOut = getDecoderOut( session, nextFilter );
       
        try
        {
            decoder.decode( session, in, decoderOut );
        }
        catch( Throwable t )
        {
            ProtocolDecoderException pde;
            if( t instanceof ProtocolDecoderException )
            {
                pde = ( ProtocolDecoderException ) t;
            }
            else
            {
                pde = new ProtocolDecoderException( t );
            }
            pde.setHexdump( in.getHexDump() );
            throw pde;
        }
        finally
        {
            // Dispose the decoder if this session is connectionless.
            if( session.getTransportType().isConnectionless() )
            {
                disposeDecoder( session );
            }

            // Release the read buffer.
            in.release();

            decoderOut.flush();
        }
    }
View Full Code Here

            session.close();
    }

    public void messageReceived( IoSession session, Object message )
    {
        ByteBuffer buf = ( ByteBuffer ) message;
        // Print out read buffer content.
        while( buf.hasRemaining() )
        {
            System.out.print( ( char ) buf.get() );
        }
        System.out.flush();
    }
View Full Code Here

                throws Exception {
            if (!(message instanceof ByteBuffer)) {
                return;
            }

            ByteBuffer rb = (ByteBuffer) message;
            // Write the received data back to remote peer
            ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
            wb.put(rb);
            wb.flip();
            session.write(wb);
        }
View Full Code Here

        ProtocolEncoder encoder = new ObjectSerializationEncoder();
        encoder.encode(session, expected, out);

        Assert.assertEquals(1, out.getBufferQueue().size());
        ByteBuffer buf = out.getBufferQueue().poll();

        testDecoderAndInputStream(expected, buf);
    }
View Full Code Here

TOP

Related Classes of org.apache.mina.common.ByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.