List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:io.vertx.core.http.impl.Http2ConnectionBase.java
License:Open Source License
/** * Return a buffer from HTTP/2 codec that Vert.x can use: * * - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version * - if it's a composite buffer we do the same * - otherwise we increase the ref count *///from w w w .j a v a 2 s.c om static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; } if (buf.isDirect() || buf instanceof CompositeByteBuf) { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } return buf.retain(); }
From source file:io.vertx.core.http.impl.HttpServerResponseImpl.java
License:Open Source License
private void end0(ByteBuf data) { checkWritten();/*from ww w.j a va 2 s.c o m*/ bytesWritten += data.readableBytes(); if (!headWritten) { // if the head was not written yet we can write out everything in one go // which is cheaper. prepareHeaders(); FullHttpResponse resp; if (trailing != null) { resp = new AssembledFullHttpResponse(response, data, trailing.trailingHeaders(), trailing.getDecoderResult()); } else { resp = new AssembledFullHttpResponse(response, data); } channelFuture = conn.writeToChannel(resp); } else { if (!data.isReadable()) { if (trailing == null) { channelFuture = conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT); } else { channelFuture = conn.writeToChannel(trailing); } } else { LastHttpContent content; if (trailing != null) { content = new AssembledLastHttpContent(data, trailing.trailingHeaders(), trailing.getDecoderResult()); } else { content = new DefaultLastHttpContent(data, false); } channelFuture = conn.writeToChannel(content); } } if (!keepAlive) { closeConnAfterWrite(); closed = true; } written = true; conn.responseComplete(); if (bodyEndHandler != null) { bodyEndHandler.handle(null); } if (endHandler != null) { endHandler.handle(null); } }
From source file:io.vertx.core.net.impl.VertxHandler.java
License:Open Source License
public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; }//from ww w . jav a 2 s . co m if (buf.isDirect() || buf instanceof CompositeByteBuf) { try { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } finally { buf.release(); } } return buf; }
From source file:io.vertx.proton.impl.ProtonTransport.java
License:Apache License
private void pumpInbound(Buffer buffer) { if (failed) { LOG.trace("Skipping processing of data following transport error: {0}", buffer); return;//from ww w . ja v a2s . c om } // Lets push bytes from vert.x to proton engine. try { ByteBuf data = buffer.getByteBuf(); do { ByteBuffer transportBuffer = transport.tail(); int amount = Math.min(transportBuffer.remaining(), data.readableBytes()); transportBuffer.limit(transportBuffer.position() + amount); data.readBytes(transportBuffer); transport.process(); } while (data.isReadable()); } catch (Exception te) { failed = true; LOG.trace("Exception while processing transport input", te); } }
From source file:itlab.teleport.HttpServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws SQLException, IOException { if (msg instanceof FullHttpRequest) { HttpRequest req = (HttpRequest) msg; final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req);/* w w w .j a v a 2s . co m*/ List<InterfaceHttpData> data = decoder.getBodyHttpDatas(); for (InterfaceHttpData entry : data) { Attribute attr = (Attribute) entry; try { System.out.println(String.format("name: %s value: %s", attr.getName(), attr.getValue())); } catch (IOException e) { e.printStackTrace(); } } } if (msg instanceof HttpContent) { String json_input = ""; HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { json_input = content.toString(CharsetUtil.UTF_8); buf.append("CONTENT: "); buf.append(content.toString(CharsetUtil.UTF_8)); buf.append("\r\n"); } if (msg instanceof LastHttpContent) { buf.append("END OF CONTENT\r\n"); LastHttpContent trailer = (LastHttpContent) msg; if (!trailer.trailingHeaders().isEmpty()) { buf.append("\r\n"); for (CharSequence name : trailer.trailingHeaders().names()) { for (CharSequence value : trailer.trailingHeaders().getAll(name)) { buf.append("TRAILING HEADER: "); buf.append(name).append(" = ").append(value).append("\r\n"); } } buf.append("\r\n"); } System.out.println(buf); try { Statement st = BD_Connect.c.createStatement();// ? ResultSet rs = st.executeQuery("Select 1"); } catch (Exception e) { new BD_Connect().BD_Connection_open(); // } String json_output = new JSON_Handler().Parse_JSON(json_input); // JSON ? ByteBuf response_content = Unpooled.wrappedBuffer(json_output.getBytes(CharsetUtil.UTF_8)); HttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, OK, response_content); ctx.writeAndFlush(resp); ctx.close(); new BD_Connect().BD_Connection_close(); // ?? ? } } }
From source file:jmeter.plugins.http2.sampler.HttpResponseHandler.java
License:Apache License
@Override protected void messageReceived(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception { Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text()); if (streamId == null) { System.err.println("HttpResponseHandler unexpected message received: " + msg); return;/*from www .j ava 2 s.c o m*/ } ChannelPromise promise = streamidPromiseMap.get(streamId); if (promise == null) { System.err.println("Message received for unknown stream id " + streamId); } else { // Do stuff with the message (for now just print it) ByteBuf content = msg.content(); if (content.isReadable()) { int contentLength = content.readableBytes(); byte[] arr = new byte[contentLength]; content.readBytes(arr); System.out.println(new String(arr, 0, contentLength, CharsetUtil.UTF_8)); } promise.setSuccess(); // Set result streamidResponseMap.put(streamId, msg); } }
From source file:lesson.discard.DiscardServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; try {//from w w w . jav a2s . c o m while (in.isReadable()) { // (1) System.out.print((char) in.readByte()); System.out.flush(); } } finally { ReferenceCountUtil.release(msg); // (2) } }
From source file:lesson.echo.DiscardServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; try {//from www . j a va2 s.com while (in.isReadable()) { System.out.print((char) in.readByte()); System.out.flush(); } ctx.write(msg); //ctx.flush(); } finally { ReferenceCountUtil.release(msg); } }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveResponseEncoderTest.java
License:Open Source License
@Test public void test() { ModbusSlaveResponseEncoder encoder = new ModbusSlaveResponseEncoder(); EmbeddedChannel channel = new EmbeddedChannel(encoder); ModbusResponse response = new ModbusResponse() { {//from ww w .j a va2 s. c o m transectionId = 0xabcd; unitId = 0x83; } @Override public void writePduCore(ByteBuf out) { out.writeShort(0xabcd); out.writeShort(0xcdef); transectionId++; } @Override public int getPduCoreLength() { return 4; } @Override public int getFunctionCode() { // TODO Auto-generated method stub return 6; } }; ByteBuf buf = null; for (int i = 0; i < 3; i++) { channel.writeOutbound(response); buf = (ByteBuf) channel.readOutbound(); int[] buffer = new int[] { 0xab, 0xcd + i, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef }; assertEquals(buffer.length, buf.readableBytes()); for (int b : buffer) { assertEquals(b, buf.readUnsignedByte()); } assertTrue(!buf.isReadable()); ReferenceCountUtil.release(buf); } for (int i = 0; i < 3; i++) { channel.writeOutbound(response); } for (int i = 0; i < 3; i++) { buf = (ByteBuf) channel.readOutbound(); int[] buffer = new int[] { 0xab, 0xcd + i + 3, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef }; assertEquals(buffer.length, buf.readableBytes()); for (int b : buffer) { assertEquals(b, buf.readUnsignedByte()); } assertTrue(!buf.isReadable()); ReferenceCountUtil.release(buf); } encoder.setWithCheckSum(true); for (int i = 0; i < 3; i++) { channel.writeOutbound(response); } for (int i = 0; i < 3; i++) { buf = (ByteBuf) channel.readOutbound(); int[] buffer = new int[] { 0xab, 0xcd + i + 6, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef }; assertEquals(buffer.length, buf.readableBytes() - 2); for (int b : buffer) { assertEquals(b, buf.readUnsignedByte()); } int calcCrc = Utils.crc(buf, buf.readerIndex() - buffer.length, buffer.length); assertEquals(calcCrc, buf.readUnsignedShort()); assertTrue(!buf.isReadable()); ReferenceCountUtil.release(buf); } }
From source file:net.tomp2p.synchronization.SyncUtils.java
License:Apache License
public static List<Instruction> decodeInstructions(ByteBuf buf) { List<Instruction> result = new ArrayList<Instruction>(); while (buf.isReadable()) { final int header = buf.readInt(); if ((header & 0x80000000) != 0) { //first bit set, we have a reference final int reference = header & 0x7FFFFFFF; result.add(new Instruction(reference)); } else {/* w w w.jav a 2 s . c o m*/ //otherwise the header is the length final int length = header; final int remaining = Math.min(length, buf.readableBytes()); DataBuffer literal = new DataBuffer(buf.slice(buf.readerIndex(), remaining)); buf.skipBytes(remaining); result.add(new Instruction(literal)); } } return result; }