List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java
License:Apache License
/** * Called once data should be decoded from the given {@link ByteBuf}. This method will call * {@link #decode(ChannelHandlerContext, ByteBuf, List)} as long as decoding should take place. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param in the {@link ByteBuf} from which to read data * @param out the {@link List} to which decoded messages should be added *//*from w ww .j a v a2s .c om*/ protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { try { while (in.isReadable()) { int outSize = out.size(); int oldInputLength = in.readableBytes(); decode(ctx, in, out); // Check if this handler was removed before try to continue the loop. // If it was removed it is not safe to continue to operate on the buffer // // See https://github.com/netty/netty/issues/1664 if (ctx.isRemoved()) { break; } if (outSize == out.size()) { if (oldInputLength == in.readableBytes()) { break; } else { continue; } } if (oldInputLength == in.readableBytes()) { throw new DecoderException(StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message."); } if (isSingleDecode()) { break; } } } catch (DecoderException e) { throw e; } catch (Throwable cause) { throw new DecoderException(cause); } }
From source file:com.bala.learning.learning.netty.HttpServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest request = this.request = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx);//from w ww .j a va 2s. c om } buf.setLength(0); buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n"); buf.append("===================================\r\n"); buf.append("VERSION: ").append(request.getProtocolVersion()).append("\r\n"); buf.append("HOSTNAME: ").append(HttpHeaders.getHost(request, "unknown")).append("\r\n"); buf.append("REQUEST_URI: ").append(request.getUri()).append("\r\n\r\n"); HttpHeaders headers = request.headers(); if (!headers.isEmpty()) { for (Map.Entry<String, String> h : headers) { String key = h.getKey(); String value = h.getValue(); buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n"); } buf.append("\r\n"); } QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); Map<String, List<String>> params = queryStringDecoder.parameters(); if (!params.isEmpty()) { for (Entry<String, List<String>> p : params.entrySet()) { String key = p.getKey(); List<String> vals = p.getValue(); for (String val : vals) { buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n"); } } buf.append("\r\n"); } appendDecoderResult(buf, request); } if (msg instanceof HttpContent) { HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { buf.append("CONTENT: "); buf.append(content.toString(CharsetUtil.UTF_8)); buf.append("\r\n"); appendDecoderResult(buf, request); } if (msg instanceof LastHttpContent) { buf.append("END OF CONTENT\r\n"); LastHttpContent trailer = (LastHttpContent) msg; if (!trailer.trailingHeaders().isEmpty()) { buf.append("\r\n"); for (String name : trailer.trailingHeaders().names()) { for (String value : trailer.trailingHeaders().getAll(name)) { buf.append("TRAILING HEADER: "); buf.append(name).append(" = ").append(value).append("\r\n"); } } buf.append("\r\n"); } if (!writeResponse(trailer, ctx)) { // If keep-alive is off, close the connection once the content is fully written. ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } } } }
From source file:com.blogspot.jabelarminecraft.blocksmith.proxy.CommonProxy.java
License:Open Source License
public List<ItemStack> convertPayloadToItemStackList(ByteBuf theBuffer) { List<ItemStack> theList = new ArrayList(); while (theBuffer.isReadable()) { int theID = theBuffer.readInt(); int theMetadata = theBuffer.readInt(); ItemStack theStack = new ItemStack(Item.getItemById(theID), 1, theMetadata); // Handle the case of mods like Tinker's Construct that use NBT instead of metadata boolean hasNBT = theBuffer.readBoolean(); if (hasNBT) { theStack.setTagCompound(ByteBufUtils.readTag(theBuffer)); // DEBUG System.out.println(//from w w w .j a v a 2 s . com "The stack " + theStack.toString() + " has NBT = " + theStack.getTagCompound().toString()); } theList.add(theStack); } // DEBUG System.out.println(theList.toString()); return theList; }
From source file:com.butor.netty.handler.codec.ftp.CrlfStringDecoder.java
License:Apache License
/** * {@inheritDoc}/* ww w . ja v a 2 s. co m*/ */ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf cb, List<Object> out) throws Exception { byte[] data = new byte[maxRequestLengthBytes]; int lineLength = 0; while (cb.isReadable()) { byte nextByte = cb.readByte(); if (nextByte == CR) { nextByte = cb.readByte(); if (nextByte == LF) { out.add(new String(data, encoding)); } } else if (nextByte == LF) { out.add(new String(data, encoding)); } else { if (lineLength >= maxRequestLengthBytes) throw new IllegalArgumentException( "Request size threshold exceeded: [" + maxRequestLengthBytes + "]"); data[lineLength] = nextByte; lineLength += 1; } } }
From source file:com.cdg.study.netty.discard.DiscardServerHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext context, Object msg) { // Netty? ?? ByteBuf buf = (ByteBuf) msg; // ?? ?? ? try {//from w ww . jav a2 s . c o m while (buf.isReadable()) { // (1) System.out.print((char) buf.readByte()); System.out.flush(); } } finally { buf.release(); // ? ? } }
From source file:com.chat.common.netty.handler.decode.ProtobufVarint32FrameDecoder.java
License:Apache License
/** * Reads variable length 32bit int from buffer * * @return decoded int if buffers readerIndex has been forwarded else nonsense value *//*from www . j av a2 s.co m*/ private static int readRawVarint32(ByteBuf buffer) { if (!buffer.isReadable()) { return 0; } buffer.markReaderIndex(); byte tmp = buffer.readByte(); if (tmp >= 0) { return tmp; } else { int result = tmp & 127; if (!buffer.isReadable()) { buffer.resetReaderIndex(); return 0; } if ((tmp = buffer.readByte()) >= 0) { result |= tmp << 7; } else { result |= (tmp & 127) << 7; if (!buffer.isReadable()) { buffer.resetReaderIndex(); return 0; } if ((tmp = buffer.readByte()) >= 0) { result |= tmp << 14; } else { result |= (tmp & 127) << 14; if (!buffer.isReadable()) { buffer.resetReaderIndex(); return 0; } if ((tmp = buffer.readByte()) >= 0) { result |= tmp << 21; } else { result |= (tmp & 127) << 21; if (!buffer.isReadable()) { buffer.resetReaderIndex(); return 0; } result |= (tmp = buffer.readByte()) << 28; if (tmp < 0) { throw new CorruptedFrameException("malformed varint."); } } } } return result; } }
From source file:com.chen.opensourceframework.netty.copy.DiscardServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2) ByteBuf in = (ByteBuf) msg; try {/*w ww. j a v a2 s . c o m*/ while (in.isReadable()) { // (1) System.out.print((char) in.readByte()); System.out.flush(); } } finally { ReferenceCountUtil.release(msg); // (2) } // // System.out.println("?:..."); // ctx.write(msg); // (1) // ctx.flush(); // (2) }
From source file:com.chen.opensourceframework.netty.discard.DiscardServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { // discard//from w w w . j a v a 2 s.c o m ByteBuf in = (ByteBuf) msg; try { while (in.isReadable()) { // (1) System.out.print((char) in.readChar()); System.out.flush(); } } finally { ReferenceCountUtil.release(msg); // (2) } }
From source file:com.chen.opensourceframework.netty.echo.EchoServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // ctx.write(msg); ByteBuf in = (ByteBuf) msg; try {//from w ww. j a v a 2s . co m while (in.isReadable()) { // (1) System.out.print(in.readChar()); System.out.flush(); } } finally { ReferenceCountUtil.release(msg); // (2) } }
From source file:com.chiorichan.factory.FileInterpreter.java
License:Mozilla Public License
public static String readLine(ByteBuf buf) { if (!buf.isReadable() || buf.readableBytes() < 1) return null; String op = ""; while (buf.isReadable() && buf.readableBytes() > 0) { byte bb = buf.readByte(); if (bb == '\n') break; op += (char) bb; }//from w w w .j a v a 2 s . com return op; }