From 4d6255d4eac9c8ed81cf6e21cde3f7061f72b046 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 2 Apr 2009 09:10:57 +0000 Subject: [PATCH] * Simplified DefaultObjectSizeEstimator as suggested by Stefan * Made sure HttpMessage.getContent() does not return null at any case * Improved TelnetServerHandler to send DOS style line delimiter so that it does not look bad in Windows --- .../netty/example/telnet/TelnetServerHandler.java | 10 +++++----- .../netty/handler/codec/http/DefaultHttpMessage.java | 5 ++++- .../netty/handler/codec/http/HttpMessageEncoder.java | 2 +- .../handler/execution/DefaultObjectSizeEstimator.java | 7 +++---- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jboss/netty/example/telnet/TelnetServerHandler.java b/src/main/java/org/jboss/netty/example/telnet/TelnetServerHandler.java index 0b9d8e3502..f880b636b1 100644 --- a/src/main/java/org/jboss/netty/example/telnet/TelnetServerHandler.java +++ b/src/main/java/org/jboss/netty/example/telnet/TelnetServerHandler.java @@ -65,8 +65,8 @@ public class TelnetServerHandler extends SimpleChannelHandler { ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // Send greeting for a new connection. e.getChannel().write( - "Welcome to " + InetAddress.getLocalHost().getHostName() + "!\n"); - e.getChannel().write("It is " + new Date() + " now.\n"); + "Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n"); + e.getChannel().write("It is " + new Date() + " now.\r\n"); } @Override @@ -81,12 +81,12 @@ public class TelnetServerHandler extends SimpleChannelHandler { String response; boolean close = false; if (request.length() == 0) { - response = "Please type something.\n"; + response = "Please type something.\r\n"; } else if (request.toLowerCase().equals("bye")) { - response = "Have a good day!\n"; + response = "Have a good day!\r\n"; close = true; } else { - response = "Did you say '" + request + "'?\n"; + response = "Did you say '" + request + "'?\r\n"; } // We do not need to write a ChannelBuffer here. diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java index 85c178b57c..077a6d64ce 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java @@ -178,7 +178,10 @@ public class DefaultHttpMessage implements HttpMessage { headers.clear(); } - public void setContent(final ChannelBuffer content) { + public void setContent(ChannelBuffer content) { + if (content == null) { + content = ChannelBuffers.EMPTY_BUFFER; + } this.content = content; } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java index 4cd002e8cc..96c1ac67e5 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java @@ -58,7 +58,7 @@ public abstract class HttpMessageEncoder extends OneToOneEncoder { header.writeBytes(CRLF); ChannelBuffer content = request.getContent(); - if (content == null) { + if (!content.readable()) { return header; // no content } else { return wrappedBuffer(header, content); diff --git a/src/main/java/org/jboss/netty/handler/execution/DefaultObjectSizeEstimator.java b/src/main/java/org/jboss/netty/handler/execution/DefaultObjectSizeEstimator.java index 88b6244873..2d9117f4ea 100644 --- a/src/main/java/org/jboss/netty/handler/execution/DefaultObjectSizeEstimator.java +++ b/src/main/java/org/jboss/netty/handler/execution/DefaultObjectSizeEstimator.java @@ -130,10 +130,9 @@ public class DefaultObjectSizeEstimator implements ObjectSizeEstimator { } private static int align(int size) { - if (size % 8 != 0) { - size /= 8; - size ++; - size *= 8; + int r = size % 8; + if (r != 0) { + size += 8 - r; } return size; }