当前位置: 首页>>代码示例>>Java>>正文


Java ByteBuf.release方法代码示例

本文整理汇总了Java中io.netty.buffer.ByteBuf.release方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf.release方法的具体用法?Java ByteBuf.release怎么用?Java ByteBuf.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.netty.buffer.ByteBuf的用法示例。


在下文中一共展示了ByteBuf.release方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendHttpResponse

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
 * 返回http信息
 * @param ctx
 * @param req
 * @param res
 */
private static void sendHttpResponse(
        ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}
 
开发者ID:ninelook,项目名称:wecard-server,代码行数:23,代码来源:NettyServerHandler.java

示例2: shouldHandleConnect

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Test
public void shouldHandleConnect() {
    Http1FrontendHandler handler = httpProxyHandler(true);
    inboundChannel.pipeline().addLast(handler);

    ByteBuf requestBytes = requestBytes(new DefaultHttpRequest(
            HttpVersion.HTTP_1_1, HttpMethod.CONNECT, "localhost:8000"));
    assertFalse(inboundChannel.writeInbound(requestBytes));

    assertNotNull(outboundChannel);
    assertTrue(outboundChannel.isActive());

    assertEquals(1, inboundChannel.outboundMessages().size());
    assertTrue(inboundChannel.outboundMessages().peek() instanceof ByteBuf);

    ByteBuf respByteBuf = (ByteBuf) inboundChannel.outboundMessages().poll();
    byte[] respBytes = new byte[respByteBuf.readableBytes()];
    respByteBuf.readBytes(respBytes);
    assertEquals("HTTP/1.1 200 OK\r\n\r\n", new String(respBytes));
    respByteBuf.release();
}
 
开发者ID:chhsiao90,项目名称:nitmproxy,代码行数:22,代码来源:Http1FrontendHandlerTest.java

示例3: doReadMessages

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    UkcpServerChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(UkcpPacket.newInstance(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
                remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
 
开发者ID:szhnet,项目名称:kcp-netty,代码行数:32,代码来源:UkcpServerChannel.java

示例4: sendHttpResponse

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
   if (res.getStatus().code() != 200) {
      ByteBuf f = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
      res.content().clear();
      res.content().writeBytes(f);
      f.release();
   }

   HttpHeaders.setContentLength(res, res.content().readableBytes());
   ChannelFuture f1;
   f1 = ctx.channel().writeAndFlush(res);

   if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
      f1.addListener(ChannelFutureListener.CLOSE);
   }
}
 
开发者ID:SpreadServe,项目名称:TFWebSock,代码行数:17,代码来源:NettyHttpFileHandler.java

示例5: getBase64EncodedString

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/** Return a Base64-encoded string. */
private static String getBase64EncodedString(String str) {
  ByteBuf byteBuf = null;
  ByteBuf encodedByteBuf = null;
  try {
    byteBuf = Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8));
    encodedByteBuf = Base64.encode(byteBuf);
    return encodedByteBuf.toString(StandardCharsets.UTF_8);
  } finally {
    // The release is called to suppress the memory leak error messages raised by netty.
    if (byteBuf != null) {
      byteBuf.release();
      if (encodedByteBuf != null) {
        encodedByteBuf.release();
      }
    }
  }
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:19,代码来源:SparkSaslServer.java

示例6: encode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private byte[] encode(Object task) {
    // erase RedissonClient field to avoid its serialization
    Injector.inject(task, null);
    
    ByteBuf buf = null;
    try {
        buf = codec.getValueEncoder().encode(task);
        byte[] dst = new byte[buf.readableBytes()];
        buf.readBytes(dst);
        return dst;
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        if (buf != null) {
            buf.release();
        }
    }
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:19,代码来源:RedissonExecutorService.java

示例7: decode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
        snappyDecoder.get().decode(buf, out);
        return innerCodec.getValueDecoder().decode(out, state);
    } finally {
        snappyDecoder.get().reset();
        out.release();
    }
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:12,代码来源:SnappyCodec.java

示例8: returnBuffer

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private void returnBuffer(Request item) {
  ByteBuf buf = item.getContext().getSerializedData();
  if(buf != null && buf.refCnt() > 0) {
    try {
      buf.release();
    } catch (Throwable x) {
      LOG.error("Release Buffer failed." , x);
    }
    item.getContext().setSerializedData(null);
  }
}
 
开发者ID:Tencent,项目名称:angel,代码行数:12,代码来源:MatrixTransportClient.java

示例9: hash

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public static byte[] hash(ByteBuf objectState) {
    ByteBuffer b = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes());
    long h1 = LongHashFunction.farmUo().hashBytes(b);
    long h2 = LongHashFunction.xx().hashBytes(b);

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
    try {
        buf.writeLong(h1).writeLong(h2);
        byte[] dst = new byte[buf.readableBytes()];
        buf.readBytes(dst);
        return dst;
    } finally {
        buf.release();
    }
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:16,代码来源:Hash.java

