List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:Http2ClientConnectionHandler.java
License:Apache License
@Override public void onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream, boolean endOfSegment) throws Http2Exception { // Copy the data into the buffer. int available = data.readableBytes(); if (collectedData == null) { collectedData = ctx().alloc().buffer(available); collectedData.writeBytes(data, data.readerIndex(), data.readableBytes()); } else {/* w w w . j a v a2s .c om*/ // Expand the buffer ByteBuf newBuffer = ctx().alloc().buffer(collectedData.readableBytes() + available); newBuffer.writeBytes(collectedData); newBuffer.writeBytes(data); collectedData.release(); collectedData = newBuffer; } // If it's the last frame, print the complete message. if (endOfStream) { // System.out.println("Received message: " + collectedData.toString(CharsetUtil.UTF_8)); final OutstandingRequest outstandingRequest = outstanding.remove(streamId); outstandingRequest.finish(collectedData); // Free the data buffer. collectedData.release(); collectedData = null; } }
From source file:HttpUploadServerHandler.java
License:Apache License
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0);//from w w w . j a va 2 s.com // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION)); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().set(CONTENT_LENGTH, String.valueOf(buf.readableBytes())); } Set<Cookie> cookies; String value = request.headers().get(COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = CookieDecoder.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } // Write the response. ChannelFuture future = channel.write(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:HttpUploadServerHandler.java
License:Apache License
private void writeMenu(ChannelHandlerContext ctx) { // print several HTML forms // Convert the response content to a ChannelBuffer. responseContent.setLength(0);/*from w w w.j av a2s . c o m*/ // create Pseudo Menu responseContent.append("<html>"); responseContent.append("<head>"); responseContent.append("<title>Netty Test Form</title>\r\n"); responseContent.append("</head>\r\n"); responseContent.append("<body bgcolor=white><style>td{font-size: 12pt;}</style>"); responseContent.append("<table border=\"0\">"); responseContent.append("<tr>"); responseContent.append("<td>"); responseContent.append("<h1>Netty Test Form</h1>"); responseContent.append("Choose one FORM"); responseContent.append("</td>"); responseContent.append("</tr>"); responseContent.append("</table>\r\n"); // GET responseContent.append("<CENTER>GET FORM<HR WIDTH=\"75%\" NOSHADE color=\"blue\"></CENTER>"); responseContent.append("<FORM ACTION=\"/formget\" METHOD=\"GET\">"); responseContent.append("<input type=hidden name=getform value=\"GET\">"); responseContent.append("<table border=\"0\">"); responseContent.append("<tr><td>Fill with value: <br> <input type=text name=\"info\" size=10></td></tr>"); responseContent.append("<tr><td>Fill with value: <br> <input type=text name=\"secondinfo\" size=20>"); responseContent .append("<tr><td>Fill with value: <br> <textarea name=\"thirdinfo\" cols=40 rows=10></textarea>"); responseContent.append("</td></tr>"); responseContent.append("<tr><td><INPUT TYPE=\"submit\" NAME=\"Send\" VALUE=\"Send\"></INPUT></td>"); responseContent.append("<td><INPUT TYPE=\"reset\" NAME=\"Clear\" VALUE=\"Clear\" ></INPUT></td></tr>"); responseContent.append("</table></FORM>\r\n"); responseContent.append("<CENTER><HR WIDTH=\"75%\" NOSHADE color=\"blue\"></CENTER>"); // POST responseContent.append("<CENTER>POST FORM<HR WIDTH=\"75%\" NOSHADE color=\"blue\"></CENTER>"); responseContent.append("<FORM ACTION=\"/formpost\" METHOD=\"POST\">"); responseContent.append("<input type=hidden name=getform value=\"POST\">"); responseContent.append("<table border=\"0\">"); responseContent.append("<tr><td>Fill with value: <br> <input type=text name=\"info\" size=10></td></tr>"); responseContent.append("<tr><td>Fill with value: <br> <input type=text name=\"secondinfo\" size=20>"); responseContent .append("<tr><td>Fill with value: <br> <textarea name=\"thirdinfo\" cols=40 rows=10></textarea>"); responseContent.append("<tr><td>Fill with file (only file name will be transmitted): <br> " + "<input type=file name=\"myfile\">"); responseContent.append("</td></tr>"); responseContent.append("<tr><td><INPUT TYPE=\"submit\" NAME=\"Send\" VALUE=\"Send\"></INPUT></td>"); responseContent.append("<td><INPUT TYPE=\"reset\" NAME=\"Clear\" VALUE=\"Clear\" ></INPUT></td></tr>"); responseContent.append("</table></FORM>\r\n"); responseContent.append("<CENTER><HR WIDTH=\"75%\" NOSHADE color=\"blue\"></CENTER>"); // POST with enctype="multipart/form-data" responseContent.append("<CENTER>POST MULTIPART FORM<HR WIDTH=\"75%\" NOSHADE color=\"blue\"></CENTER>"); responseContent .append("<FORM ACTION=\"/formpostmultipart\" ENCTYPE=\"multipart/form-data\" METHOD=\"POST\">"); responseContent.append("<input type=hidden name=getform value=\"POST\">"); responseContent.append("<table border=\"0\">"); responseContent.append("<tr><td>Fill with value: <br> <input type=text name=\"info\" size=10></td></tr>"); responseContent.append("<tr><td>Fill with value: <br> <input type=text name=\"secondinfo\" size=20>"); responseContent .append("<tr><td>Fill with value: <br> <textarea name=\"thirdinfo\" cols=40 rows=10></textarea>"); responseContent.append("<tr><td>Fill with file: <br> <input type=file name=\"myfile\">"); responseContent.append("</td></tr>"); responseContent.append("<tr><td><INPUT TYPE=\"submit\" NAME=\"Send\" VALUE=\"Send\"></INPUT></td>"); responseContent.append("<td><INPUT TYPE=\"reset\" NAME=\"Clear\" VALUE=\"Clear\" ></INPUT></td></tr>"); responseContent.append("</table></FORM>\r\n"); responseContent.append("<CENTER><HR WIDTH=\"75%\" NOSHADE color=\"blue\"></CENTER>"); responseContent.append("</body>"); responseContent.append("</html>"); ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); response.headers().set(CONTENT_LENGTH, String.valueOf(buf.readableBytes())); // Write the response. ctx.channel().write(response); }
From source file:alluxio.client.block.stream.GrpcDataReaderTest.java
License:Apache License
/** * Reads the chunks from the given {@link DataReader}. * * @param reader the data reader//from w w w . j ava 2s .c o m * @param checksumStart the start position to calculate the checksum * @param bytesToRead bytes to read * @return the checksum of the data read starting from checksumStart */ private long checkChunks(DataReader reader, long checksumStart, long bytesToRead) throws Exception { long pos = 0; long checksum = 0; while (true) { DataBuffer chunk = reader.readChunk(); if (chunk == null) { break; } try { assertTrue(chunk instanceof NioDataBuffer); ByteBuf buf = (ByteBuf) chunk.getNettyOutput(); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); for (int i = 0; i < bytes.length; i++) { if (pos >= checksumStart) { checksum += BufferUtils.byteToInt(bytes[i]); } pos++; if (pos >= bytesToRead) { return checksum; } } } finally { chunk.release(); } } return checksum; }
From source file:alluxio.client.block.stream.GrpcDataWriter.java
License:Apache License
@Override public void writeChunk(final ByteBuf buf) throws IOException { mPosToQueue += buf.readableBytes(); try {/*from w w w. j a va 2 s . c o m*/ WriteRequest request = WriteRequest.newBuilder().setCommand(mPartialRequest) .setChunk(Chunk.newBuilder().setData(UnsafeByteOperations.unsafeWrap(buf.nioBuffer())).build()) .build(); if (mStream instanceof GrpcDataMessageBlockingStream) { ((GrpcDataMessageBlockingStream<WriteRequest, WriteResponse>) mStream) .sendDataMessage(new DataMessage<>(request, new NettyDataBuffer(buf)), mDataTimeoutMs); } else { mStream.send(request, mDataTimeoutMs); } } finally { buf.release(); } }
From source file:alluxio.client.block.stream.LocalFileDataWriter.java
License:Apache License
@Override public void writeChunk(final ByteBuf buf) throws IOException { try {/*w ww.ja v a 2s. c om*/ Preconditions.checkState(!mStream.isCanceled() && !mStream.isClosed(), "DataWriter is closed while writing chunks."); int sz = buf.readableBytes(); ensureReserved(mPos + sz); mPos += sz; Preconditions.checkState(mWriter.append(buf) == sz); } finally { buf.release(); } }
From source file:alluxio.client.block.stream.LocalFilePacketWriter.java
License:Apache License
@Override public void writePacket(final ByteBuf buf) throws IOException { try {//www . j av a 2 s .co m Preconditions.checkState(!mClosed, "PacketWriter is closed while writing packets."); int sz = buf.readableBytes(); ensureReserved(mPos + sz); mPos += sz; Preconditions.checkState(buf.readBytes(mWriter.getChannel(), sz) == sz); } finally { buf.release(); } }
From source file:alluxio.client.block.stream.NettyPacketReader.java
License:Apache License
@Override public DataBuffer readPacket() throws IOException { Preconditions.checkState(!mClosed, "PacketReader is closed while reading packets."); ByteBuf buf; // TODO(peis): Have a better criteria to resume so that we can have fewer state changes. if (!tooManyPacketsPending()) { NettyUtils.enableAutoRead(mChannel); }// w w w . j a va 2 s . c o m try { buf = mPackets.poll(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } if (buf == null) { throw new IOException(String.format("Timeout to read %d from %s.", mId, mChannel.toString())); } if (buf == THROWABLE) { throw CommonUtils.castToIOException(Preconditions.checkNotNull(mPacketReaderException)); } if (buf == EOF_OR_CANCELLED) { mDone = true; return null; } mPosToRead += buf.readableBytes(); Preconditions.checkState(mPosToRead - mStart <= mBytesToRead); return new DataNettyBufferV2(buf); }
From source file:alluxio.client.block.stream.NettyPacketReaderTest.java
License:Apache License
/** * Reads the packets from the given {@link PacketReader}. * * @param reader the packet reader/*from w w w.j av a 2 s. c o m*/ * @param checksumStart the start position to calculate the checksum * @param bytesToRead bytes to read * @return the checksum of the data read starting from checksumStart */ private long checkPackets(PacketReader reader, long checksumStart, long bytesToRead) throws Exception { long pos = 0; long checksum = 0; while (true) { DataBuffer packet = reader.readPacket(); if (packet == null) { break; } try { Assert.assertTrue(packet instanceof DataNettyBufferV2); ByteBuf buf = (ByteBuf) packet.getNettyOutput(); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); for (int i = 0; i < bytes.length; i++) { if (pos >= checksumStart) { checksum += BufferUtils.byteToInt(bytes[i]); } pos++; if (pos >= bytesToRead) { return checksum; } } } finally { packet.release(); } } return checksum; }
From source file:alluxio.client.block.stream.NettyPacketWriter.java
License:Apache License
@Override public void writePacket(final ByteBuf buf) throws IOException { final long len; final long offset; try (LockResource lr = new LockResource(mLock)) { Preconditions.checkState(!mClosed && !mEOFSent && !mCancelSent); Preconditions.checkArgument(buf.readableBytes() <= PACKET_SIZE); while (true) { if (mPacketWriteException != null) { throw new IOException(mPacketWriteException); }/* ww w . j a v a2s .c o m*/ if (!tooManyPacketsInFlight()) { offset = mPosToQueue; mPosToQueue += buf.readableBytes(); len = buf.readableBytes(); break; } try { if (!mBufferNotFullOrFailed.await(WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { throw new IOException(String.format("Timeout to write packet to %d @ %s.", mId, mAddress)); } } catch (InterruptedException e) { throw Throwables.propagate(e); } } } catch (Throwable e) { buf.release(); throw e; } Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(mId).setOffset(offset) .setSessionId(mSessionId).setTier(mTier).setType(mRequestType).build(); DataBuffer dataBuffer = new DataNettyBufferV2(buf); mChannel.writeAndFlush(new RPCProtoMessage(new ProtoMessage(writeRequest), dataBuffer)) .addListener(new WriteListener(offset + len)); }