List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:com.example.time1.client.FragmentedTimeClientHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { final ByteBuf buf = (ByteBuf) msg; byteBuf.writeBytes(buf); // ???? byteBuf ? buf.release(); if (byteBuf.readableBytes() >= 8) { // ?? final long epochSeconds = byteBuf.readLong(); final OffsetDateTime serverTime = Instant.ofEpochSecond(epochSeconds).atOffset(ZoneOffset.UTC); log.info("server time: {}", serverTime); ctx.close();// w ww .j a v a 2 s. co m } }
From source file:com.fanavard.challenge.server.websocket.server.WebSocketServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);/*from w w w . jav a 2 s . c o m*/ buf.release(); HttpHeaderUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.fjn.helper.frameworkex.netty.v4.echotest.EchoClientHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { System.out.println("Client received : " + ByteBufUtil.hexDump(msg.readBytes(msg.readableBytes()))); msg.release(); }
From source file:com.fjn.helper.frameworkex.netty.v4.timetest.TimeClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf m = (ByteBuf) msg; buf.writeBytes(m); // (2) m.release(); if (buf.readableBytes() >= 4) { // (3) long currentTimeMillis = (buf.readUnsignedInt() - 2208988800L) * 1000L; System.out.println(new Date(currentTimeMillis)); ctx.close();/*from ww w . ja va 2s . c om*/ } }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static final ByteBuf getKeyValArrayResponse(ByteBufAllocator alloc, Collection<Entry<ByteArrayWrapper, ByteArrayWrapper>> items) { Iterator<Map.Entry<ByteArrayWrapper, ByteArrayWrapper>> it = items.iterator(); ByteBuf response = alloc.buffer();//from w w w . j a v a 2 s . com response.writeByte(ARRAY_ID); int size = 0; ByteBuf tmp = alloc.buffer(); while (it.hasNext()) { Map.Entry<ByteArrayWrapper, ByteArrayWrapper> next = it.next(); byte[] key; byte[] nextByteArray; try { key = next.getKey().toBytes(); nextByteArray = next.getValue().toBytes(); } catch (EntryDestroyedException e) { continue; } tmp.writeByte(BULK_STRING_ID); // Add key tmp.writeBytes(intToBytes(key.length)); tmp.writeBytes(CRLFar); tmp.writeBytes(key); tmp.writeBytes(CRLFar); tmp.writeByte(BULK_STRING_ID); // Add value tmp.writeBytes(intToBytes(nextByteArray.length)); tmp.writeBytes(CRLFar); tmp.writeBytes(nextByteArray); tmp.writeBytes(CRLFar); size++; } response.writeBytes(intToBytes(size * 2)); response.writeBytes(CRLFar); response.writeBytes(tmp); tmp.release(); return response; }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static ByteBuf getBulkStringArrayResponseOfValues(ByteBufAllocator alloc, Collection<?> items) { Iterator<?> it = items.iterator(); ByteBuf response = alloc.buffer();/*from w ww . j ava 2s.c o m*/ response.writeByte(Coder.ARRAY_ID); ByteBuf tmp = alloc.buffer(); int size = 0; while (it.hasNext()) { Object next = it.next(); ByteArrayWrapper nextWrapper = null; if (next instanceof Entry) { try { nextWrapper = (ByteArrayWrapper) ((Entry<?, ?>) next).getValue(); } catch (EntryDestroyedException e) { continue; } } else if (next instanceof Struct) { nextWrapper = (ByteArrayWrapper) ((Struct) next).getFieldValues()[1]; } if (nextWrapper != null) { tmp.writeByte(Coder.BULK_STRING_ID); tmp.writeBytes(intToBytes(nextWrapper.length())); tmp.writeBytes(Coder.CRLFar); tmp.writeBytes(nextWrapper.toBytes()); tmp.writeBytes(Coder.CRLFar); } else { tmp.writeBytes(Coder.bNIL); } size++; } response.writeBytes(intToBytes(size)); response.writeBytes(Coder.CRLFar); response.writeBytes(tmp); tmp.release(); return response; }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static ByteBuf zRangeResponse(ByteBufAllocator alloc, Collection<?> list, boolean withScores) { if (list.isEmpty()) return Coder.getEmptyArrayResponse(alloc); ByteBuf buffer = alloc.buffer();/*from w w w . j a v a 2s . c o m*/ buffer.writeByte(Coder.ARRAY_ID); ByteBuf tmp = alloc.buffer(); int size = 0; for (Object entry : list) { ByteArrayWrapper key; DoubleWrapper score; if (entry instanceof Entry) { try { key = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey(); score = (DoubleWrapper) ((Entry<?, ?>) entry).getValue(); } catch (EntryDestroyedException e) { continue; } } else { Object[] fieldVals = ((Struct) entry).getFieldValues(); key = (ByteArrayWrapper) fieldVals[0]; score = (DoubleWrapper) fieldVals[1]; } byte[] byteAr = key.toBytes(); tmp.writeByte(Coder.BULK_STRING_ID); tmp.writeBytes(intToBytes(byteAr.length)); tmp.writeBytes(Coder.CRLFar); tmp.writeBytes(byteAr); tmp.writeBytes(Coder.CRLFar); size++; if (withScores) { String scoreString = score.toString(); byte[] scoreAr = stringToBytes(scoreString); tmp.writeByte(Coder.BULK_STRING_ID); tmp.writeBytes(intToBytes(scoreString.length())); tmp.writeBytes(Coder.CRLFar); tmp.writeBytes(scoreAr); tmp.writeBytes(Coder.CRLFar); size++; } } buffer.writeBytes(intToBytes(size)); buffer.writeBytes(Coder.CRLFar); buffer.writeBytes(tmp); tmp.release(); return buffer; }
From source file:com.gemstone.gemfire.internal.redis.ExecutionHandlerContext.java
License:Apache License
/** * Reset the transaction of client//from w ww .j a v a 2s . com */ public void clearTransaction() { this.transactionID = null; if (this.transactionQueue != null) { for (Command c : this.transactionQueue) { ByteBuf r = c.getResponse(); if (r != null) r.release(); } this.transactionQueue.clear(); } }
From source file:com.github.milenkovicm.kafka.handler.AbstractProducerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { final ByteBuf message = (ByteBuf) msg; final ProduceResponse produceResponse = parseProduceResponse(message); if (produceResponse.topics.get(0).partitions.get(0).errorCode != 0) { // TODO: this should be handled better when there is more then one partition involved Error error = Error.valueOf(produceResponse.topics.get(0).partitions.get(0).errorCode); this.ackHandle.fail(produceResponse.correlationId, error.exception); // TODO: should we disconnect channel if there is error ... maybe disconnection may help to recover } else {//w w w. j av a 2 s . com this.ackHandle.success(produceResponse.correlationId); } message.release(); }
From source file:com.github.milenkovicm.kafka.handler.CopyProducerHandler.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { final ByteBuf message = (ByteBuf) msg; final ByteBuf kafkaMessage = creteProduceRequest(ctx.alloc(), message, topicName); message.release(); super.write(ctx, kafkaMessage, promise); }