private static final int HEADER_SIZE = 12; /** * Method is called, when new data was read from the Connection and ready * to be processed. * * We override this method to perform Buffer -> GIOPMessage transformation. * * @param ctx Context of {@link FilterChainContext} processing * @return the next action * @throws java.io.IOException */ @Override public NextAction handleRead(final FilterChainContext ctx) throws IOException { // Get the source buffer from the context Buffer sourceBuffer = ctx.getMessage(); // sourceBuffer may contain multiple messages. We should process as // many as possible before returning the remainder (otherwise messages // may be delayed) while (sourceBuffer != null) { int sourceBufferLength = sourceBuffer.remaining(); // If source buffer doesn't contain header if (sourceBufferLength < HEADER_SIZE) { // stop the filterchain processing and store sourceBuffer to be // used next time break; } // Get the body length int bodyLength = sourceBuffer.getInt(HEADER_SIZE - 4); // The complete message length int completeMessageLength = HEADER_SIZE + bodyLength; // If the source message doesn't contain entire body if (sourceBufferLength < completeMessageLength) { // stop the filterchain processing and store sourceBuffer to be // used next time break; } // Check if the source buffer has more than 1 complete GIOP message // If yes - split up the first message and the remainder final Buffer remainder = sourceBufferLength > completeMessageLength ? sourceBuffer.split(completeMessageLength) : null; // Construct a GIOP message final GIOPMessage giopMessage = new GIOPMessage(); // Set GIOP header bytes giopMessage.setGIOPHeader(sourceBuffer.get(), sourceBuffer.get(), sourceBuffer.get(), sourceBuffer.get()); // Set major version giopMessage.setMajor(sourceBuffer.get()); // Set minor version giopMessage.setMinor(sourceBuffer.get()); // Set flags giopMessage.setFlags(sourceBuffer.get()); // Set value giopMessage.setValue(sourceBuffer.get()); // Set body length giopMessage.setBodyLength(sourceBuffer.getInt()); // Read body final byte[] body = new byte[bodyLength]; sourceBuffer.get(body); // Set body giopMessage.setBody(body); ctx.setMessage(giopMessage); // We can try to dispose the buffer sourceBuffer.tryDispose(); // Now try the remaining data sourceBuffer = remainder; } // Instruct FilterChain to store the remainder (if any) and continue execution return ctx.getInvokeAction(sourceBuffer); }