本文整理汇总了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);
}
}
示例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();
}
示例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();
}
}
}
示例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);
}
}
示例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();
}
}
}
}
示例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();
}
}
}
示例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();
}
}
示例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);
}
}
示例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();
}
}
示例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;
}
}
}
示例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();
}
}
示例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();
}
}
示例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();
}
}
}
示例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" );
}
示例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();
}