Prettier string representation of ChannelEvent implementations

This commit is contained in:
Trustin Lee 2009-02-08 16:37:49 +00:00
parent 52e4288964
commit dd971b5f79
3 changed files with 101 additions and 6 deletions

View File

@ -24,6 +24,8 @@ package org.jboss.netty.channel;
import java.net.SocketAddress;
import org.jboss.netty.util.StringUtil;
/**
* The default {@link MessageEvent} implementation. It is recommended to
* use {@link Channels#messageEvent(Channel, ChannelFuture, Object)} and
@ -84,10 +86,13 @@ public final class DownstreamMessageEvent implements MessageEvent {
@Override
public String toString() {
if (remoteAddress == null) {
return getChannel().toString() + " - (WRITE: " + message + ')';
if (getRemoteAddress() == null) {
return getChannel().toString() + " - (WRITE: " +
StringUtil.stripControlCharacters(getMessage()) + ')';
} else {
return getChannel().toString() + " - (WRITE: " + message + ", " + remoteAddress + ')';
return getChannel().toString() + " - (WRITE: " +
StringUtil.stripControlCharacters(getMessage()) + ", " +
getRemoteAddress() + ')';
}
}
}

View File

@ -26,6 +26,8 @@ import static org.jboss.netty.channel.Channels.*;
import java.net.SocketAddress;
import org.jboss.netty.util.StringUtil;
/**
* The default {@link MessageEvent} implementation. It is recommended to
* use {@link Channels#messageEvent(Channel, ChannelFuture, Object)} and
@ -80,10 +82,13 @@ public final class UpstreamMessageEvent implements MessageEvent {
@Override
public String toString() {
if (remoteAddress == null) {
return getChannel().toString() + " - (RECEIVED: " + message + ')';
if (getRemoteAddress() == null) {
return getChannel().toString() + " - (RECEIVED: " +
StringUtil.stripControlCharacters(getMessage()) + ')';
} else {
return getChannel().toString() + " - (RECEIVED: " + message + ", " + remoteAddress + ')';
return getChannel().toString() + " - (RECEIVED: " +
StringUtil.stripControlCharacters(getMessage()) + ", " +
getRemoteAddress() + ')';
}
}
}

View File

@ -0,0 +1,85 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.util;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public class StringUtil {
public static String stripControlCharacters(Object value) {
if (value == null) {
return null;
}
return stripControlCharacters(value.toString());
}
public static String stripControlCharacters(String value) {
if (value == null) {
return null;
}
boolean hasControlChars = false;
for (int i = value.length() - 1; i >= 0; i --) {
if (Character.isISOControl(value.charAt(i))) {
hasControlChars = true;
break;
}
}
if (!hasControlChars) {
return value;
}
StringBuilder buf = new StringBuilder(value.length());
int i = 0;
// Skip initial control characters (i.e. left trim)
for (; i < value.length(); i ++) {
if (!Character.isISOControl(value.charAt(i))) {
break;
}
}
// Copy non control characters and substitute control characters with
// a space. The last control characters are trimmed.
boolean suppressingControlChars = false;
for (; i < value.length(); i ++) {
if (Character.isISOControl(value.charAt(i))) {
suppressingControlChars = true;
continue;
} else {
if (suppressingControlChars) {
suppressingControlChars = false;
buf.append(' ');
}
buf.append(value.charAt(i));
}
}
return buf.toString();
}
}