List of usage examples for io.netty.buffer ByteBuf copy
public abstract ByteBuf copy(int index, int length);
From source file:c5db.client.codec.WebsocketProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext channelHandlerContext, Call call, List<Object> objects) throws Exception { final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(); Call.getSchema().writeTo(lcpo, call); final long size = lcpo.buffer.size(); if (size < MAX_SIZE) { ByteBuf byteBuf = channelHandlerContext.alloc().buffer((int) size); lcpo.buffer.finish().stream().forEach(byteBuf::writeBytes); final BinaryWebSocketFrame frame = new BinaryWebSocketFrame(byteBuf); objects.add(frame);/*from w w w . j av a 2 s . c om*/ } else { long remaining = size; boolean first = true; ByteBuf byteBuf = channelHandlerContext.alloc().buffer((int) size); lcpo.buffer.finish().stream().forEach(byteBuf::writeBytes); while (remaining > 0) { WebSocketFrame frame; if (remaining > MAX_SIZE) { final ByteBuf slice = byteBuf.copy((int) (size - remaining), (int) MAX_SIZE); if (first) { frame = new BinaryWebSocketFrame(false, 0, slice); first = false; } else { frame = new ContinuationWebSocketFrame(false, 0, slice); } remaining -= MAX_SIZE; } else { frame = new ContinuationWebSocketFrame(true, 0, byteBuf.copy((int) (size - remaining), (int) remaining)); remaining = 0; } objects.add(frame); } } }
From source file:c5db.codec.WebsocketProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext channelHandlerContext, Response response, List<Object> objects) throws IOException { final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(); Response.getSchema().writeTo(lcpo, response); final long size = lcpo.buffer.size(); ByteBuf byteBuf = channelHandlerContext.alloc().buffer((int) size); lcpo.buffer.finish().stream().forEach(byteBuf::writeBytes); if (size < MAX_SIZE) { final BinaryWebSocketFrame frame = new BinaryWebSocketFrame(byteBuf); objects.add(frame);// ww w . j a v a 2 s. c om } else { long remaining = size; boolean first = true; while (remaining > 0) { WebSocketFrame frame; if (remaining > MAX_SIZE) { final ByteBuf slice = byteBuf.copy((int) (size - remaining), (int) MAX_SIZE); if (first) { frame = new BinaryWebSocketFrame(false, 0, slice); first = false; } else { frame = new ContinuationWebSocketFrame(false, 0, slice); } remaining -= MAX_SIZE; } else { frame = new ContinuationWebSocketFrame(true, 0, byteBuf.copy((int) (size - remaining), (int) remaining)); remaining = 0; } objects.add(frame); } } }
From source file:com.facebook.nifty.core.ThriftFrameDecoder.java
License:Apache License
protected ByteBuf extractFrame(ByteBuf buffer, int index, int length) { // Slice should be sufficient here (and avoids the copy in LengthFieldBasedFrameDecoder) // because we know no one is going to modify the contents in the read buffers. //return buffer.slice(index, length).retain(); return buffer.copy(index, length); }
From source file:com.l2jmobius.commons.network.codecs.CryptCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { in.resetReaderIndex();//from w w w . java 2 s. co m _crypt.decrypt(in); in.readerIndex(in.writerIndex()); out.add(in.copy(0, in.writerIndex())); }
From source file:divconq.ctp.stream.FunnelStream.java
License:Open Source License
public ReturnOption nextMessage() { FileDescriptor curr = this.current; if (curr == null) return ReturnOption.CONTINUE; FileDescriptor blk = new FileDescriptor(); blk.copyAttributes(curr);//from w ww . ja va2s . c o m ByteBuf payload = this.currbuf; if ((payload != null) && payload.isReadable()) { int ramt = Math.min(this.aperture, payload.readableBytes()); ByteBuf pslice = payload.copy(payload.readerIndex(), ramt); payload.skipBytes(ramt); // TODO blk.payloadoffset = 0; blk.setEof(!payload.isReadable() && curr.isEof()); if (blk.isEof()) { payload.release(); this.current = null; this.currbuf = null; } payload = pslice; } else { blk.setEof(curr.isEof()); if (payload != null) payload.release(); payload = null; this.current = null; this.currbuf = null; } // current has been sent at least once this.relayed = true; return this.downstream.handle(blk, payload); }
From source file:divconq.ctp.stream.SplitStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); ByteBuf in = data; if (in != null) { while (in.isReadable()) { int amt = Math.min(in.readableBytes(), this.size - this.currchunk); ByteBuf out = in.copy(in.readerIndex(), amt); in.skipBytes(amt);//from w w w . j av a2 s. c om this.currchunk += amt; boolean eof = (this.currchunk == this.size) || (!in.isReadable() && file.isEof()); this.nextMessage(out, file, eof); if (eof) { this.seqnum++; this.currchunk = 0; } } in.release(); } else if (file.isEof()) { this.nextMessage(null, file, false); } // write all messages in the queue while (this.outlist.size() > 0) { ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0)); if (ret != ReturnOption.CONTINUE) return ret; } return ReturnOption.CONTINUE; }
From source file:divconq.ctp.stream.UntarStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); ByteBuf in = data; if (in != null) { while (in.isReadable()) { switch (this.tstate) { case RECORD: // starting a new record if (in.readableBytes() < TarConstants.DEFAULT_RCDSIZE - this.partialLength) { int offset = this.partialLength; this.partialLength += in.readableBytes(); in.readBytes(this.header_buffer, offset, in.readableBytes()); continue; }/*from ww w .j a v a 2s .c o m*/ in.readBytes(this.header_buffer, this.partialLength, TarConstants.DEFAULT_RCDSIZE - this.partialLength); this.partialLength = 0; //in.readBytes(this.header_buffer, 0, this.header_buffer.length); boolean hasHitEOF = this.isEOFRecord(this.header_buffer); // if we hit this twice in a row we are at the end - however, source will send FINAL anyway so we don't really care if (hasHitEOF) { this.currEntry = null; continue; } try { this.currEntry = new TarArchiveEntry(this.header_buffer, this.encoding); } catch (Exception x) { OperationContext.get().getTaskRun().kill("Error detected parsing the header: " + x); in.release(); return ReturnOption.DONE; } this.tstate = UntarState.XTRAS; case XTRAS: if (!in.isReadable()) continue; // TODO support long names and such - see org.apache.commons.compress.archivers.tar.TarArchiveInputStream if (this.currEntry.isGNULongLinkEntry()) { /* byte[] longLinkData = getLongNameData(); if (longLinkData == null) { // Bugzilla: 40334 // Malformed tar file - long link entry name not followed by // entry return null; } currEntry.setLinkName(encoding.decode(longLinkData)); */ OperationContext.get().getTaskRun().kill("long link currently not supported"); in.release(); return ReturnOption.DONE; } if (this.currEntry.isGNULongNameEntry()) { /* byte[] longNameData = getLongNameData(); if (longNameData == null) { // Bugzilla: 40334 // Malformed tar file - long entry name not followed by // entry return null; } currEntry.setName(encoding.decode(longNameData)); */ OperationContext.get().getTaskRun().kill("long name currently not supported"); in.release(); return ReturnOption.DONE; } if (this.currEntry.isPaxHeader()) { // Process Pax headers /* paxHeaders(); */ OperationContext.get().getTaskRun().kill("pax currently not supported"); in.release(); return ReturnOption.DONE; } if (this.currEntry.isGNUSparse()) { // Process sparse files /* readGNUSparse(); */ OperationContext.get().getTaskRun().kill("sparse currently not supported"); in.release(); return ReturnOption.DONE; } this.tstate = UntarState.PREP; case PREP: if (!in.isReadable()) continue; // TODO remove System.out.println("name: " + this.currEntry.getName()); System.out.println("size: " + this.currEntry.getSize()); System.out.println("modified: " + this.currEntry.getModTime()); // If the size of the next element in the archive has changed // due to a new size being reported in the posix header // information, we update entrySize here so that it contains // the correct value. long entrySize = this.currEntry.getSize(); this.remainContent = entrySize; long numRecords = (entrySize / this.header_buffer.length) + 1; this.remainSkip = (numRecords * this.header_buffer.length) - entrySize; // grab as much as we can from the current buffer int readSize = (int) Math.min(this.remainContent, in.readableBytes()); this.remainContent -= readSize; // handle empty files too if ((readSize > 0) || (this.remainContent == 0)) { System.out.println("reading content: " + readSize); ByteBuf out = in.copy(in.readerIndex(), readSize); int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize); this.remainSkip -= skipSize; in.skipBytes(readSize + skipSize); this.nextMessage(out); } this.tstate = UntarState.CONTENT; case CONTENT: if (!in.isReadable()) continue; // check if there is still content left in the entry we were last reading from if (this.remainContent > 0) { readSize = (int) Math.min(this.remainContent, in.readableBytes()); this.remainContent -= readSize; //System.out.println("reading content: " + readSize); //ByteBuf out = Hub.instance.getBufferAllocator().heapBuffer((int) readSize); ByteBuf out = in.copy(in.readerIndex(), readSize); int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize); this.remainSkip -= skipSize; //System.out.println("skipping content: " + skipSize); in.skipBytes(readSize + skipSize); this.nextMessage(out); } if (this.remainContent > 0) continue; this.currEntry = null; this.tstate = UntarState.SKIP; case SKIP: if (!in.isReadable()) continue; // check if there is still padding left in the entry we were last reading from if (this.remainSkip > 0) { int skipSize = (int) Math.min(this.remainSkip, in.readableBytes()); this.remainSkip -= skipSize; //System.out.println("skipping content: " + skipSize); in.skipBytes((int) skipSize); } if (this.remainSkip > 0) continue; this.tstate = UntarState.RECORD; } } in.release(); } // write all messages in the queue while (this.outlist.size() > 0) { ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0)); if (ret != ReturnOption.CONTINUE) return ret; } return ReturnOption.CONTINUE; }
From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java
License:Apache License
private static Buffer binaryDecodeBYTEA(int index, int len, ByteBuf buff) { return Buffer.buffer(buff.copy(index, len)); }
From source file:io.scalecube.socketio.serialization.PacketDecoder.java
License:Apache License
public static Packet decodePacket(final ByteBuf payload) throws IOException { int payloadSize = payload.readableBytes(); // Decode packet type int typeDelimiterIndex = payload.forEachByte(packetDelimiterFinder); if (typeDelimiterIndex == -1) { return Packet.NULL_INSTANCE; }//w w w. j ava2 s . c o m ByteBuf typeBytes = payload.slice(0, typeDelimiterIndex); String typeString = typeBytes.toString(CharsetUtil.UTF_8); int typeId = Integer.valueOf(typeString); PacketType type = PacketType.valueOf(typeId); // Skip message id int messageIdDelimiterIndex = payload.forEachByte(typeDelimiterIndex + 1, payloadSize - typeDelimiterIndex - 1, packetDelimiterFinder); if (messageIdDelimiterIndex == -1) { return Packet.NULL_INSTANCE; } // Skip endpoint int endpointDelimiterIndex = payload.forEachByte(messageIdDelimiterIndex + 1, payloadSize - messageIdDelimiterIndex - 1, packetDelimiterFinder); // Create instance of packet Packet packet = new Packet(type); // Decode data boolean messagingType = type == PacketType.MESSAGE || type == PacketType.JSON; if (endpointDelimiterIndex != -1 && messagingType) { int dataLength = payloadSize - endpointDelimiterIndex - 1; if (dataLength > 0) { ByteBuf data = payload.copy(endpointDelimiterIndex + 1, dataLength); packet.setData(data); } } return packet; }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testHttp1xOrH2CHandlerFragmentedHttp2Request() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler()); ByteBuf expected = Unpooled.copiedBuffer(Http1xOrH2CHandler.HTTP_2_PREFACE, StandardCharsets.UTF_8); ByteBuf buff = expected.copy(0, 1); ch.writeInbound(buff);//w w w . j a v a 2 s. c o m assertEquals(0, buff.refCnt()); assertEquals(0, ch.outboundMessages().size()); buff = expected.copy(1, expected.readableBytes() - 1); ch.writeInbound(buff); assertEquals(0, buff.refCnt()); assertEquals(1, ch.outboundMessages().size()); ByteBuf res = (ByteBuf) ch.outboundMessages().poll(); assertEquals(1, res.refCnt()); assertEquals(Http1xOrH2CHandler.HTTP_2_PREFACE, res.toString(StandardCharsets.UTF_8)); assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class)); }