List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract ByteBuf writerIndex(int writerIndex);
From source file:divconq.api.internal.UploadStream.java
License:Open Source License
@Override public ByteBuf getChunk(int length) throws IOException { if (this.in == null || length == 0) return Unpooled.EMPTY_BUFFER; // indicate that we are keeping busy and not hung this.ops.touch(); //System.out.println("Get last activity after touch: " + this.ops.getLastActivity()); int read = 0; ByteBuffer byteBuffer = ByteBuffer.allocate(length); while (read < length) { int readnow = this.in.read(byteBuffer); if (readnow == -1) { this.in.close(); this.in = null; break; } else {/*from ww w . j ava 2 s. c o m*/ read += readnow; } } if (read == 0) return Unpooled.EMPTY_BUFFER; byteBuffer.flip(); ByteBuf buffer = Unpooled.wrappedBuffer(byteBuffer); buffer.readerIndex(0); buffer.writerIndex(read); return buffer; }
From source file:divconq.ctp.stream.FileSourceStream.java
License:Open Source License
public void readLocalFile() { FileSystemFile fs = (FileSystemFile) this.current; if (this.in == null) { this.insize = fs.getSize(); // As a source we are responsible for progress tracking OperationContext.get().setAmountCompleted(0); try {/*from w ww.j a va2 s.co m*/ this.in = FileChannel.open(fs.localPath(), StandardOpenOption.READ); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Unable to read source file " + x); return; } } while (true) { // TODO sizing? ByteBuf data = Hub.instance.getBufferAllocator().heapBuffer(32768); ByteBuffer buffer = ByteBuffer.wrap(data.array(), data.arrayOffset(), data.capacity()); int pos = -1; try { pos = (int) this.in.read(buffer); } catch (IOException x1) { OperationContext.get().getTaskRun().kill("Problem reading source file: " + x1); data.release(); return; } FileDescriptor fref = FileDescriptor.fromFileStore(this.current); fref.setPath(this.current.path().subpath(this.source.path())); System.out.println("writing: " + fref.getPath() + " from: " + this.inprog); if (pos == -1) { try { this.in.close(); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Problem closing source file: " + x); data.release(); return; } OperationContext.get().setAmountCompleted(100); fref.setEof(true); this.current = null; this.in = null; this.insize = 0; this.inprog = 0; } else { this.inprog += pos; data.writerIndex(pos); OperationContext.get().setAmountCompleted((int) (this.inprog * 100 / this.insize)); } if (this.downstream.handle(fref, data) != ReturnOption.CONTINUE) break; if (this.current == null) { // we need the next file OperationContext.get().getTaskRun().resume(); // wait on the implied request break; } } }
From source file:divconq.http.multipart.AbstractDiskHttpData.java
License:Apache License
@Override public ByteBuf getChunk(int length) throws IOException { if (file == null || length == 0) { return EMPTY_BUFFER; }/*from w ww. ja v a2s . c o m*/ if (fileChannel == null) { FileInputStream inputStream = new FileInputStream(file); fileChannel = inputStream.getChannel(); } int read = 0; ByteBuffer byteBuffer = ByteBuffer.allocate(length); while (read < length) { int readnow = fileChannel.read(byteBuffer); if (readnow == -1) { fileChannel.close(); fileChannel = null; break; } else { read += readnow; } } if (read == 0) { return EMPTY_BUFFER; } byteBuffer.flip(); ByteBuf buffer = wrappedBuffer(byteBuffer); buffer.readerIndex(0); buffer.writerIndex(read); return buffer; }
From source file:io.grpc.alts.internal.AltsProtocolNegotiatorTest.java
License:Apache License
@Test public void unprotectLargeIncomingFrame() throws Exception { // We use a server frameprotector with twice the standard frame size. int serverFrameSize = 4096 * 2; // This should fit into one frame. byte[] unprotectedBytes = new byte[serverFrameSize - 500]; Arrays.fill(unprotectedBytes, (byte) 7); ByteBuf unprotectedData = Unpooled.wrappedBuffer(unprotectedBytes); unprotectedData.writerIndex(unprotectedBytes.length); // Perform handshake. doHandshake();//from ww w .j a va 2 s .c om // Protect the message on the server. TsiFrameProtector serverProtector = serverHandshaker.createFrameProtector(serverFrameSize, channel.alloc()); serverProtector.protectFlush(Collections.singletonList(unprotectedData), new Consumer<ByteBuf>() { @Override public void accept(ByteBuf buf) { channel.writeInbound(buf); } }, channel.alloc()); channel.flushInbound(); // Read the protected message at the client and verify that it matches the original message. assertEquals(1, channel.inboundMessages().size()); ByteBuf receivedData1 = channel.readInbound(); int receivedLen1 = receivedData1.readableBytes(); byte[] receivedBytes = new byte[receivedLen1]; receivedData1.readBytes(receivedBytes, 0, receivedLen1); assertThat(unprotectedBytes.length).isEqualTo(receivedBytes.length); assertThat(unprotectedBytes).isEqualTo(receivedBytes); }
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@Test public void writableNioBuffers_worksWithNormal() { ByteBuf buf = alloc.buffer(1); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); buf.writerIndex(1); assertEquals('a', buf.readByte()); } finally {//ww w .j av a2 s . c om buf.release(); } }
From source file:io.grpc.alts.internal.NettyTsiHandshaker.java
License:Apache License
/** * Gets data that is ready to be sent to the to the remote peer. This should be called in a loop * until no bytes are written to the output buffer. * * @param out the buffer to receive the bytes. *///from w ww .ja v a2 s .c o m void getBytesToSendToPeer(ByteBuf out) throws GeneralSecurityException { checkState(unwrapper != null, "protector already created"); try (BufUnwrapper unwrapper = this.unwrapper) { // Write as many bytes as possible into the buffer. int bytesWritten = 0; for (ByteBuffer nioBuffer : unwrapper.writableNioBuffers(out)) { if (!nioBuffer.hasRemaining()) { // This buffer doesn't have any more space to write, go to the next buffer. continue; } int prevPos = nioBuffer.position(); internalHandshaker.getBytesToSendToPeer(nioBuffer); bytesWritten += nioBuffer.position() - prevPos; // If the buffer position was not changed, the frame has been completely read into the // buffers. if (nioBuffer.position() == prevPos) { break; } } out.writerIndex(out.writerIndex() + bytesWritten); } }
From source file:io.horizondb.client.MsgToByteEncoder.java
License:Apache License
/** * {@inheritDoc}//from w w w . j a v a 2s. c o m */ @Override protected void encode(ChannelHandlerContext ctx, Msg<?> msg, ByteBuf out) throws Exception { out.writerIndex(Buffers.wrap(out.capacity(msg.computeSerializedSize())).order(ByteOrder.LITTLE_ENDIAN) .writeObject(msg).writerIndex()); }
From source file:io.nodyn.buffer.Buffer.java
License:Apache License
public static Object fill(JSObject obj, Object val, int offset, int end) { int byteVal = 0; if (val instanceof Number) { byteVal = ((Number) val).intValue() & 0xFF; } else if (val instanceof String && !((String) val).isEmpty()) { byteVal = ((String) val).charAt(0); }/*from w w w . j av a2 s. com*/ ByteBuf b = extract(obj); for (int i = offset; i < end; ++i) { b.setByte(i, byteVal); } b.writerIndex(Math.max(b.writerIndex(), offset + end)); return obj; }
From source file:io.nodyn.fs.Fs.java
License:Apache License
public static int read(POSIX posix, int fd, ByteBuf buf, int offset, int length) { byte[] input = new byte[length]; int read = posix.read(fd, input, length); if (read != -1) { buf.setBytes(offset, input, 0, read); buf.writerIndex(Math.max(buf.writerIndex(), offset + read)); }// w w w. j ava 2s . c om return read; }
From source file:io.nodyn.fs.Fs.java
License:Apache License
public static int pread(POSIX posix, int fd, ByteBuf buf, int offset, int length, int position) { byte[] input = new byte[length]; int read = posix.pread(fd, input, length, position); if (read != -1) { buf.setBytes(offset, input, 0, read); buf.writerIndex(Math.max(buf.writerIndex(), offset + read)); }/*from www . j a va 2s. c o m*/ return read; }