示例10: decode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
	in.markReaderIndex();
	byte[] lengthBytes = new byte[3];

	for (int i = 0; i < 3; i++)
	{
		if (!in.isReadable())
		{
			in.resetReaderIndex();
			return;
		}
		lengthBytes[i] = in.readByte();
		if (lengthBytes[i] >= 0)
		{
			ByteBuf buffer = Unpooled.wrappedBuffer(lengthBytes);

			try
			{
				int packetLength = readVarInt(buffer);
				if (in.readableBytes() < packetLength)
				{
					in.resetReaderIndex();
					return;
				}

				out.add(in.readBytes(packetLength));
			}
			finally
			{
				buffer.release();
			}

			return;
		}
	}
}
 
开发者ID:Dytanic,项目名称:CloudNet,代码行数:38,代码来源:ProtocolLengthDeserializer.java

示例11: testBuffers

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private void testBuffers(ByteBuf[] buffers, SerializedField.Builder[] fields) {
  int i = 0;
  for (SerializedField.Builder field : fields) {
    i += fieldBuffersCount(field, buffers, i, buffers.length);
  }
  while (i < buffers.length && buffers[i].readableBytes() == 0) {
    ++ i;
  }
  assertEquals(buffers.length, i);
  for (ByteBuf byteBuf : buffers) {
    byteBuf.release();
  }
}
 
开发者ID:dremio,项目名称:dremio-oss,代码行数:14,代码来源:TestBackwardsCompatibilityEncoder.java

示例12: writeToStream

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public void writeToStream(FSDataOutputStream stream) throws IOException {
  Stopwatch watch = new Stopwatch();
  watch.start();
  available = false;
  check = ThreadLocalRandom.current().nextLong();
  start = stream.getPos();
  logger.debug("Writing check value {} at position {}", check, start);
  stream.writeLong(check);
  batch.getHeader().writeDelimitedTo(stream);
  ByteBuf buf = batch.getBody();
  if (buf != null) {
    bodyLength = buf.capacity();
  } else {
    bodyLength = 0;
  }
  if (bodyLength > 0) {
    buf.getBytes(0, stream, bodyLength);
  }
  stream.hsync();
  FileStatus status = fs.getFileStatus(path);
  long len = status.getLen();
  logger.debug("After spooling batch, stream at position {}. File length {}", stream.getPos(), len);
  batch.sendOk();
  latch.countDown();
  long t = watch.elapsed(TimeUnit.MICROSECONDS);
  logger.debug("Took {} us to spool {} to disk. Rate {} mb/s", t, bodyLength, bodyLength / t);
  if (buf != null) {
    buf.release();
  }
}
 
开发者ID:skhalifa,项目名称:QDrill,代码行数:31,代码来源:SpoolingRawBatchBuffer.java

示例13: addFaviconToStatusResponse

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private void addFaviconToStatusResponse(ServerStatusResponse response)
{
    File file1 = this.getFile("server-icon.png");

    if (file1.isFile())
    {
        ByteBuf bytebuf = Unpooled.buffer();

        try
        {
            BufferedImage bufferedimage = ImageIO.read(file1);
            Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]);
            Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]);
            ImageIO.write(bufferedimage, "PNG", (OutputStream)(new ByteBufOutputStream(bytebuf)));
            ByteBuf bytebuf1 = Base64.encode(bytebuf);
            response.setFavicon("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8));
        }
        catch (Exception exception)
        {
            logger.error((String)"Couldn\'t load server icon", (Throwable)exception);
        }
        finally
        {
            bytebuf.release();
        }
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:28,代码来源:MinecraftServer.java

示例14: decode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode( ChannelHandlerContext channelHandlerContext, ByteBuf input, List<Object> output ) throws Exception {
    input.markReaderIndex();

    byte[] bytes = new byte[3];
    for ( int i = 0; i < bytes.length; i++ ) {
        if ( !input.isReadable() ) {
            input.resetReaderIndex();
            return;
        }
        bytes[i] = input.readByte();
        if ( bytes[i] >= 0 ) {
            ByteBuf buf = Unpooled.wrappedBuffer( bytes );
            try {
                int j = Message.readVarInt( buf );
                if ( input.readableBytes() < j ) {
                    input.resetReaderIndex();
                    return;
                }
                output.add( input.readBytes( j ) );
                return;
            } finally {
                buf.release();
            }
        }
    }
    throw new CorruptedFrameException( "length wider than 21-bit" );
}
 
开发者ID:lukas81298,项目名称:FlexMC,代码行数:29,代码来源:MessageSplitter.java

示例15: encode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, NetoOutboundByteMessage msg, ByteBuf out) throws Exception {
    ByteBuf buffer = msg.buildMessage(charset);
    out.writeBytes(buffer);
    logger.debug("encode - {}", buffer);
    buffer.release();
}
 
开发者ID:veritasware,项目名称:neto,代码行数:8,代码来源:NetoByteToMessageEncoder.java


注:本文中的io.netty.buffer.ByteBuf.release方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。