List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerHelloHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { buffer = buffer.order(ByteOrder.LITTLE_ENDIAN); while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) { int messageLength = getMessageLength(buffer); MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex())); switch (messageType) { case Hello: onHello(ctx, buffer.readSlice(messageLength)); break; default:// ww w.jav a 2 s. c o m throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid, "unexpected MessageType: " + messageType); } } }
From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerSymmetricHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { buffer = buffer.order(ByteOrder.LITTLE_ENDIAN); while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) { int messageLength = getMessageLength(buffer); MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex())); switch (messageType) { case SecureMessage: onSecureMessage(ctx, buffer.readSlice(messageLength), out); break; default://from w ww .ja v a 2 s. c o m out.add(buffer.readSlice(messageLength).retain()); } } }
From source file:com.dingwang.netty.decoder.PersonDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in == null || in.readableBytes() <= 0) { System.out.println("##################"); return;//from w w w . j av a 2 s . c o m } byte[] src = new byte[in.readableBytes()]; in.readBytes(src); String message = new String(src, "UTF-8"); Person p = new Gson().fromJson(message, Person.class); System.out.println("message======" + message); out.add(p); }
From source file:com.dingwang.netty.decoder.TimeDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { System.out.println("==========decode"); if (in.readableBytes() < 4) { return;//from w w w . java 2 s.c om } out.add(new UnixTime(in.readInt())); }
From source file:com.dingwang.rpc.decode.RpcDecoder.java
License:Open Source License
@Override public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;//from ww w. ja v a2 s .co m } in.markReaderIndex(); int dataLength = in.readInt(); if (dataLength < 0) { ctx.close(); } if (in.readableBytes() < dataLength) { in.resetReaderIndex(); } byte[] data = new byte[dataLength]; in.readBytes(data); Object obj = SerializationUtil.deserialize(data, genericClass); out.add(obj); }
From source file:com.dinstone.jrpc.transport.netty4.TransportProtocolDecoder.java
License:Apache License
private byte[] readFrame(ByteBuf in) { if (in.readableBytes() > 4) { in.markReaderIndex();//from www .j av a2 s .co m int len = in.readInt(); if (len > maxObjectSize) { throw new IllegalStateException( "The encoded object is too big: " + len + " (> " + maxObjectSize + ")"); } else if (len < 1) { throw new IllegalStateException("The encoded object is too small: " + len + " (< 1)"); } if (in.readableBytes() < len) { in.resetReaderIndex(); return null; } byte[] rpcBytes = new byte[len]; in.readBytes(rpcBytes); return rpcBytes; } return null; }
From source file:com.dinstone.rpc.netty.RpcProtocolDecoder.java
License:Apache License
private byte[] readFrame(ByteBuf in) { int remaining = in.readableBytes(); if (remaining < 4) { return null; }/*from w w w . java 2 s. com*/ int objectSize = in.getInt(0); if (objectSize > maxObjectSize) { throw new IllegalArgumentException( "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')'); } if (remaining - 4 >= objectSize) { objectSize = in.readInt(); // RPC object size byte[] rpcBytes = new byte[objectSize]; in.readBytes(rpcBytes); return rpcBytes; } return null; }
From source file:com.doctor.netty5.example.factorial_algorithm.BigIntegerDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // ? ?F + + -> 1 + 4 + ?? // ??//* w w w.ja v a 2 s. c om*/ if (in.readableBytes() < 5) { return; // F + } in.markReaderIndex(); short magicNumber = in.readUnsignedByte();// ?F if (magicNumber != 'F') { in.resetReaderIndex(); throw new RuntimeException("magicNumber must be 'F' "); } // ??? int dataLength = in.readInt(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } byte[] data = new byte[dataLength]; in.readBytes(data); out.add(new BigInteger(data)); }
From source file:com.dwarf.netty.guide.factorial.BigIntegerDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // Wait until the length prefix is available. if (in.readableBytes() < 5) { return;/*from w ww .ja v a 2 s . c om*/ } in.markReaderIndex(); // Check the magic number. int magicNumber = in.readUnsignedByte(); if (magicNumber != 'F') { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid magic number: " + magicNumber); } // Wait until the whole data is available. int dataLength = in.readInt(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } // Convert the received data into a new BigInteger. byte[] decoded = new byte[dataLength]; in.readBytes(decoded); out.add(new BigInteger(decoded)); }
From source file:com.ebay.jetstream.http.netty.client.HttpClient.java
License:MIT License
public void post(URI uri, Object content, Map<String, String> headers, ResponseFuture responsefuture) throws Exception { if (m_shutDown.get()) { throw new IOException("IO has been Shutdown = "); }/* w w w . ja v a 2 s. c o m*/ if (uri == null) throw new NullPointerException("uri is null or incorrect"); if (!m_started.get()) { synchronized (this) { if (!m_started.get()) { start(); m_started.set(true); } } } ByteBuf byteBuf = Unpooled.buffer(m_config.getInitialRequestBufferSize()); ByteBufOutputStream outputStream = new ByteBufOutputStream(byteBuf); // transform to json try { m_mapper.writeValue(outputStream, content); } catch (JsonGenerationException e) { throw e; } catch (JsonMappingException e) { throw e; } catch (IOException e) { throw e; } finally { outputStream.close(); } EmbeddedChannel encoder; String contenteEncoding; if ("gzip".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP, 1)); contenteEncoding = "gzip"; } else if ("deflate".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB, 1)); contenteEncoding = "deflate"; } else { encoder = null; contenteEncoding = null; } if (encoder != null) { encoder.config().setAllocator(UnpooledByteBufAllocator.DEFAULT); encoder.writeOutbound(byteBuf); encoder.finish(); byteBuf = (ByteBuf) encoder.readOutbound(); encoder.close(); } DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toString(), byteBuf); if (contenteEncoding != null) { HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_ENCODING, contenteEncoding); } HttpHeaders.setHeader(request, HttpHeaders.Names.ACCEPT_ENCODING, "gzip, deflate"); HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_TYPE, "application/json"); HttpHeaders.setContentLength(request, byteBuf.readableBytes()); if (isKeepAlive()) HttpHeaders.setHeader(request, HttpHeaders.Names.CONNECTION, "keep-alive"); if (headers != null) { @SuppressWarnings("rawtypes") Iterator it = headers.entrySet().iterator(); while (it.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry pairs = (Map.Entry) it.next(); HttpHeaders.setHeader(request, (String) pairs.getKey(), pairs.getValue()); } } if (responsefuture != null) { RequestId reqid = RequestId.newRequestId(); m_responseDispatcher.add(reqid, responsefuture); HttpHeaders.setHeader(request, "X_EBAY_REQ_ID", reqid.toString()); // we expect this to be echoed in the response used for correlation. } if (m_dataQueue.size() < m_workQueueCapacity) { ProcessHttpWorkRequest workRequest = new ProcessHttpWorkRequest(this, uri, request); if (!m_dataQueue.offer(workRequest)) { if (responsefuture != null) { responsefuture.setFailure(); m_responseDispatcher.remove(request.headers().get("X_EBAY_REQ_ID")); } } } else { throw new IOException("downstream Queue is full "); } }