List of usage examples for io.netty.buffer ByteBuf readSlice
public abstract ByteBuf readSlice(int length);
From source file:com.tesora.dve.server.connectionmanager.loaddata.MSPLoadDataDecoder.java
License:Open Source License
private ByteBuf decodeNextFrame(ByteBuf in) { if (in.readableBytes() < 4) { //three bytes for the length, one for the sequence. return null; }/*w ww . j a va 2s . c om*/ in.markReaderIndex(); ByteBuf buffer = in.order(ByteOrder.LITTLE_ENDIAN); int length = buffer.readUnsignedMedium(); if (buffer.readableBytes() < length + 1) { in.resetReaderIndex(); return null; } return buffer.readSlice(length + 1).order(ByteOrder.LITTLE_ENDIAN).retain(); }
From source file:com.twitter.http2.HttpFrameEncoder.java
License:Apache License
/** * Encode an HTTP/2 HEADERS Frame/*from w w w . j av a2s .co m*/ */ public ByteBuf encodeHeadersFrame(int streamId, boolean endStream, boolean exclusive, int dependency, int weight, ByteBuf headerBlock) { byte flags = endStream ? HTTP_FLAG_END_STREAM : 0; boolean hasPriority = exclusive || dependency != HTTP_DEFAULT_DEPENDENCY || weight != HTTP_DEFAULT_WEIGHT; if (hasPriority) { flags |= HTTP_FLAG_PRIORITY; } int maxLength = hasPriority ? HTTP_MAX_LENGTH - 5 : HTTP_MAX_LENGTH; boolean needsContinuations = headerBlock.readableBytes() > maxLength; if (!needsContinuations) { flags |= HTTP_FLAG_END_HEADERS; } int length = needsContinuations ? maxLength : headerBlock.readableBytes(); if (hasPriority) { length += 5; } int frameLength = hasPriority ? HTTP_FRAME_HEADER_SIZE + 5 : HTTP_FRAME_HEADER_SIZE; ByteBuf header = Unpooled.buffer(frameLength); writeFrameHeader(header, length, HTTP_HEADERS_FRAME, flags, streamId); if (hasPriority) { if (exclusive) { header.writeInt(dependency | 0x80000000); } else { header.writeInt(dependency); } header.writeByte(weight - 1); length -= 5; } ByteBuf frame = Unpooled.wrappedBuffer(header, headerBlock.readSlice(length)); if (needsContinuations) { while (headerBlock.readableBytes() > HTTP_MAX_LENGTH) { header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE); writeFrameHeader(header, HTTP_MAX_LENGTH, HTTP_CONTINUATION_FRAME, (byte) 0, streamId); frame = Unpooled.wrappedBuffer(frame, header, headerBlock.readSlice(HTTP_MAX_LENGTH)); } header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE); writeFrameHeader(header, headerBlock.readableBytes(), HTTP_CONTINUATION_FRAME, HTTP_FLAG_END_HEADERS, streamId); frame = Unpooled.wrappedBuffer(frame, header, headerBlock); } return frame; }
From source file:com.twitter.http2.HttpFrameEncoder.java
License:Apache License
/** * Encode an HTTP/2 PUSH_PROMISE Frame//w w w.ja v a2 s .c o m */ public ByteBuf encodePushPromiseFrame(int streamId, int promisedStreamId, ByteBuf headerBlock) { boolean needsContinuations = headerBlock.readableBytes() > HTTP_MAX_LENGTH - 4; int length = needsContinuations ? HTTP_MAX_LENGTH - 4 : headerBlock.readableBytes(); byte flags = needsContinuations ? 0 : HTTP_FLAG_END_HEADERS; ByteBuf header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + 4); writeFrameHeader(header, length + 4, HTTP_PUSH_PROMISE_FRAME, flags, streamId); header.writeInt(promisedStreamId); ByteBuf frame = Unpooled.wrappedBuffer(header, headerBlock.readSlice(length)); if (needsContinuations) { while (headerBlock.readableBytes() > HTTP_MAX_LENGTH) { header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE); writeFrameHeader(header, HTTP_MAX_LENGTH, HTTP_CONTINUATION_FRAME, (byte) 0, streamId); frame = Unpooled.wrappedBuffer(frame, header, headerBlock.readSlice(HTTP_MAX_LENGTH)); } header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE); writeFrameHeader(header, headerBlock.readableBytes(), HTTP_CONTINUATION_FRAME, HTTP_FLAG_END_HEADERS, streamId); frame = Unpooled.wrappedBuffer(frame, header, headerBlock); } return frame; }
From source file:com.twitter.http2.HttpStreamEncoder.java
License:Apache License
private HttpDataFrame[] createHttpDataFrames(int streamId, ByteBuf content) { int readableBytes = content.readableBytes(); int count = readableBytes / MAX_DATA_LENGTH; if (readableBytes % MAX_DATA_LENGTH > 0) { count++;//ww w .ja v a 2 s . co m } HttpDataFrame[] httpDataFrames = new HttpDataFrame[count]; for (int i = 0; i < count; i++) { int dataSize = Math.min(content.readableBytes(), MAX_DATA_LENGTH); HttpDataFrame httpDataFrame = new DefaultHttpDataFrame(streamId, content.readSlice(dataSize)); httpDataFrames[i] = httpDataFrame; } return httpDataFrames; }
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static int writeArg(ByteBufAllocator allocator, ByteBuf arg, int writableBytes, List<ByteBuf> bufs) { if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { throw new UnsupportedOperationException( "writableBytes must be larger than " + TFrame.FRAME_SIZE_LENGTH); }// w w w . j a va 2 s. co m int readableBytes = arg.readableBytes(); int headerSize = TFrame.FRAME_SIZE_LENGTH; int chunkLength = Math.min(readableBytes + headerSize, writableBytes); ByteBuf sizeBuf = allocator.buffer(TFrame.FRAME_SIZE_LENGTH); bufs.add(sizeBuf); // Write the size of the `arg` sizeBuf.writeShort(chunkLength - headerSize); if (readableBytes == 0) { return TFrame.FRAME_SIZE_LENGTH; } else { bufs.add(arg.readSlice(chunkLength - headerSize).retain()); return chunkLength; } }
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static ByteBuf readArg(ByteBuf buffer) { if (buffer.readableBytes() < TFrame.FRAME_SIZE_LENGTH) { return null; }// w ww. j av a 2s.co m int len = buffer.readUnsignedShort(); if (len > buffer.readableBytes()) { throw new UnsupportedOperationException("wrong read index for args"); } else if (len == 0) { return Unpooled.EMPTY_BUFFER; } /* Read a slice, retain a copy */ ByteBuf arg = buffer.readSlice(len); arg.retain(); return arg; }
From source file:com.uber.tchannel.codecs.TFrameCodec.java
License:Open Source License
public static TFrame decode(ByteBuf buffer) { // size:2//from ww w . j a v a 2 s . co m 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); }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureChecker.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try {/*from ww w . j a va 2 s .co m*/ if (msg instanceof ByteBuf) { final ByteBuf in = (ByteBuf) msg; final int dataLength = in.readInt(); final ByteBuf data = in.readSlice(dataLength); final int signatureLength = in.readInt(); final byte[] signature = new byte[signatureLength]; in.readBytes(signature); verifySignature.update(data.nioBuffer()); final boolean valid = verifySignature.verify(signature); //Log.v(TAG, "Read " + dataLength + "b of data with " + signatureLength + "b " + // (valid ? "valid" : "invalid") + " signature" + // (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : "")); if (valid) { data.retain(); ctx.fireChannelRead(data); } else { throw new SignatureException("Message has a broken signature, closing connection"); } } else { throw new SignatureException("Can't check signature of message of type " + (msg != null ? msg.getClass() : "null") + ", closing connection"); } } catch (SignatureException | RuntimeException e) { ctx.close(); throw e; } }
From source file:divconq.bus.net.StreamDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if ((in instanceof EmptyByteBuf) || (in.readableBytes() == 0)) return;//from www . ja v a 2 s . c o m OperationContext.useHubContext(); Logger.trace("Decoding Stream Data: " + in.readableBytes()); switch (this.state) { case HEADER: { if (this.headerparser == null) { this.builder = new ObjectBuilder(); this.headerparser = new BufferToCompositeParser(this.builder); } this.headerparser.parseStruct(in); // if not done wait for more bytes if (!this.headerparser.isDone()) return; this.state = State.PAYLOAD_SIZE; // deliberate fall through } case PAYLOAD_SIZE: { if (in.readableBytes() < 4) return; this.size = in.readInt(); this.state = State.PAYLOAD; // deliberate fall through } case PAYLOAD: { // return here, without any state reset, means we need more before we can decide what to do if (in.readableBytes() < this.size) return; // we have enough data to send the message... StreamMessage msg = new StreamMessage(); // add Data only if there are some bytes, otherwise skip buffer allocation if (this.size > 0) { ByteBuf bb = in.readSlice(this.size); bb.retain(); msg.setData(bb); } msg.copyFields((RecordStruct) this.builder.getRoot()); out.add(msg); // set state to start over - ready to process next message this.headerparser = null; this.size = 0; this.state = State.HEADER; } } }
From source file:divconq.ctp.f.BlockCommand.java
License:Open Source License
@Override public boolean decode(ByteBuf in) { while (this.state != State.DONE) { switch (this.state) { case BLOCK_TYPE: { if (in.readableBytes() < 1) return false; this.blocktype = in.readUnsignedByte(); this.eof = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_EOF) != 0); this.skipHeaders = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_HEADER) == 0); this.skipPayload = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_CONTENT) == 0); // completely done, exit the loop and decode if (this.skipHeaders && this.skipPayload) { this.state = State.DONE; break; }/*ww w . j a v a 2s .com*/ // to skip headers, go back to loop if (this.skipHeaders) { this.state = State.STREAM_OFFSET; break; } this.state = State.HEADER_ATTR; // deliberate fall through } case HEADER_ATTR: { if (in.readableBytes() < 2) return false; this.currattr = in.readShort(); // done with headers, go back to loop to skip down to payload if (this.currattr == CtpConstants.CTP_F_ATTR_END) { if (this.skipPayload) this.state = State.DONE; else this.state = State.STREAM_OFFSET; break; } this.state = State.HEADER_SIZE; // deliberate fall through } case HEADER_SIZE: { if (in.readableBytes() < 2) return false; this.currasize = in.readShort(); // an empty attribute is like a flag - present but no data // go on to next header if (this.currasize == 0) { this.headers.put(this.currattr, new byte[0]); this.currattr = 0; this.state = State.HEADER_ATTR; break; } this.state = State.HEADER_VALUE; // deliberate fall through } case HEADER_VALUE: { if (in.readableBytes() < this.currasize) return false; byte[] val = new byte[this.currasize]; in.readBytes(val); this.headers.put(this.currattr, val); this.currattr = 0; this.currasize = 0; this.state = State.HEADER_ATTR; break; } case STREAM_OFFSET: { if (in.readableBytes() < 8) return false; this.streamOffset = in.readLong(); this.state = State.PAYLOAD_SIZE; // deliberate fall through } case PAYLOAD_SIZE: { if (in.readableBytes() < 3) return false; this.paysize = in.readMedium(); this.state = State.PAYLOAD; // deliberate fall through } case PAYLOAD: { // return here, without any state reset, means we need more before we can decide what to do if (in.readableBytes() < this.paysize) return false; // add Data only if there are some bytes, otherwise skip buffer allocation if (this.paysize > 0) { ByteBuf bb = in.readSlice(this.paysize); bb.retain(); this.data = bb; } this.state = State.DONE; // deliberate fall through } case DONE: { break; } } } return true; }