Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#bytesBefore()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#bytesBefore() . 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: Message.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public String readCString( ChannelBuffer buffer ) {
    int i = buffer.bytesBefore( ( byte ) 0 );
    if ( i < 0 ) {
        return null;
    }
    String s = buffer.toString( buffer.readerIndex(), i, Charset.forName( "UTF-8" ) );
    buffer.skipBytes( i + 1 );
    return s;
}
 
Example 2
Source File: MemcachedCommandDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
/**
 * Process an inbound string from the pipeline's downstream, and depending
 * on the state (waiting for data or processing commands), turn them into
 * the correct type of command.
 * 
 * @param channelHandlerContext
 * @param messageEvent
 * @throws Exception
 */
@Override
public void messageReceived(ChannelHandlerContext channelHandlerContext, MessageEvent messageEvent)
		throws Exception {
	ChannelBuffer in = (ChannelBuffer) messageEvent.getMessage();

	try {
		// Because of the frame handler, we are assured that we are
		// receiving only complete lines or payloads.
		// Verify that we are in 'processing()' mode
		if (status.state == SessionStatus.State.PROCESSING) {
			// split into pieces
			List<String> pieces = new ArrayList<String>(6);
			int pos = in.bytesBefore(space);
			do {
				if (pos != -1) {
					pieces.add(in.toString(in.readerIndex(), pos, USASCII));
					in.skipBytes(pos + 1);
				}
			} while ((pos = in.bytesBefore(space)) != -1);
			pieces.add(in.toString(USASCII));

			processLine(pieces, messageEvent.getChannel(), channelHandlerContext);
		} else if (status.state == SessionStatus.State.PROCESSING_MULTILINE) {
			ChannelBuffer slice = in.copy();
			byte[] payload = slice.array();
			in.skipBytes(in.readableBytes());
			continueSet(messageEvent.getChannel(), status, payload, channelHandlerContext);
		} else {
			throw new InvalidProtocolStateException("invalid protocol state");
		}

	} finally {
		// Now indicate that we need more for this command by changing the
		// session status's state.
		// This instructs the frame decoder to start collecting data for us.
		// Note, we don't do this if we're waiting for data.
		if (status.state != SessionStatus.State.WAITING_FOR_DATA)
			status.ready();
	}
}
 
Example 3
Source File: MemcachedFrameDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, org.jboss.netty.channel.Channel channel, ChannelBuffer buffer)
		throws Exception {
	// check the state. if we're WAITING_FOR_DATA that means instead of
	// breaking into lines, we need N bytes
	// otherwise, we're waiting for input
	if (status.state == SessionStatus.State.WAITING_FOR_DATA) {
		if (buffer.readableBytes() < status.bytesNeeded + MemcachedResponseEncoder.CRLF.capacity())
			return null;

		// verify delimiter matches at the right location
		ChannelBuffer dest = buffer.slice(status.bytesNeeded + buffer.readerIndex(), 2);
		StatsCounter.bytes_written.addAndGet(status.bytesNeeded);
		if (!dest.equals(MemcachedResponseEncoder.CRLF)) {
			// before we throw error... we're ready for the next command
			status.ready();

			// error, no delimiter at end of payload
			throw new IncorrectlyTerminatedPayloadException("payload not terminated correctly");
		} else {
			status.processingMultiline();

			// There's enough bytes in the buffer and the delimiter is at
			// the end. Read it.
			ChannelBuffer result = buffer.slice(buffer.readerIndex(), status.bytesNeeded);
			buffer.skipBytes(status.bytesNeeded + MemcachedResponseEncoder.CRLF.capacity());

			return result;
		}

	} else {
		int minFrameLength = Integer.MAX_VALUE;
		ChannelBuffer foundDelimiter = null;
		// command length
		int frameLength = buffer.bytesBefore(buffer.readerIndex(), buffer.readableBytes(),
				ChannelBufferIndexFinder.CRLF);
		if (frameLength >= 0 && frameLength < minFrameLength && buffer.readableBytes() >= frameLength + 2) {
			minFrameLength = frameLength;
			foundDelimiter = MemcachedResponseEncoder.CRLF;
		}

		if (foundDelimiter != null) {
			int minDelimLength = foundDelimiter.capacity();

			if (discardingTooLongFrame) {
				// We've just finished discarding a very large frame.
				// Throw an exception and go back to the initial state.
				long tooLongFrameLength = this.tooLongFrameLength;
				this.tooLongFrameLength = 0L;
				discardingTooLongFrame = false;
				buffer.skipBytes(minFrameLength + minDelimLength);
				fail(tooLongFrameLength + minFrameLength + minDelimLength);
			}

			if (minFrameLength > maxFrameLength) {
				// Discard read frame.
				buffer.skipBytes(minFrameLength + minDelimLength);
				StatsCounter.bytes_written.addAndGet(minFrameLength + minDelimLength);
				fail(minFrameLength);
			}

			ChannelBuffer frame = buffer.slice(buffer.readerIndex(), minFrameLength);
			buffer.skipBytes(minFrameLength + minDelimLength);
			status.processing();
			StatsCounter.bytes_written.addAndGet(minFrameLength + minDelimLength);
			return frame;
		} else {
			if (buffer.readableBytes() > maxFrameLength) {
				// Discard the content of the buffer until a delimiter is
				// found.
				tooLongFrameLength = buffer.readableBytes();
				buffer.skipBytes(buffer.readableBytes());
				discardingTooLongFrame = true;
				StatsCounter.bytes_written.addAndGet(tooLongFrameLength);
			}

			return null;
		}
	}
}
 
