List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:com.alibaba.rocketmq.remoting.netty.NettyDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = null; try {//from w w w . ja v a 2s. co m frame = (ByteBuf) super.decode(ctx, in); if (null == frame) { return null; } ByteBuffer byteBuffer = frame.nioBuffer(); return RemotingCommand.decode(byteBuffer); } catch (Exception e) { log.error("decode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); RemotingUtil.closeChannel(ctx.channel()); } finally { if (null != frame) { frame.release(); } } return null; }
From source file:com.allanbank.mongodb.netty.NettyOutputBufferTest.java
License:Apache License
/** * Test method for {@link NettyOutputBuffer#close()}. *//* ww w. j a v a2 s.c om*/ @Test public void testClose() { final ByteBuf mockBuffer = createMock(ByteBuf.class); expect(mockBuffer.writerIndex()).andReturn(0); expect(mockBuffer.release()).andReturn(true); replay(mockBuffer); final NettyOutputBuffer outBuffer = new NettyOutputBuffer(mockBuffer, new StringEncoderCache()); assertThat(outBuffer.getBuffer(), sameInstance(mockBuffer)); outBuffer.close(); verify(mockBuffer); }
From source file:com.athena.dolly.websocket.server.test.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.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);/*from w w w.j a va2 s . co m*/ buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.athena.dolly.websocket.server.test.WebSocketSslServerHandler.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.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from www . j a v a 2s . co m buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().write(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.basho.riak.client.core.netty.RiakHttpMessageHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpResponse) { message = new RiakHttpMessage((HttpResponse) msg); }/*from ww w . j av a 2s. co m*/ if (msg instanceof HttpContent) { /** TODO: commented for compilation * chunks.add(((HttpContent)msg).data().retain()); * totalContentLength += ((HttpContent)msg).data().readableBytes(); */ if (msg instanceof LastHttpContent) { byte[] bytes = new byte[totalContentLength]; int index = 0; for (ByteBuf buffer : chunks) { int readable = buffer.readableBytes(); buffer.readBytes(bytes, index, readable); index += readable; buffer.release(); } int responseCode = message.getResponse().getStatus().code(); message.setContent(bytes); /** TODO: commented for compilation if ( responseCode < 200 || (responseCode >= 400 && responseCode != 404 && responseCode != 412 ) ) { listener.onException(chc.channel(), new RiakResponseException(responseCode, new String(bytes))); } else { message.setContent(bytes); listener.onSuccess(chc.channel(), message); } chc.channel().pipeline().remove(this); */ } } }
From source file:com.cats.version.httpserver.HttpStaticFileServerHandler.java
License:Apache License
private static void sendListing(ChannelHandlerContext ctx, File dir) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); String dirPath = dir.getPath(); StringBuilder buf = new StringBuilder().append("<!DOCTYPE html>\r\n").append("<html><head><title>") .append("Listing of: ").append(dirPath).append("</title></head><body>\r\n") .append("<h3>Listing of: ").append(dirPath).append("</h3>\r\n") .append("<ul>").append("<li><a href=\"../\">..</a></li>\r\n"); for (File f : dir.listFiles()) { if (f.isHidden() || !f.canRead()) { continue; }/*w ww . j ava 2 s . c o m*/ String name = f.getName(); if (!ALLOWED_FILE_NAME.matcher(name).matches()) { continue; } buf.append("<li><a href=\"").append(name).append("\">").append(name).append("</a></li>\r\n"); } buf.append("</ul></body></html>\r\n"); ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8); response.content().writeBytes(buffer); buffer.release(); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.cats.version.httpserver.HttpStaticFileServerHandler.java
License:Apache License
public static void sendWarningInfo(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); StringBuffer buf = new StringBuffer(); buf.append("<!DOCTYPE html>\r\n<html>"); buf.append("<br/><br/><br/><br/><br/><br/>"); buf.append("<center><font size='5px'>"); buf.append(/*from w ww . j ava 2 s .c o m*/ "Access denied, because this is version monitor service server</br></br> CTAS@Intel 2016~2026 All rights reserved"); buf.append("</font></center>"); buf.append("<html>"); ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8); response.content().writeBytes(buffer); buffer.release(); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.cats.version.httpserver.msg.IMsgHandler.java
License:Apache License
public void sendMessageToClient(String strRsp, ChannelHandlerContext ctx) { if (null == strRsp) { return;/* w w w . java 2 s . co m*/ } FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); ByteBuf buffer = Unpooled.copiedBuffer(strRsp, CharsetUtil.UTF_8); response.content().writeBytes(buffer); buffer.release(); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
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 w w. j a va2s .c om while (buf.isReadable()) { // (1) System.out.print((char) buf.readByte()); System.out.flush(); } } finally { buf.release(); // ? ? } }
From source file:com.cdg.study.netty.time.TimeClientHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf m = (ByteBuf) msg; // (1) try {//w w w . j av a2 s.com long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L; System.out.println(new Date(currentTimeMillis)); ctx.close(); } finally { m.release(); } }