List of usage examples for io.netty.buffer ByteBuf discardReadBytes
public abstract ByteBuf discardReadBytes();
From source file:net.openhft.fix.transport.codec.NettyFrameDecoder.java
License:Apache License
/** * TODO: loop for more messages in the ByteBuf *///www . j a v a 2 s . c o m void doDecode(ByteBuf in, List<Object> out) { if (m_msgLength == -1) { if (in.readableBytes() >= NettyFrameHelper.MSG_MIN_BYTES) { //int rindex = in.readerIndex(); int bsi = in.indexOf(0, 12, NettyFrameHelper.BYTE_SOH); int bli = in.indexOf(12, 20, NettyFrameHelper.BYTE_SOH); // check the existence of: // - BeginString 8= // - BodyLength 9= if (in.getByte(0) == NettyFrameHelper.BYTE_BEGIN_STRING && in.getByte(1) == NettyFrameHelper.BYTE_EQUALS && in.getByte(bsi + 1) == NettyFrameHelper.BYTE_BODY_LENGTH && in.getByte(bsi + 2) == NettyFrameHelper.BYTE_EQUALS) { int bodyLength = 0; for (int i = bsi + 3; i < bli; i++) { bodyLength *= 10; bodyLength += ((int) in.getByte(i) - (int) '0'); } m_msgLength = 1 + bodyLength + bli + NettyFrameHelper.MSG_CSUM_LEN; } else { throw new Error("Unexpected state (header)"); } } } if (m_msgLength != -1 && in.readableBytes() >= m_msgLength) { if (in.readableBytes() >= m_msgLength) { byte[] rv = new byte[m_msgLength]; in.readBytes(rv); in.discardReadBytes(); //TODO: validate checksum out.add(rv); m_msgLength = -1; } else { throw new Error("Unexpected state (body)"); } } }
From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.RecordIdDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { throw new IOException( "Received unexpected empty frame. Maybe you have enabled secure transmission on only one endpoint of the connection."); }/*from w w w . j ava 2s.co m*/ byte type = frame.readByte(); frame.discardReadBytes(); String id = frame.toString(CharsetUtil.UTF_8); try { log.debug("received type {} with id {}", type, id); return RecordId.fromString(store.getTracker(), id); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); } return null; }
From source file:org.apache.tinkerpop.gremlin.server.handler.WsGremlinBinaryRequestDecoder.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext channelHandlerContext, final BinaryWebSocketFrame frame, final List<Object> objects) throws Exception { final ByteBuf messageBytes = frame.content(); final byte len = messageBytes.readByte(); if (len <= 0) { objects.add(RequestMessage.INVALID); return;/*from www. ja va 2 s. c o m*/ } final ByteBuf contentTypeBytes = channelHandlerContext.alloc().buffer(len); try { messageBytes.readBytes(contentTypeBytes); final String contentType = contentTypeBytes.toString(UTF8); final MessageSerializer serializer = select(contentType, Serializers.DEFAULT_REQUEST_SERIALIZER); // it's important to re-initialize these channel attributes as they apply globally to the channel. in // other words, the next request to this channel might not come with the same configuration and mixed // state can carry through from one request to the next channelHandlerContext.channel().attr(StateKey.SESSION).set(null); channelHandlerContext.channel().attr(StateKey.SERIALIZER).set(serializer); channelHandlerContext.channel().attr(StateKey.USE_BINARY).set(true); try { objects.add(serializer.deserializeRequest(messageBytes.discardReadBytes())); } catch (SerializationException se) { objects.add(RequestMessage.INVALID); } } finally { contentTypeBytes.release(); } }
From source file:org.apache.tinkerpop.gremlin.server.handler.WsGremlinCloseRequestDecoder.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext channelHandlerContext, final CloseWebSocketFrame frame, final List<Object> objects) throws Exception { final ByteBuf messageBytes = frame.content(); final byte len = messageBytes.readByte(); if (len <= 0) { objects.add(RequestMessage.INVALID); return;/*from w ww. java 2 s . c o m*/ } final ByteBuf contentTypeBytes = channelHandlerContext.alloc().buffer(len); try { messageBytes.readBytes(contentTypeBytes); final String contentType = contentTypeBytes.toString(UTF8); final MessageSerializer serializer = select(contentType, Serializers.DEFAULT_REQUEST_SERIALIZER); // it's important to re-initialize these channel attributes as they apply globally to the channel. in // other words, the next request to this channel might not come with the same configuration and mixed // state can carry through from one request to the next channelHandlerContext.channel().attr(StateKey.SESSION).set(null); channelHandlerContext.channel().attr(StateKey.SERIALIZER).set(serializer); channelHandlerContext.channel().attr(StateKey.USE_BINARY).set(true); try { objects.add(serializer.deserializeRequest(messageBytes.discardReadBytes())); } catch (SerializationException se) { objects.add(RequestMessage.INVALID); } } finally { contentTypeBytes.release(); } }
From source file:org.code_house.ebus.netty.codec.SlaveTelegramDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() == 0) { return;/*from ww w . j a v a 2s . co m*/ } int index = in.readerIndex(); // no master, no slave, no broadcast -> slave reply or ACK if (in.readableBytes() >= 3) { // we have at least three bytes which should be slave ACK, slave reply len, slave crc. byte ack = in.readByte(); if (ack == Constants.ACK) { // slave confirmed received data byte length = in.readByte(); if (in.readableBytes() >= length + 1 /* crc */) { ByteBuffer exchange = ByteBuffer.allocate(length); in.getBytes(in.readerIndex(), exchange); in.skipBytes(length); byte crc = in.readByte(); SlaveData data = new SlaveData(exchange, calculateCrc(length), crc); if (data.isValid()) { // proper slave reply out.add(new Confirmation()); out.add(new SlaveHeader(length)); out.add(data); } else { System.out.println("Slave CRC fail!!!"); in.discardReadBytes(); } } } else if (ack == Constants.NACK) { out.add(new Rejection()); } } if (out.isEmpty()) { System.out.println("slave fail -> \n" + ByteBufUtil.prettyHexDump(in)); in.readerIndex(index); } if (in.readableBytes() > 0) { out.add(in.readBytes(super.actualReadableBytes())); } }
From source file:org.graylog.collector.file.splitters.NewlineChunkSplitter.java
License:Open Source License
@Override public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) { return new Iterable<String>() { @Override// w w w. j a v a2 s.c om public Iterator<String> iterator() { return new AbstractIterator<String>() { @Override protected String computeNext() { try { if (!buffer.isReadable()) { return endOfData(); } final int i = buffer.forEachByte(ByteBufProcessor.FIND_CRLF); if (i == -1) { if (includeRemainingData) { final ByteBuf remaining = buffer.readBytes(buffer.readableBytes()); return remaining.toString(charset); } else { return endOfData(); } } final ByteBuf fullLine = buffer.readBytes(i); // Strip the \r/\n bytes from the buffer. final byte readByte = buffer.readByte(); // the \r or \n byte if (readByte == '\r') { buffer.readByte(); // the \n byte if previous was \r } return fullLine.toString(charset); } finally { buffer.discardReadBytes(); } } }; } }; }
From source file:org.graylog.collector.file.splitters.PatternChunkSplitter.java
License:Open Source License
@Override public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) { return new Iterable<String>() { @Override/*from w ww .ja v a 2 s . c o m*/ public Iterator<String> iterator() { return new AbstractIterator<String>() { // TODO Might throw an exception if multibyte charset is used and buffer is not complete. // Use CharsetDecoder to create a CharBuffer and match on that! private final String inputAsString = buffer.toString(charset); final Matcher matcher = pattern.matcher(inputAsString); private int positionInString = 0; @Override protected String computeNext() { try { if (!buffer.isReadable()) { return endOfData(); } if (matcher.find()) { int firstByte = matcher.start(); if (firstByte == 0) { // advance further, the buffer begins with our pattern. if (matcher.find()) { firstByte = matcher.start(); } else { if (!includeRemainingData) { // couldn't find the end of the entry (i.e. there wasn't a next line yet) return endOfData(); } else { // couldn't find another line, but we are asked to finish up, include everything that remains return getRemainingContent(); } } } if (firstByte == 0) { // still haven't found a non-zero length string, keep waiting for more data. return endOfData(); } final String substring = inputAsString.substring(positionInString, firstByte); positionInString = firstByte; buffer.skipBytes(substring.getBytes(charset).length); // TODO performance return substring; } else { if (includeRemainingData) { return getRemainingContent(); } return endOfData(); } } catch (IllegalStateException e) { // the cause contains the CharacterCodingException from the ChannelBuffer.toString() methods // this usually means the buffer ended with an incomplete encoding of a unicode character. // WHY U SO SUCK CHARACTER ENCODINGS? // we need to wait until more data is available return endOfData(); } finally { buffer.discardReadBytes(); } } private String getRemainingContent() { final ByteBuf channelBuffer = buffer.readBytes(buffer.readableBytes()); return channelBuffer.toString(charset); } }; } }; }
From source file:org.iotivity.cloud.base.protocols.coap.CoapDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // TODO: need exceptional case handling while (in.isReadable(bufferToRead)) { switch (nextState) { case SHIM_HEADER: int shimHeader = in.readByte(); bufferToRead = (shimHeader >>> 4) & 0x0F; tokenLength = (shimHeader) & 0x0F; switch (bufferToRead) { case 13: bufferToRead = 1;/* w w w .ja va 2 s. c o m*/ nextState = ParsingState.OPTION_PAYLOAD_LENGTH; break; case 14: bufferToRead = 2; nextState = ParsingState.OPTION_PAYLOAD_LENGTH; break; case 15: bufferToRead = 4; nextState = ParsingState.OPTION_PAYLOAD_LENGTH; break; default: optionPayloadLength = bufferToRead; bufferToRead += 1 + tokenLength; // code + tkl nextState = ParsingState.CODE_TOKEN_OPTION; break; } break; case OPTION_PAYLOAD_LENGTH: switch (bufferToRead) { case 1: optionPayloadLength = 13 + (in.readByte() & 0xFF); break; case 2: optionPayloadLength = 269 + (((in.readByte() & 0xFF) << 8) + (in.readByte() & 0xFF)); break; case 4: optionPayloadLength = 65805 + (((in.readByte() & 0xFF) << 24) + ((in.readByte() & 0xFF) << 16) + ((in.readByte() & 0xFF) << 8) + (in.readByte() & 0xFF)); break; } nextState = ParsingState.CODE_TOKEN_OPTION; bufferToRead = 1 + tokenLength + optionPayloadLength; // code // + // tkl break; case CODE_TOKEN_OPTION: int code = in.readByte() & 0xFF; if (code <= 31) { partialMsg = new CoapRequest(code); } else { partialMsg = new CoapResponse(code); } if (tokenLength > 0) { byte[] token = new byte[tokenLength]; in.readBytes(token); partialMsg.setToken(token); } if (optionPayloadLength > 0) { int optionLen = parseOptions(partialMsg, in, optionPayloadLength); if (optionPayloadLength > optionLen) { nextState = ParsingState.PAYLOAD; bufferToRead = optionPayloadLength - optionLen; continue; } } nextState = ParsingState.FINISH; bufferToRead = 0; break; case PAYLOAD: byte[] payload = new byte[bufferToRead]; in.readBytes(payload); partialMsg.setPayload(payload); nextState = ParsingState.FINISH; bufferToRead = 0; break; case FINISH: nextState = ParsingState.SHIM_HEADER; bufferToRead = 1; out.add(partialMsg); break; default: break; } } in.discardReadBytes(); }
From source file:org.jboss.arquillian.daemon.server.NettyServer.java
License:Apache License
private static void sendResponse(final ChannelHandlerContext ctx, final ByteBuf out, final String response) { out.discardReadBytes(); try {/*from ww w .j a va 2s .c om*/ out.writeBytes(response.getBytes(WireProtocol.CHARSET)); out.writeBytes(Delimiters.lineDelimiter()[0]); } catch (final UnsupportedEncodingException uee) { throw new RuntimeException("Unsupported encoding", uee); } ctx.flush(); }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfChunkAggregator.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IllegalStateException { while (in.isReadable()) { switch (state) { case HEADER_ONE: { final byte b = in.readByte(); checkNewLine(b, "Malformed chunk header encountered (byte 0)"); state = State.HEADER_TWO; initChunk();/* w ww . j a v a2s .c om*/ break; } case HEADER_TWO: { final byte b = in.readByte(); checkHash(b, "Malformed chunk header encountered (byte 1)"); state = State.HEADER_LENGTH_FIRST; break; } case HEADER_LENGTH_FIRST: { final byte b = in.readByte(); chunkSize = processHeaderLengthFirst(b); state = State.HEADER_LENGTH_OTHER; break; } case HEADER_LENGTH_OTHER: { final byte b = in.readByte(); if (b == '\n') { state = State.DATA; break; } if (b < '0' || b > '9') { LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM_PARAM, b, (byte) '0', (byte) '9'); throw new IllegalStateException("Invalid chunk size encountered"); } chunkSize *= 10; chunkSize += b - '0'; checkChunkSize(); break; } case DATA: /* * FIXME: this gathers all data into one big chunk before passing * it on. Make sure the pipeline can work with partial data * and then change this piece to pass the data on as it * comes through. */ if (in.readableBytes() < chunkSize) { LOG.debug("Buffer has {} bytes, need {} to complete chunk", in.readableBytes(), chunkSize); in.discardReadBytes(); return; } aggregateChunks(in.readBytes((int) chunkSize)); state = State.FOOTER_ONE; break; case FOOTER_ONE: { final byte b = in.readByte(); checkNewLine(b, "Malformed chunk footer encountered (byte 0)"); state = State.FOOTER_TWO; chunkSize = 0; break; } case FOOTER_TWO: { final byte b = in.readByte(); checkHash(b, "Malformed chunk footer encountered (byte 1)"); state = State.FOOTER_THREE; break; } case FOOTER_THREE: { final byte b = in.readByte(); // In this state, either header-of-new-chunk or message-end is expected // Depends on the next character extractNewChunkOrMessageEnd(b); break; } case FOOTER_FOUR: { final byte b = in.readByte(); checkNewLine(b, "Malformed chunk footer encountered (byte 3)"); state = State.HEADER_ONE; out.add(chunk); chunk = null; break; } } } in.discardReadBytes(); }