Example 4
Source File: MemcachedCommandDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
/**
 * Process an inbound string from the pipeline's downstream, and depending
 * on the state (waiting for data or processing commands), turn them into
 * the correct type of command.
 * 
 * @param channelHandlerContext
 * @param messageEvent
 * @throws Exception
 */
@Override
public void messageReceived(ChannelHandlerContext channelHandlerContext, MessageEvent messageEvent)
		throws Exception {
	ChannelBuffer in = (ChannelBuffer) messageEvent.getMessage();

	try {
		// Because of the frame handler, we are assured that we are
		// receiving only complete lines or payloads.
		// Verify that we are in 'processing()' mode
		if (status.state == SessionStatus.State.PROCESSING) {
			// split into pieces
			List<String> pieces = new ArrayList<String>(6);
			int pos = in.bytesBefore(space);
			do {
				if (pos != -1) {
					pieces.add(in.toString(in.readerIndex(), pos, USASCII));
					in.skipBytes(pos + 1);
				}
			} while ((pos = in.bytesBefore(space)) != -1);
			pieces.add(in.toString(USASCII));

			processLine(pieces, messageEvent.getChannel(), channelHandlerContext);
		} else if (status.state == SessionStatus.State.PROCESSING_MULTILINE) {
			ChannelBuffer slice = in.copy();
			byte[] payload = slice.array();
			in.skipBytes(in.readableBytes());
			continueSet(messageEvent.getChannel(), status, payload, channelHandlerContext);
		} else {
			throw new InvalidProtocolStateException("invalid protocol state");
		}

	} finally {
		// Now indicate that we need more for this command by changing the
		// session status's state.
		// This instructs the frame decoder to start collecting data for us.
		// Note, we don't do this if we're waiting for data.
		if (status.state != SessionStatus.State.WAITING_FOR_DATA)
			status.ready();
	}
}
 
Example 5
Source File: MemcachedFrameDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, org.jboss.netty.channel.Channel channel, ChannelBuffer buffer)
		throws Exception {
	// check the state. if we're WAITING_FOR_DATA that means instead of
	// breaking into lines, we need N bytes
	// otherwise, we're waiting for input
	if (status.state == SessionStatus.State.WAITING_FOR_DATA) {
		if (buffer.readableBytes() < status.bytesNeeded + MemcachedResponseEncoder.CRLF.capacity())
			return null;

		// verify delimiter matches at the right location
		ChannelBuffer dest = buffer.slice(status.bytesNeeded + buffer.readerIndex(), 2);
		StatsCounter.bytes_written.addAndGet(status.bytesNeeded);
		if (!dest.equals(MemcachedResponseEncoder.CRLF)) {
			// before we throw error... we're ready for the next command
			status.ready();

			// error, no delimiter at end of payload
			throw new IncorrectlyTerminatedPayloadException("payload not terminated correctly");
		} else {
			status.processingMultiline();

			// There's enough bytes in the buffer and the delimiter is at
			// the end. Read it.
			ChannelBuffer result = buffer.slice(buffer.readerIndex(), status.bytesNeeded);
			buffer.skipBytes(status.bytesNeeded + MemcachedResponseEncoder.CRLF.capacity());

			return result;
		}

	} else {
		int minFrameLength = Integer.MAX_VALUE;
		ChannelBuffer foundDelimiter = null;
		// command length
		int frameLength = buffer.bytesBefore(buffer.readerIndex(), buffer.readableBytes(),
				ChannelBufferIndexFinder.CRLF);
		if (frameLength >= 0 && frameLength < minFrameLength && buffer.readableBytes() >= frameLength + 2) {
			minFrameLength = frameLength;
			foundDelimiter = MemcachedResponseEncoder.CRLF;
		}

		if (foundDelimiter != null) {
			int minDelimLength = foundDelimiter.capacity();

			if (discardingTooLongFrame) {
				// We've just finished discarding a very large frame.
				// Throw an exception and go back to the initial state.
				long tooLongFrameLength = this.tooLongFrameLength;
				this.tooLongFrameLength = 0L;
				discardingTooLongFrame = false;
				buffer.skipBytes(minFrameLength + minDelimLength);
				fail(tooLongFrameLength + minFrameLength + minDelimLength);
			}

			if (minFrameLength > maxFrameLength) {
				// Discard read frame.
				buffer.skipBytes(minFrameLength + minDelimLength);
				StatsCounter.bytes_written.addAndGet(minFrameLength + minDelimLength);
				fail(minFrameLength);
			}

			ChannelBuffer frame = buffer.slice(buffer.readerIndex(), minFrameLength);
			buffer.skipBytes(minFrameLength + minDelimLength);
			status.processing();
			StatsCounter.bytes_written.addAndGet(minFrameLength + minDelimLength);
			return frame;
		} else {
			if (buffer.readableBytes() > maxFrameLength) {
				// Discard the content of the buffer until a delimiter is
				// found.
				tooLongFrameLength = buffer.readableBytes();
				buffer.skipBytes(buffer.readableBytes());
				discardingTooLongFrame = true;
				StatsCounter.bytes_written.addAndGet(tooLongFrameLength);
			}

			return null;
		}
	}
}