Do not allocate a short-lived ByteBuf 24/77524/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 6 Nov 2018 08:21:26 +0000 (09:21 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 6 Nov 2018 09:14:28 +0000 (10:14 +0100)
ByteBuf.writeCharSequence() allows us to bypass manual buffer
management, use that.

Change-Id: I85dc57a8411a057843ec45b5b70fea8907ae9bed
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServerHandler.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/websockets/WebSocketServerHandler.java

index 666ad758a5118d4b7afb2cc3758142ba2974dfa4..ed90e3f2369ed3818691802cad1daf44e4ed4610 100755 (executable)
@@ -17,8 +17,6 @@ import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
 import static io.netty.handler.codec.http.HttpUtil.setContentLength;
 import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelHandlerContext;
@@ -126,9 +124,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
         // Generate an error page if response getStatus code is not OK (200).
         final boolean notOkay = !OK.equals(res.status());
         if (notOkay) {
-            final ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
-            res.content().writeBytes(buf);
-            buf.release();
+            res.content().writeCharSequence(res.status().toString(), CharsetUtil.UTF_8);
             setContentLength(res, res.content().readableBytes());
         }
 
index 7438ced14b297b8eb027241172a6a76637cd9de8..903b0dbf56d5a247453aaec886d39edb2415acb5 100755 (executable)
@@ -18,8 +18,6 @@ import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
 import static io.netty.handler.codec.http.HttpUtil.setContentLength;
 import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelHandlerContext;
@@ -48,7 +46,6 @@ import org.slf4j.LoggerFactory;
  * {@link FullHttpRequest} and {@link WebSocketFrame} messages.
  */
 public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object> {
-
     private static final Logger LOG = LoggerFactory.getLogger(WebSocketServerHandler.class);
 
     private WebSocketServerHandshaker handshaker;
@@ -129,9 +126,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
         // Generate an error page if response getStatus code is not OK (200).
         final boolean notOkay = !OK.equals(res.status());
         if (notOkay) {
-            final ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
-            res.content().writeBytes(buf);
-            buf.release();
+            res.content().writeCharSequence(res.status().toString(), CharsetUtil.UTF_8);
             setContentLength(res, res.content().readableBytes());
         }
 
@@ -145,10 +140,8 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
     /**
      * Handles web socket frame.
      *
-     * @param ctx
-     *            {@link ChannelHandlerContext}
-     * @param frame
-     *            {@link WebSocketFrame}
+     * @param ctx {@link ChannelHandlerContext}
+     * @param frame {@link WebSocketFrame}
      */
     private void handleWebSocketFrame(final ChannelHandlerContext ctx, final WebSocketFrame frame) {
         if (frame instanceof CloseWebSocketFrame) {