Java Code Examples for org.apache.mina.common.ByteBuffer#flip()

The following examples show how to use org.apache.mina.common.ByteBuffer#flip() . 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: StructEncoder.java    From javastruct with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) 
throws Exception {
	StructMessage m = (StructMessage)obj;
	byte[] buffer = JavaStruct.pack(m);
    ByteBuffer b = ByteBuffer.allocate(buffer.length + 8, false);
    b.putInt(buffer.length + 4);
    b.putInt(m.getID());
    b.put(buffer);
    b.flip();
    out.write(b);
}
 
Example 2
Source File: WelderEncoder.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput out)
    throws Exception {             
    ByteBuffer buf = ByteBuffer.allocate(PAYLOAD_SIZE);
    String s = (String) message;                    
    buf.put(s.getBytes());
    buf.flip();
    out.write(buf);
}
 
Example 3
Source File: CougarProtocolEncoder.java    From cougar with Apache License 2.0 4 votes vote down vote up
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
    final ByteBuffer buffer;
    if (message instanceof ProtocolMessage) {
        ProtocolMessage pm = (ProtocolMessage) message;
        nioLogger.log(PROTOCOL, session, "CougarProtocolEncoder: Writing protocol message %s", pm.getProtocolMessageType());

        Byte version = (Byte) session.getAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME);
        // go for lowest likely common denominator, since this will likely only occur for RejectMessages
        if (version == null) {
            version = CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MIN_SUPPORTED;
        }
        buffer = pm.getSerialisedForm(version);
        if (buffer == null) {
            badMessagesRequested.incrementAndGet();
            throw new IllegalArgumentException("Couldn't serialise ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "]");
        }

        switch (pm.getProtocolMessageType()) {
            case ACCEPT:
                acceptsSent.incrementAndGet();
                break;

            case CONNECT:
                connectsSent.incrementAndGet();
                break;

            case REJECT:
                rejectsSent.incrementAndGet();
                break;

            case KEEP_ALIVE:
                keepAlivesSent.incrementAndGet();
                break;
            case DISCONNECT:
                disconnectsSent.incrementAndGet();
                break;

            case MESSAGE_REQUEST:
                messageRequestsSent.incrementAndGet();
                nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", (((RequestMessage) pm).getPayload().length + 8));
                break;
            case MESSAGE_RESPONSE:
                messageRequestsSent.incrementAndGet();
                nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", ((ResponseMessage) pm).getPayload().length);
                break;

            case EVENT:
                eventsSent.incrementAndGet();
                nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing event of length %s", ((EventMessage) pm).getPayload().length);
                break;
            case SUSPEND:
                suspendsSent.incrementAndGet();
                break;

            case START_TLS_REQUEST:
                tlsRequestsSent.incrementAndGet();
                break;
            case START_TLS_RESPONSE:
                tlsResponsesSent.incrementAndGet();
                break;

            default:
                badMessagesRequested.incrementAndGet();
                throw new IllegalArgumentException("Unknown ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "] received");

        }
    } else {
        throw new IllegalArgumentException("Unknown message type " + message);
    }
    buffer.flip();
    out.write(buffer);
    out.flush();
}