List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:com.torodb.mongowp.bson.netty.NettyBsonLowLevelReader.java
License:Apache License
public Entry<?> readDocumentEntry(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException { BsonType bsonType = ParsingTools.getBsonType(byteBuf.readByte()); String key = stringReader.readCString(byteBuf, true); switch (bsonType) { case ARRAY:/*from w w w.j ava 2 s . c om*/ return new SimpleEntry<>(key, readArray(byteBuf)); case BINARY: return new SimpleEntry<>(key, readBinary(byteBuf)); case DATETIME: return new SimpleEntry<>(key, readDateTime(byteBuf)); case DB_POINTER: return new SimpleEntry<>(key, readDbPointer(byteBuf)); case DECIMAL128: return new SimpleEntry<>(key, readDecimal128(byteBuf)); case DEPRECATED: return new SimpleEntry<>(key, readDeprecated(byteBuf)); case DOCUMENT: return new SimpleEntry<>(key, readDocument(byteBuf)); case DOUBLE: return new SimpleEntry<>(key, readDouble(byteBuf)); case BOOLEAN: return new SimpleEntry<>(key, readBoolean(byteBuf)); case INT32: return new SimpleEntry<>(key, readInt32(byteBuf)); case INT64: return new SimpleEntry<>(key, readInt64(byteBuf)); case JAVA_SCRIPT: return new SimpleEntry<>(key, readJavaScript(byteBuf)); case JAVA_SCRIPT_WITH_SCOPE: return new SimpleEntry<>(key, readJavaScriptWithScope(byteBuf)); case MAX: return new SimpleEntry<>(key, readMax(byteBuf)); case MIN: return new SimpleEntry<>(key, readMin(byteBuf)); case NULL: return new SimpleEntry<>(key, readNull(byteBuf)); case OBJECT_ID: return new SimpleEntry<>(key, readObjectId(byteBuf)); case REGEX: return new SimpleEntry<>(key, readRegex(byteBuf)); case STRING: return new SimpleEntry<>(key, readString(byteBuf)); case TIMESTAMP: return new SimpleEntry<>(key, readTimestamp(byteBuf)); case UNDEFINED: return new SimpleEntry<>(key, readUndefined(byteBuf)); default: throw new NettyBsonReaderException("Unexpected bson type " + bsonType); } }
From source file:com.touwolf.esl.impl.EslMessageDecoder.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { while (in.isReadable()) { char ch = (char) in.readByte(); if (isBody && ch == '\n') { eslMsg.setBody(decode(sb.toString())); out.add(eslMsg);/*w ww .j a v a 2s . c o m*/ isHeader = true; isBody = false; sb = new StringBuilder(); eslMsg = new EslMessageImpl(); } else if (isHeader && ch == '\n' && header == null && !isBody) { if ("api/response".equalsIgnoreCase(eslMsg.get("Content-Type"))) { isHeader = false; isBody = true; } else { if (!eslMsg.getHeaders().isEmpty()) { out.add(eslMsg); } eslMsg = new EslMessageImpl(); } sb = new StringBuilder(); header = null; value = null; } else if (isHeader && ch == ':' && !isBody) { header = sb.toString(); sb = new StringBuilder(); isHeader = false; } else if (ch == '\n' && !isBody) { value = decode(sb.toString()); isHeader = true; sb = new StringBuilder(); if (header != null && value != null) { eslMsg.getHeaders().put(header.trim(), value.trim()); } header = null; value = null; } else { sb.append(ch); } } }
From source file:com.turn.ttorrent.client.io.PeerMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { if (buf.readableBytes() == 0) { out.add(new PeerMessage.KeepAliveMessage()); return;/*w w w . j av a2 s. com*/ } byte type = buf.readByte(); PeerMessage message; switch (type) { case 0: message = new PeerMessage.ChokeMessage(); break; case 1: message = new PeerMessage.UnchokeMessage(); break; case 2: message = new PeerMessage.InterestedMessage(); break; case 3: message = new PeerMessage.NotInterestedMessage(); break; case 4: message = new PeerMessage.HaveMessage(); break; case 5: message = new PeerMessage.BitfieldMessage(); break; case 6: message = new PeerMessage.RequestMessage(); break; case 7: message = new PeerMessage.PieceMessage(); break; case 8: message = new PeerMessage.CancelMessage(); break; case 20: byte extendedType = buf.readByte(); // Throws on short packet. This is normal. switch (extendedType) { case 0: message = new PeerExtendedMessage.HandshakeMessage(); break; case 1: message = new PeerExtendedMessage.UtPexMessage(); break; default: throw new IOException("Unknown extended message type " + extendedType); } break; default: throw new IOException("Unknown message type " + type); } message.fromWire(buf); out.add(message); // if (buf.readableBytes() > 0) throw new IOException("Badly framed message " + message + "; remaining=" + buf.readableBytes()); }
From source file:com.twitter.http2.HttpFrameDecoder.java
License:Apache License
/** * Decode the byte buffer.//from w ww. j av a 2s . c om */ public void decode(ByteBuf buffer) { boolean endStream; boolean endSegment; int minLength; int dependency; int weight; boolean exclusive; int errorCode; while (true) { switch (state) { case READ_CONNECTION_HEADER: while (buffer.isReadable()) { byte b = buffer.readByte(); if (b != CLIENT_CONNECTION_PREFACE[length++]) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Connection Header"); return; } if (length == CLIENT_CONNECTION_PREFACE.length) { state = State.READ_FRAME_HEADER; break; } } if (buffer.isReadable()) { break; } else { return; } case READ_FRAME_HEADER: // Wait until entire header is readable if (buffer.readableBytes() < HTTP_FRAME_HEADER_SIZE) { return; } // Read frame header fields readFrameHeader(buffer); // TODO(jpinner) Sec 4.2 FRAME_SIZE_ERROR if (!isValidFrameHeader(length, type, flags, streamId)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Header"); } else if (frameHasPadding(type, flags)) { state = State.READ_PADDING_LENGTH; } else { paddingLength = 0; state = getNextState(length, type); } break; case READ_PADDING_LENGTH: if (buffer.readableBytes() < 1) { return; } paddingLength = buffer.readUnsignedByte(); --length; if (!isValidPaddingLength(length, type, flags, paddingLength)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Padding Length"); } else { state = getNextState(length, type); } break; case READ_DATA_FRAME: endStream = hasFlag(flags, HTTP_FLAG_END_STREAM); state = State.READ_DATA; if (hasFlag(flags, HTTP_FLAG_PADDED)) { delegate.readDataFramePadding(streamId, endStream, paddingLength + 1); } break; case READ_DATA: // Generate data frames that do not exceed maxChunkSize // maxChunkSize must be > 0 so we cannot infinitely loop int dataLength = Math.min(maxChunkSize, length - paddingLength); // Wait until entire frame is readable if (buffer.readableBytes() < dataLength) { return; } ByteBuf data = buffer.readBytes(dataLength); length -= dataLength; if (length == paddingLength) { if (paddingLength == 0) { state = State.READ_FRAME_HEADER; } else { state = State.SKIP_FRAME_PADDING; } } endStream = length == paddingLength && hasFlag(flags, HTTP_FLAG_END_STREAM); endSegment = length == paddingLength && hasFlag(flags, HTTP_FLAG_END_SEGMENT); delegate.readDataFrame(streamId, endStream, endSegment, data); break; case READ_HEADERS_FRAME: minLength = 0; if (hasFlag(flags, HTTP_FLAG_PRIORITY)) { minLength = 5; } if (buffer.readableBytes() < minLength) { return; } endStream = hasFlag(flags, HTTP_FLAG_END_STREAM); endSegment = hasFlag(flags, HTTP_FLAG_END_SEGMENT); exclusive = false; dependency = 0; weight = 16; if (hasFlag(flags, HTTP_FLAG_PRIORITY)) { dependency = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); weight = buffer.readUnsignedByte() + 1; if (dependency < 0) { dependency = dependency & 0x7FFFFFFF; exclusive = true; } length -= 5; } state = State.READ_HEADER_BLOCK; delegate.readHeadersFrame(streamId, endStream, endSegment, exclusive, dependency, weight); break; case READ_PRIORITY_FRAME: if (buffer.readableBytes() < length) { return; } exclusive = false; dependency = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); weight = buffer.readUnsignedByte() + 1; if (dependency < 0) { dependency = dependency & 0x7FFFFFFF; exclusive = true; } state = State.READ_FRAME_HEADER; delegate.readPriorityFrame(streamId, exclusive, dependency, weight); break; case READ_RST_STREAM_FRAME: if (buffer.readableBytes() < length) { return; } errorCode = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(length); state = State.READ_FRAME_HEADER; delegate.readRstStreamFrame(streamId, errorCode); break; case READ_SETTINGS_FRAME: boolean ack = hasFlag(flags, HTTP_FLAG_ACK); state = State.READ_SETTING; delegate.readSettingsFrame(ack); break; case READ_SETTING: if (length == 0) { state = State.READ_FRAME_HEADER; delegate.readSettingsEnd(); break; } if (buffer.readableBytes() < 6) { return; } int id = getUnsignedShort(buffer, buffer.readerIndex()); int value = getSignedInt(buffer, buffer.readerIndex() + 2); buffer.skipBytes(6); length -= 6; delegate.readSetting(id, value); break; case READ_PUSH_PROMISE_FRAME: if (buffer.readableBytes() < 4) { return; } int promisedStreamId = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); length -= 4; if (promisedStreamId == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Promised-Stream-ID"); } else { state = State.READ_HEADER_BLOCK; delegate.readPushPromiseFrame(streamId, promisedStreamId); } break; case READ_PING_FRAME: if (buffer.readableBytes() < length) { return; } long ping = getSignedLong(buffer, buffer.readerIndex()); buffer.skipBytes(length); boolean pong = hasFlag(flags, HTTP_FLAG_ACK); state = State.READ_FRAME_HEADER; delegate.readPingFrame(ping, pong); break; case READ_GOAWAY_FRAME: if (buffer.readableBytes() < 8) { return; } int lastStreamId = getUnsignedInt(buffer, buffer.readerIndex()); errorCode = getSignedInt(buffer, buffer.readerIndex() + 4); buffer.skipBytes(8); length -= 8; if (length == 0) { state = State.READ_FRAME_HEADER; } else { paddingLength = length; state = State.SKIP_FRAME_PADDING; } delegate.readGoAwayFrame(lastStreamId, errorCode); break; case READ_WINDOW_UPDATE_FRAME: // Wait until entire frame is readable if (buffer.readableBytes() < length) { return; } int windowSizeIncrement = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(length); if (windowSizeIncrement == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Window Size Increment"); } else { state = State.READ_FRAME_HEADER; delegate.readWindowUpdateFrame(streamId, windowSizeIncrement); } break; case READ_CONTINUATION_FRAME_HEADER: // Wait until entire frame header is readable if (buffer.readableBytes() < HTTP_FRAME_HEADER_SIZE) { return; } // Read and validate continuation frame header fields int prevStreamId = streamId; readFrameHeader(buffer); // TODO(jpinner) Sec 4.2 FRAME_SIZE_ERROR // TODO(jpinner) invalid flags if (type != HTTP_CONTINUATION_FRAME || streamId != prevStreamId) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Continuation Frame"); } else { paddingLength = 0; state = State.READ_HEADER_BLOCK; } break; case READ_HEADER_BLOCK: if (length == paddingLength) { boolean endHeaders = hasFlag(flags, HTTP_FLAG_END_HEADERS); if (endHeaders) { state = State.SKIP_FRAME_PADDING; delegate.readHeaderBlockEnd(); } else { state = State.SKIP_FRAME_PADDING_CONTINUATION; } break; } if (!buffer.isReadable()) { return; } int readableBytes = Math.min(buffer.readableBytes(), length - paddingLength); ByteBuf headerBlockFragment = buffer.readBytes(readableBytes); length -= readableBytes; delegate.readHeaderBlock(headerBlockFragment); break; case SKIP_FRAME_PADDING: int numBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numBytes); length -= numBytes; if (length == 0) { state = State.READ_FRAME_HEADER; break; } return; case SKIP_FRAME_PADDING_CONTINUATION: int numPaddingBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numPaddingBytes); length -= numPaddingBytes; if (length == 0) { state = State.READ_CONTINUATION_FRAME_HEADER; break; } return; case FRAME_ERROR: buffer.skipBytes(buffer.readableBytes()); return; default: throw new Error("Shouldn't reach here."); } } }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertDataFrame(ByteBuf frame, int streamId, boolean last, ByteBuf data) { byte type = 0x00; byte flags = 0x00; if (last) {/* w ww . j ava 2 s .c o m*/ flags |= 0x01; } int length = assertFrameHeader(frame, type, flags, streamId); assertEquals(data.readableBytes(), length); for (int i = 0; i < length; i++) { assertEquals(data.getByte(i), frame.readByte()); } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertHeadersFrame(ByteBuf frame, int streamId, boolean exclusive, int dependency, int weight, boolean last, ByteBuf headerBlock) { boolean hasPriority = exclusive || dependency != HTTP_DEFAULT_DEPENDENCY || weight != HTTP_DEFAULT_WEIGHT; int maxLength = hasPriority ? HTTP_MAX_LENGTH - 5 : HTTP_MAX_LENGTH; byte type = 0x01; byte flags = 0x00; if (last) {//from w w w . jav a2 s.com flags |= 0x01; } if (headerBlock.readableBytes() <= maxLength) { flags |= 0x04; } if (hasPriority) { flags |= 0x20; } int length = assertFrameHeader(frame, type, flags, streamId); if (hasPriority) { assertTrue(length >= 5); if (exclusive) { assertEquals(dependency | 0x80000000, frame.readInt()); } else { assertEquals(dependency, frame.readInt()); } assertEquals(weight - 1, frame.readUnsignedByte()); length -= 5; } assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } while (headerBlock.isReadable()) { type = 0x09; flags = 0x00; if (headerBlock.readableBytes() <= HTTP_MAX_LENGTH) { flags |= 0x04; } length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertPushPromiseFrame(ByteBuf frame, int streamId, int promisedStreamId, ByteBuf headerBlock) { int maxLength = HTTP_MAX_LENGTH - 4; byte type = 0x05; byte flags = 0x00; if (headerBlock.readableBytes() <= maxLength) { flags |= 0x04;/*w w w . j a v a 2 s .co m*/ } int length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length >= 4); assertEquals(promisedStreamId, frame.readInt()); length -= 4; assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } while (headerBlock.isReadable()) { type = 0x09; flags = 0x00; if (headerBlock.readableBytes() <= HTTP_MAX_LENGTH) { flags |= 0x04; } length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static int assertFrameHeader(ByteBuf frame, byte type, byte flags, int streamId) { int length = frame.readUnsignedMedium(); assertEquals(type, frame.readByte()); assertEquals(flags, frame.readByte()); assertEquals(streamId, frame.readInt()); return length; }
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static Trace decodeTrace(ByteBuf buffer) { long spanId = buffer.readLong(); long parentId = buffer.readLong(); long traceId = buffer.readLong(); byte traceFlags = buffer.readByte(); return new Trace(spanId, parentId, traceId, traceFlags); }
From source file:com.uber.tchannel.codecs.TFrameCodec.java
License:Open Source License
public static TFrame decode(ByteBuf buffer) { // size:2//w ww . ja va 2s . c om int size = buffer.readUnsignedShort() - TFrame.FRAME_HEADER_LENGTH; // type:1 byte type = buffer.readByte(); // reserved:1 buffer.skipBytes(1); // id:4 long id = buffer.readUnsignedInt(); // reserved:8 buffer.skipBytes(8); // payload:16+ ByteBuf payload = buffer.readSlice(size); // payload.retain(); return new TFrame(size, type, id, payload); }