List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:alluxio.worker.netty.DataServerBlockWriteHandler.java
License:Apache License
@Override protected void writeBuf(ByteBuf buf, long pos) throws Exception { if (mBytesReserved < pos) { long bytesToReserve = Math.max(FILE_BUFFER_SIZE, pos - mBytesReserved); // Allocate enough space in the existing temporary block for the write. mWorker.requestSpace(mRequest.mSessionId, mRequest.mId, bytesToReserve); mBytesReserved += bytesToReserve; }// w w w.jav a 2s. co m BlockWriter blockWriter = ((BlockWriteRequestInternal) mRequest).mBlockWriter; GatheringByteChannel outputChannel = blockWriter.getChannel(); int sz = buf.readableBytes(); Preconditions.checkState(buf.readBytes(outputChannel, sz) == sz); }
From source file:alluxio.worker.netty.DataServerReadHandlerTest.java
License:Apache License
/** * Checks all the read responses.// w w w . j av a2 s.co m */ protected void checkAllReadResponses(EmbeddedChannel channel, long checksumExpected) { boolean eof = false; long checksumActual = 0; while (!eof) { Object readResponse = waitForOneResponse(channel); if (readResponse == null) { Assert.fail(); break; } DataBuffer buffer = checkReadResponse(readResponse, Protocol.Status.Code.OK); eof = buffer == null; if (buffer != null) { if (buffer instanceof DataNettyBufferV2) { ByteBuf buf = (ByteBuf) buffer.getNettyOutput(); while (buf.readableBytes() > 0) { checksumActual += BufferUtils.byteToInt(buf.readByte()); } buf.release(); } else { Assert.assertTrue(buffer instanceof DataFileChannel); ByteBuffer buf = buffer.getReadOnlyByteBuffer(); byte[] array = new byte[buf.remaining()]; buf.get(array); for (int i = 0; i < array.length; i++) { checksumActual += BufferUtils.byteToInt(array[i]); } } } } Assert.assertEquals(checksumExpected, checksumActual); Assert.assertTrue(eof); }
From source file:alluxio.worker.netty.DataServerUFSFileReadHandler.java
License:Apache License
@Override protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException { ByteBuf buf = channel.alloc().buffer(len, len); try {/*from ww w. ja v a 2s .c o m*/ InputStream in = ((FileReadRequestInternal) mRequest).mInputStream; if (in != null) { // if we have not reached the end of the file while (buf.writableBytes() > 0 && buf.writeBytes(in, buf.writableBytes()) != -1) { } } if (buf.readableBytes() == 0) { buf.release(); return null; } return new DataNettyBufferV2(buf); } catch (Throwable e) { buf.release(); throw e; } }
From source file:alluxio.worker.netty.DataServerUFSFileWriteHandler.java
License:Apache License
@Override protected void writeBuf(ByteBuf buf, long pos) throws Exception { buf.readBytes(((FileWriteRequestInternal) mRequest).mOutputStream, buf.readableBytes()); }
From source file:alluxio.worker.netty.DataServerWriteHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception { if (!acceptMessage(object)) { ctx.fireChannelRead(object);// w w w. j a va2 s.c o m return; } RPCProtoMessage msg = (RPCProtoMessage) object; Protocol.WriteRequest writeRequest = msg.getMessage().getMessage(); // Only initialize (open the readers) if this is the first packet in the block/file. if (writeRequest.getOffset() == 0) { initializeRequest(msg); } // Validate msg and return error if invalid. Init variables if necessary. String error = validateRequest(msg); if (!error.isEmpty()) { pushAbortPacket(ctx.channel(), new Error(new IllegalArgumentException(error), true, Protocol.Status.Code.INVALID_ARGUMENT)); return; } try (LockResource lr = new LockResource(mLock)) { // If we have seen an error, return early and release the data. This can only // happen for those mis-behaving clients who first sends some invalid requests, then // then some random data. It can leak memory if we do not release buffers here. if (mError != null) { if (msg.getPayloadDataBuffer() != null) { msg.getPayloadDataBuffer().release(); } return; } ByteBuf buf; if (writeRequest.getEof()) { buf = EOF; } else if (writeRequest.getCancel()) { buf = CANCEL; } else { DataBuffer dataBuffer = msg.getPayloadDataBuffer(); Preconditions.checkState(dataBuffer != null && dataBuffer.getLength() > 0); assert dataBuffer.getNettyOutput() instanceof ByteBuf; buf = (ByteBuf) dataBuffer.getNettyOutput(); mPosToQueue += buf.readableBytes(); } if (!mPacketWriterActive) { mPacketWriterActive = true; mPacketWriterExecutor.submit(new PacketWriter(ctx.channel())); } mPackets.offer(buf); if (tooManyPacketsInFlight()) { NettyUtils.disableAutoRead(ctx.channel()); } } }
From source file:alluxio.worker.netty.ReadHandlerTest.java
License:Apache License
/** * Checks all the read responses.//from www . j a va 2 s. com */ protected void checkAllReadResponses(EmbeddedChannel channel, long checksumExpected) { boolean eof = false; long checksumActual = 0; while (!eof) { Object readResponse = waitForOneResponse(channel); if (readResponse == null) { Assert.fail(); break; } DataBuffer buffer = checkReadResponse(readResponse, PStatus.OK); eof = buffer == null; if (buffer != null) { if (buffer instanceof DataNettyBufferV2) { ByteBuf buf = (ByteBuf) buffer.getNettyOutput(); while (buf.readableBytes() > 0) { checksumActual += BufferUtils.byteToInt(buf.readByte()); } buf.release(); } else { Assert.assertTrue(buffer instanceof DataFileChannel); final ByteBuffer byteBuffer = ByteBuffer.allocate((int) buffer.getLength()); WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } @Override public int write(ByteBuffer src) throws IOException { int sz = src.remaining(); byteBuffer.put(src); return sz; } }; try { ((FileRegion) buffer.getNettyOutput()).transferTo(writableByteChannel, 0); } catch (IOException e) { Assert.fail(); } byteBuffer.flip(); while (byteBuffer.remaining() > 0) { checksumActual += BufferUtils.byteToInt(byteBuffer.get()); } } } } Assert.assertEquals(checksumExpected, checksumActual); Assert.assertTrue(eof); }
From source file:appeng.core.sync.AppEngPacket.java
License:Open Source License
protected void configureWrite(final ByteBuf data) { data.capacity(data.readableBytes()); this.p = new PacketBuffer(data); }
From source file:appeng.core.sync.packets.PacketCompressedNBT.java
License:Open Source License
public PacketCompressedNBT(final ByteBuf stream) throws IOException { this.data = null; this.compressFrame = null; final GZIPInputStream gzReader = new GZIPInputStream(new InputStream() { @Override//from w ww . j ava 2 s . com public int read() throws IOException { if (stream.readableBytes() <= 0) { return -1; } return stream.readByte() & 0xff; } }); final DataInputStream inStream = new DataInputStream(gzReader); this.in = CompressedStreamTools.read(inStream); inStream.close(); }
From source file:appeng.core.sync.packets.PacketMEInventoryUpdate.java
License:Open Source License
public PacketMEInventoryUpdate(final ByteBuf stream) throws IOException { this.data = null; this.compressFrame = null; this.list = new LinkedList<IAEItemStack>(); this.ref = stream.readByte(); // int originalBytes = stream.readableBytes(); final GZIPInputStream gzReader = new GZIPInputStream(new InputStream() { @Override//w ww .j a va 2 s . c om public int read() throws IOException { if (stream.readableBytes() <= 0) { return -1; } return stream.readByte() & STREAM_MASK; } }); final ByteBuf uncompressed = Unpooled.buffer(stream.readableBytes()); final byte[] tmp = new byte[TEMP_BUFFER_SIZE]; while (gzReader.available() != 0) { final int bytes = gzReader.read(tmp); if (bytes > 0) { uncompressed.writeBytes(tmp, 0, bytes); } } gzReader.close(); // int uncompressedBytes = uncompressed.readableBytes(); // AELog.info( "Receiver: " + originalBytes + " -> " + uncompressedBytes ); while (uncompressed.readableBytes() > 0) { this.list.add(AEItemStack.loadItemStackFromPacket(uncompressed)); } this.empty = this.list.isEmpty(); }
From source file:appeng.core.sync.packets.PacketMEInventoryUpdate.java
License:Open Source License
public void appendItem(final IAEItemStack is) throws IOException, BufferOverflowException { final ByteBuf tmp = Unpooled.buffer(OPERATION_BYTE_LIMIT); is.writeToPacket(tmp);/* w w w.j a v a2s . com*/ this.compressFrame.flush(); if (this.writtenBytes + tmp.readableBytes() > UNCOMPRESSED_PACKET_BYTE_LIMIT) { throw new BufferOverflowException(); } else { this.writtenBytes += tmp.readableBytes(); this.compressFrame.write(tmp.array(), 0, tmp.readableBytes()); this.empty = false; } }