Small performance improvements

Motivation:

Found performance issues via FindBugs and PMD.

Modifications:

- Removed unnecessary boxing/unboxing operations in DefaultTextHeaders.convertToInt(CharSequence) and DefaultTextHeaders.convertToLong(CharSequence). A boxed primitive is created from a string, just to extract the unboxed primitive value.
- Added a static modifier for DefaultHttp2Connection.ParentChangedEvent class. This class is an inner class, but does not use its embedded reference to the object which created it. This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.
- Added a static compiled Pattern to avoid compile it each time it is used when we need to replace some part of authority.
- Improved using of StringBuilders.

Result:

Performance improvements.
This commit is contained in:
Idel Pivnitskiy 2014-11-09 01:46:30 +03:00 committed by Scott Mitchell
parent 3843ca55a2
commit 3d200085a4
31 changed files with 344 additions and 343 deletions

View File

@ -1104,23 +1104,18 @@ public abstract class AbstractByteBuf extends ByteBuf {
return StringUtil.simpleClassName(this) + "(freed)";
}
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append("(ridx: ");
buf.append(readerIndex);
buf.append(", widx: ");
buf.append(writerIndex);
buf.append(", cap: ");
buf.append(capacity());
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(ridx: ").append(readerIndex)
.append(", widx: ").append(writerIndex)
.append(", cap: ").append(capacity());
if (maxCapacity != Integer.MAX_VALUE) {
buf.append('/');
buf.append(maxCapacity);
buf.append('/').append(maxCapacity);
}
ByteBuf unwrapped = unwrap();
if (unwrapped != null) {
buf.append(", unwrapped: ");
buf.append(unwrapped);
buf.append(", unwrapped: ").append(unwrapped);
}
buf.append(')');
return buf.toString();

View File

@ -309,41 +309,41 @@ abstract class PoolArena<T> {
protected abstract void destroyChunk(PoolChunk<T> chunk);
public synchronized String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Chunk(s) at 0~25%:");
buf.append(StringUtil.NEWLINE);
buf.append(qInit);
buf.append(StringUtil.NEWLINE);
buf.append("Chunk(s) at 0~50%:");
buf.append(StringUtil.NEWLINE);
buf.append(q000);
buf.append(StringUtil.NEWLINE);
buf.append("Chunk(s) at 25~75%:");
buf.append(StringUtil.NEWLINE);
buf.append(q025);
buf.append(StringUtil.NEWLINE);
buf.append("Chunk(s) at 50~100%:");
buf.append(StringUtil.NEWLINE);
buf.append(q050);
buf.append(StringUtil.NEWLINE);
buf.append("Chunk(s) at 75~100%:");
buf.append(StringUtil.NEWLINE);
buf.append(q075);
buf.append(StringUtil.NEWLINE);
buf.append("Chunk(s) at 100%:");
buf.append(StringUtil.NEWLINE);
buf.append(q100);
buf.append(StringUtil.NEWLINE);
buf.append("tiny subpages:");
StringBuilder buf = new StringBuilder()
.append("Chunk(s) at 0~25%:")
.append(StringUtil.NEWLINE)
.append(qInit)
.append(StringUtil.NEWLINE)
.append("Chunk(s) at 0~50%:")
.append(StringUtil.NEWLINE)
.append(q000)
.append(StringUtil.NEWLINE)
.append("Chunk(s) at 25~75%:")
.append(StringUtil.NEWLINE)
.append(q025)
.append(StringUtil.NEWLINE)
.append("Chunk(s) at 50~100%:")
.append(StringUtil.NEWLINE)
.append(q050)
.append(StringUtil.NEWLINE)
.append("Chunk(s) at 75~100%:")
.append(StringUtil.NEWLINE)
.append(q075)
.append(StringUtil.NEWLINE)
.append("Chunk(s) at 100%:")
.append(StringUtil.NEWLINE)
.append(q100)
.append(StringUtil.NEWLINE)
.append("tiny subpages:");
for (int i = 1; i < tinySubpagePools.length; i ++) {
PoolSubpage<T> head = tinySubpagePools[i];
if (head.next == head) {
continue;
}
buf.append(StringUtil.NEWLINE);
buf.append(i);
buf.append(": ");
buf.append(StringUtil.NEWLINE)
.append(i)
.append(": ");
PoolSubpage<T> s = head.next;
for (;;) {
buf.append(s);
@ -353,17 +353,17 @@ abstract class PoolArena<T> {
}
}
}
buf.append(StringUtil.NEWLINE);
buf.append("small subpages:");
buf.append(StringUtil.NEWLINE)
.append("small subpages:");
for (int i = 1; i < smallSubpagePools.length; i ++) {
PoolSubpage<T> head = smallSubpagePools[i];
if (head.next == head) {
continue;
}
buf.append(StringUtil.NEWLINE);
buf.append(i);
buf.append(": ");
buf.append(StringUtil.NEWLINE)
.append(i)
.append(": ");
PoolSubpage<T> s = head.next;
for (;;) {
buf.append(s);

View File

@ -416,16 +416,16 @@ final class PoolChunk<T> {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Chunk(");
buf.append(Integer.toHexString(System.identityHashCode(this)));
buf.append(": ");
buf.append(usage());
buf.append("%, ");
buf.append(chunkSize - freeBytes);
buf.append('/');
buf.append(chunkSize);
buf.append(')');
return buf.toString();
return new StringBuilder()
.append("Chunk(")
.append(Integer.toHexString(System.identityHashCode(this)))
.append(": ")
.append(usage())
.append("%, ")
.append(chunkSize - freeBytes)
.append('/')
.append(chunkSize)
.append(')')
.toString();
}
}

View File

@ -19,8 +19,6 @@ import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
/**
* The default {@link Cookie} implementation.
*/
@ -308,26 +306,26 @@ public class DefaultCookie implements Cookie {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(getName());
buf.append('=');
buf.append(getValue());
StringBuilder buf = new StringBuilder()
.append(getName())
.append('=')
.append(getValue());
if (getDomain() != null) {
buf.append(", domain=");
buf.append(getDomain());
buf.append(", domain=")
.append(getDomain());
}
if (getPath() != null) {
buf.append(", path=");
buf.append(getPath());
buf.append(", path=")
.append(getPath());
}
if (getComment() != null) {
buf.append(", comment=");
buf.append(getComment());
buf.append(", comment=")
.append(getComment());
}
if (getMaxAge() >= 0) {
buf.append(", maxAge=");
buf.append(getMaxAge());
buf.append('s');
buf.append(", maxAge=")
.append(getMaxAge())
.append('s');
}
if (isSecure()) {
buf.append(", secure");

View File

@ -54,14 +54,15 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append("(version: ");
buf.append(getProtocolVersion().text());
buf.append(", keepAlive: ");
buf.append(HttpHeaders.isKeepAlive(this));
buf.append(')');
buf.append(StringUtil.NEWLINE);
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(version: ")
.append(getProtocolVersion().text())
.append(", keepAlive: ")
.append(HttpHeaders.isKeepAlive(this))
.append(')')
.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.
@ -79,11 +80,15 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht
}
void appendHeaders(StringBuilder buf) {
for (Map.Entry<String, String> e: headers()) {
buf.append(e.getKey());
buf.append(": ");
buf.append(e.getValue());
buf.append(StringUtil.NEWLINE);
appendHeaders(buf, headers());
}
void appendHeaders(StringBuilder buf, HttpHeaders headers) {
for (Map.Entry<String, String> e: headers) {
buf.append(e.getKey())
.append(": ")
.append(e.getValue())
.append(StringUtil.NEWLINE);
}
}
}

View File

@ -521,9 +521,9 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
char firstChar = line.charAt(0);
if (name != null && (firstChar == ' ' || firstChar == '\t')) {
StringBuilder buf = new StringBuilder(value.length() + line.length() + 1);
buf.append(value);
buf.append(' ');
buf.append(line.toString().trim());
buf.append(value)
.append(' ')
.append(line.toString().trim());
value = buf.toString();
} else {
if (name != null) {
@ -589,8 +589,8 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
String lineTrimmed = line.toString().trim();
CharSequence currentLastPos = current.get(lastPos);
StringBuilder b = new StringBuilder(currentLastPos.length() + lineTrimmed.length());
b.append(currentLastPos);
b.append(lineTrimmed);
b.append(currentLastPos)
.append(lineTrimmed);
current.set(lastPos, b.toString());
} else {
// Content-Length, Transfer-Encoding, or Trailer

View File

@ -60,9 +60,9 @@ public class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
if (uri.lastIndexOf(SLASH, index) <= startIndex) {
int len = uri.length();
StringBuilder sb = new StringBuilder(len + 1);
sb.append(uri, 0, index);
sb.append(SLASH);
sb.append(uri, index, len);
sb.append(uri, 0, index)
.append(SLASH)
.append(uri, index, len);
uri = sb.toString();
}
}

View File

@ -541,11 +541,11 @@ public class HttpResponseStatus implements Comparable<HttpResponseStatus> {
@Override
public String toString() {
StringBuilder buf = new StringBuilder(reasonPhrase.length() + 5);
buf.append(code);
buf.append(' ');
buf.append(reasonPhrase);
return buf.toString();
return new StringBuilder(reasonPhrase.length() + 5)
.append(code)
.append(' ')
.append(reasonPhrase)
.toString();
}
void encode(ByteBuf buf) {

View File

@ -49,7 +49,6 @@ import static io.netty.buffer.Unpooled.*;
* This encoder will help to encode Request for a FORM as POST.
*/
public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
/**
* Different modes to use to encode form data.
*/
@ -560,42 +559,43 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
globalBodySize -= pastAttribute.size();
StringBuilder replacement = new StringBuilder(
139 + multipartDataBoundary.length() + multipartMixedBoundary.length() * 2 +
fileUpload.getFilename().length() + fileUpload.getName().length());
replacement.append("--");
replacement.append(multipartDataBoundary);
replacement.append("\r\n");
replacement.append("--")
.append(multipartDataBoundary)
.append("\r\n")
replacement.append(HttpPostBodyUtil.CONTENT_DISPOSITION);
replacement.append(": ");
replacement.append(HttpPostBodyUtil.FORM_DATA);
replacement.append("; ");
replacement.append(HttpPostBodyUtil.NAME);
replacement.append("=\"");
replacement.append(fileUpload.getName());
replacement.append("\"\r\n");
.append(HttpPostBodyUtil.CONTENT_DISPOSITION)
.append(": ")
.append(HttpPostBodyUtil.FORM_DATA)
.append("; ")
.append(HttpPostBodyUtil.NAME)
.append("=\"")
.append(fileUpload.getName())
.append("\"\r\n")
replacement.append(HttpHeaders.Names.CONTENT_TYPE);
replacement.append(": ");
replacement.append(HttpPostBodyUtil.MULTIPART_MIXED);
replacement.append("; ");
replacement.append(HttpHeaders.Values.BOUNDARY);
replacement.append('=');
replacement.append(multipartMixedBoundary);
replacement.append("\r\n\r\n");
.append(HttpHeaders.Names.CONTENT_TYPE)
.append(": ")
.append(HttpPostBodyUtil.MULTIPART_MIXED)
.append("; ")
.append(HttpHeaders.Values.BOUNDARY)
.append('=')
.append(multipartMixedBoundary)
.append("\r\n\r\n")
replacement.append("--");
replacement.append(multipartMixedBoundary);
replacement.append("\r\n");
.append("--")
.append(multipartMixedBoundary)
.append("\r\n")
replacement.append(HttpPostBodyUtil.CONTENT_DISPOSITION);
replacement.append(": ");
replacement.append(HttpPostBodyUtil.ATTACHMENT);
replacement.append("; ");
replacement.append(HttpPostBodyUtil.FILENAME);
replacement.append("=\"");
replacement.append(fileUpload.getFilename());
replacement.append("\"\r\n");
.append(HttpPostBodyUtil.CONTENT_DISPOSITION)
.append(": ")
.append(HttpPostBodyUtil.ATTACHMENT)
.append("; ")
.append(HttpPostBodyUtil.FILENAME)
.append("=\"")
.append(fileUpload.getFilename())
.append("\"\r\n");
pastAttribute.setValue(replacement.toString(), 1);
pastAttribute.setValue("", 2);

View File

@ -121,16 +121,16 @@ public class DefaultSpdyDataFrame extends DefaultSpdyStreamFrame implements Spdy
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append("(last: ");
buf.append(isLast());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Size = ");
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(last: ")
.append(isLast())
.append(')')
.append(StringUtil.NEWLINE)
.append("--> Stream-ID = ")
.append(streamId())
.append(StringUtil.NEWLINE)
.append("--> Size = ");
if (refCnt() == 0) {
buf.append("(freed)");
} else {

View File

@ -83,14 +83,14 @@ public class DefaultSpdyGoAwayFrame implements SpdyGoAwayFrame {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append(StringUtil.NEWLINE);
buf.append("--> Last-good-stream-ID = ");
buf.append(lastGoodStreamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Status: ");
buf.append(status());
return buf.toString();
return new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append(StringUtil.NEWLINE)
.append("--> Last-good-stream-ID = ")
.append(lastGoodStreamId())
.append(StringUtil.NEWLINE)
.append("--> Status: ")
.append(status())
.toString();
}
}

View File

@ -79,17 +79,17 @@ public class DefaultSpdyHeadersFrame extends DefaultSpdyStreamFrame
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append("(last: ");
buf.append(isLast());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Headers:");
buf.append(StringUtil.NEWLINE);
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(last: ")
.append(isLast())
.append(')')
.append(StringUtil.NEWLINE)
.append("--> Stream-ID = ")
.append(streamId())
.append(StringUtil.NEWLINE)
.append("--> Headers:")
.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.

View File

@ -46,11 +46,11 @@ public class DefaultSpdyPingFrame implements SpdyPingFrame {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append(StringUtil.NEWLINE);
buf.append("--> ID = ");
buf.append(id());
return buf.toString();
return new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append(StringUtil.NEWLINE)
.append("--> ID = ")
.append(id())
.toString();
}
}

View File

@ -71,14 +71,14 @@ public class DefaultSpdyRstStreamFrame extends DefaultSpdyStreamFrame
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Status: ");
buf.append(status());
return buf.toString();
return new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append(StringUtil.NEWLINE)
.append("--> Stream-ID = ")
.append(streamId())
.append(StringUtil.NEWLINE)
.append("--> Status: ")
.append(status())
.toString();
}
}

View File

@ -152,10 +152,11 @@ public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append(StringUtil.NEWLINE);
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append(StringUtil.NEWLINE);
appendSettings(buf);
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
return buf.toString();
}

View File

@ -52,17 +52,17 @@ public class DefaultSpdySynReplyFrame extends DefaultSpdyHeadersFrame
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append("(last: ");
buf.append(isLast());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Headers:");
buf.append(StringUtil.NEWLINE);
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(last: ")
.append(isLast())
.append(')')
.append(StringUtil.NEWLINE)
.append("--> Stream-ID = ")
.append(streamId())
.append(StringUtil.NEWLINE)
.append("--> Headers:")
.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.

View File

@ -102,27 +102,27 @@ public class DefaultSpdySynStreamFrame extends DefaultSpdyHeadersFrame
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append("(last: ");
buf.append(isLast());
buf.append("; unidirectional: ");
buf.append(isUnidirectional());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamId());
buf.append(StringUtil.NEWLINE);
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append("(last: ")
.append(isLast())
.append("; unidirectional: ")
.append(isUnidirectional())
.append(')')
.append(StringUtil.NEWLINE)
.append("--> Stream-ID = ")
.append(streamId())
.append(StringUtil.NEWLINE);
if (associatedStreamId != 0) {
buf.append("--> Associated-To-Stream-ID = ");
buf.append(associatedStreamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Associated-To-Stream-ID = ")
.append(associatedStreamId())
.append(StringUtil.NEWLINE);
}
buf.append("--> Priority = ");
buf.append(priority());
buf.append(StringUtil.NEWLINE);
buf.append("--> Headers:");
buf.append(StringUtil.NEWLINE);
buf.append("--> Priority = ")
.append(priority())
.append(StringUtil.NEWLINE)
.append("--> Headers:")
.append(StringUtil.NEWLINE);
appendHeaders(buf);
// Remove the last newline.

View File

@ -69,14 +69,14 @@ public class DefaultSpdyWindowUpdateFrame implements SpdyWindowUpdateFrame {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamId());
buf.append(StringUtil.NEWLINE);
buf.append("--> Delta-Window-Size = ");
buf.append(deltaWindowSize());
return buf.toString();
return new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append(StringUtil.NEWLINE)
.append("--> Stream-ID = ")
.append(streamId())
.append(StringUtil.NEWLINE)
.append("--> Delta-Window-Size = ")
.append(deltaWindowSize())
.toString();
}
}

View File

@ -69,12 +69,11 @@ public class DecoderResult {
}
String cause = cause().toString();
StringBuilder buf = new StringBuilder(cause.length() + 17);
buf.append("failure(");
buf.append(cause);
buf.append(')');
return buf.toString();
return new StringBuilder(cause.length() + 17)
.append("failure(")
.append(cause)
.append(')')
.toString();
} else {
return "unfinished";
}

View File

@ -591,17 +591,16 @@ public class HashedWheelTimer implements Timer {
final long currentTime = System.nanoTime();
long remaining = deadline - currentTime + timer.startTime;
StringBuilder buf = new StringBuilder(192);
buf.append(StringUtil.simpleClassName(this));
buf.append('(');
buf.append("deadline: ");
StringBuilder buf = new StringBuilder(192)
.append(StringUtil.simpleClassName(this))
.append('(')
.append("deadline: ");
if (remaining > 0) {
buf.append(remaining);
buf.append(" ns later");
buf.append(remaining)
.append(" ns later");
} else if (remaining < 0) {
buf.append(-remaining);
buf.append(" ns ago");
buf.append(-remaining)
.append(" ns ago");
} else {
buf.append("now");
}
@ -610,10 +609,10 @@ public class HashedWheelTimer implements Timer {
buf.append(", cancelled");
}
buf.append(", task: ");
buf.append(task());
return buf.append(')').toString();
return buf.append(", task: ")
.append(task())
.append(')')
.toString();
}
}

View File

@ -327,27 +327,27 @@ public final class ResourceLeakDetector<T> {
array = lastRecords.toArray();
}
StringBuilder buf = new StringBuilder(16384);
buf.append(NEWLINE);
buf.append("Recent access records: ");
buf.append(array.length);
buf.append(NEWLINE);
StringBuilder buf = new StringBuilder(16384)
.append(NEWLINE)
.append("Recent access records: ")
.append(array.length)
.append(NEWLINE);
if (array.length > 0) {
for (int i = array.length - 1; i >= 0; i --) {
buf.append('#');
buf.append(i + 1);
buf.append(':');
buf.append(NEWLINE);
buf.append(array[i]);
buf.append('#')
.append(i + 1)
.append(':')
.append(NEWLINE)
.append(array[i]);
}
}
buf.append("Created at:");
buf.append(NEWLINE);
buf.append(creationRecord);
buf.setLength(buf.length() - NEWLINE.length());
buf.append("Created at:")
.append(NEWLINE)
.append(creationRecord);
buf.setLength(buf.length() - NEWLINE.length());
return buf.toString();
}
}

View File

@ -806,10 +806,10 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
}
protected StringBuilder toStringBuilder() {
StringBuilder buf = new StringBuilder(64);
buf.append(StringUtil.simpleClassName(this));
buf.append('@');
buf.append(Integer.toHexString(hashCode()));
StringBuilder buf = new StringBuilder(64)
.append(StringUtil.simpleClassName(this))
.append('@')
.append(Integer.toHexString(hashCode()));
Object result = this.result;
if (result == SUCCESS) {
@ -817,9 +817,9 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
} else if (result == UNCANCELLABLE) {
buf.append("(uncancellable)");
} else if (result instanceof CauseHolder) {
buf.append("(failure(");
buf.append(((CauseHolder) result).cause);
buf.append(')');
buf.append("(failure(")
.append(((CauseHolder) result).cause)
.append(')');
} else {
buf.append("(incomplete)");
}

View File

@ -129,9 +129,9 @@ class PromiseTask<V> extends DefaultPromise<V> implements RunnableFuture<V> {
protected StringBuilder toStringBuilder() {
StringBuilder buf = super.toStringBuilder();
buf.setCharAt(buf.length() - 1, ',');
buf.append(" task: ");
buf.append(task);
buf.append(')');
return buf;
return buf.append(" task: ")
.append(task)
.append(')');
}
}

View File

@ -149,13 +149,13 @@ final class ScheduledFutureTask<V> extends PromiseTask<V> implements ScheduledFu
protected StringBuilder toStringBuilder() {
StringBuilder buf = super.toStringBuilder();
buf.setCharAt(buf.length() - 1, ',');
buf.append(" id: ");
buf.append(id);
buf.append(", deadline: ");
buf.append(deadlineNanos);
buf.append(", period: ");
buf.append(periodNanos);
buf.append(')');
return buf;
return buf.append(" id: ")
.append(id)
.append(", deadline: ")
.append(deadlineNanos)
.append(", period: ")
.append(periodNanos)
.append(')');
}
}

View File

@ -239,7 +239,7 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
throw new Error(e);
}
if (!uri.startsWith("/")) {
if (uri.isEmpty() || uri.charAt(0) != '/') {
return null;
}
@ -250,7 +250,7 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
// You will have to do something serious in the production environment.
if (uri.contains(File.separator + '.') ||
uri.contains('.' + File.separator) ||
uri.startsWith(".") || uri.endsWith(".") ||
uri.charAt(0) == '.' || uri.charAt(uri.length() - 1) == '.' ||
INSECURE_URI.matcher(uri).matches()) {
return null;
}
@ -265,21 +265,20 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
StringBuilder buf = new StringBuilder();
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")
buf.append("<!DOCTYPE html>\r\n");
buf.append("<html><head><title>");
buf.append("Listing of: ");
buf.append(dirPath);
buf.append("</title></head><body>\r\n");
.append("<h3>Listing of: ")
.append(dirPath)
.append("</h3>\r\n")
buf.append("<h3>Listing of: ");
buf.append(dirPath);
buf.append("</h3>\r\n");
buf.append("<ul>");
buf.append("<li><a href=\"../\">..</a></li>\r\n");
.append("<ul>")
.append("<li><a href=\"../\">..</a></li>\r\n");
for (File f: dir.listFiles()) {
if (f.isHidden() || !f.canRead()) {
@ -291,11 +290,11 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
continue;
}
buf.append("<li><a href=\"");
buf.append(name);
buf.append("\">");
buf.append(name);
buf.append("</a></li>\r\n");
buf.append("<li><a href=\"")
.append(name)
.append("\">")
.append(name)
.append("</a></li>\r\n");
}
buf.append("</ul></body></html>\r\n");

View File

@ -66,9 +66,13 @@ public class SpdyFrameLogger extends ChannelDuplexHandler {
private void log(SpdyFrame msg, Direction d) {
if (logger.isEnabled(level)) {
StringBuilder b = new StringBuilder("\n----------------").append(d.name()).append("--------------------\n");
b.append(msg);
b.append("\n------------------------------------");
StringBuilder b = new StringBuilder(200)
.append("\n----------------")
.append(d.name())
.append("--------------------\n")
.append(msg)
.append("\n------------------------------------");
logger.log(level, b.toString());
}
}

View File

@ -170,11 +170,11 @@ public class LoggingHandler extends ChannelDuplexHandler {
protected String format(ChannelHandlerContext ctx, String message) {
String chStr = ctx.channel().toString();
StringBuilder buf = new StringBuilder(chStr.length() + message.length() + 1);
buf.append(chStr);
buf.append(' ');
buf.append(message);
return buf.toString();
return new StringBuilder(chStr.length() + message.length() + 1)
.append(chStr)
.append(' ')
.append(message)
.toString();
}
@Override
@ -319,10 +319,10 @@ public class LoggingHandler extends ChannelDuplexHandler {
protected String formatByteBuf(String eventName, ByteBuf buf) {
int length = buf.readableBytes();
int rows = length / 16 + (length % 15 == 0? 0 : 1) + 4;
StringBuilder dump = new StringBuilder(rows * 80 + eventName.length() + 16);
StringBuilder dump = new StringBuilder(rows * 80 + eventName.length() + 16)
dump.append(eventName).append('(').append(length).append('B').append(')');
dump.append(
.append(eventName).append('(').append(length).append('B').append(')')
.append(
NEWLINE + " +-------------------------------------------------+" +
NEWLINE + " | 0 1 2 3 4 5 6 7 8 9 a b c d e f |" +
NEWLINE + "+--------+-------------------------------------------------+----------------+");
@ -335,9 +335,9 @@ public class LoggingHandler extends ChannelDuplexHandler {
int relIdx = i - startIndex;
int relIdxMod16 = relIdx & 15;
if (relIdxMod16 == 0) {
dump.append(NEWLINE);
dump.append(Long.toHexString(relIdx & 0xFFFFFFFFL | 0x100000000L));
dump.setCharAt(dump.length() - 9, '|');
dump.append(NEWLINE)
.append(Long.toHexString(relIdx & 0xFFFFFFFFL | 0x100000000L))
.setCharAt(dump.length() - 9, '|');
dump.append('|');
}
dump.append(BYTE2HEX[buf.getUnsignedByte(i)]);
@ -352,13 +352,13 @@ public class LoggingHandler extends ChannelDuplexHandler {
if ((i - startIndex & 15) != 0) {
int remainder = length & 15;
dump.append(HEXPADDING[remainder]);
dump.append(" |");
dump.append(HEXPADDING[remainder])
.append(" |");
for (int j = i - remainder; j < i; j ++) {
dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
}
dump.append(BYTEPADDING[remainder]);
dump.append('|');
dump.append(BYTEPADDING[remainder])
.append('|');
}
dump.append(
@ -384,3 +384,4 @@ public class LoggingHandler extends ChannelDuplexHandler {
return formatByteBuf(eventName, msg.content());
}
}

View File

@ -191,11 +191,11 @@ public final class UnitHelp {
* Display current OS/ARCH.
*/
public static void logOsArch() {
final StringBuilder text = new StringBuilder(1024);
text.append("\n\t");
text.append(System.getProperty("os.name"));
text.append("\n\t");
text.append(System.getProperty("os.arch"));
final StringBuilder text = new StringBuilder(1024)
.append("\n\t")
.append(System.getProperty("os.name"))
.append("\n\t")
.append(System.getProperty("os.arch"));
log.info("\n\t[os/arch]{}", text);
}

View File

@ -396,42 +396,42 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append('(');
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append('(');
if (group != null) {
buf.append("group: ");
buf.append(StringUtil.simpleClassName(group));
buf.append(", ");
buf.append("group: ")
.append(StringUtil.simpleClassName(group))
.append(", ");
}
if (channelFactory != null) {
buf.append("channelFactory: ");
buf.append(channelFactory);
buf.append(", ");
buf.append("channelFactory: ")
.append(channelFactory)
.append(", ");
}
if (localAddress != null) {
buf.append("localAddress: ");
buf.append(localAddress);
buf.append(", ");
buf.append("localAddress: ")
.append(localAddress)
.append(", ");
}
synchronized (options) {
if (!options.isEmpty()) {
buf.append("options: ");
buf.append(options);
buf.append(", ");
buf.append("options: ")
.append(options)
.append(", ");
}
}
synchronized (attrs) {
if (!attrs.isEmpty()) {
buf.append("attrs: ");
buf.append(attrs);
buf.append(", ");
buf.append("attrs: ")
.append(attrs)
.append(", ");
}
}
if (handler != null) {
buf.append("handler: ");
buf.append(handler);
buf.append(", ");
buf.append("handler: ")
.append(handler)
.append(", ");
}
if (buf.charAt(buf.length() - 1) == '(') {
buf.append(')');

View File

@ -224,10 +224,10 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
StringBuilder buf = new StringBuilder(super.toString());
buf.setLength(buf.length() - 1);
buf.append(", remoteAddress: ");
buf.append(remoteAddress);
buf.append(')');
return buf.toString();
return buf.append(", remoteAddress: ")
.append(remoteAddress)
.append(')')
.toString();
}
}

View File

@ -701,20 +701,20 @@ final class DefaultChannelPipeline implements ChannelPipeline {
*/
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(StringUtil.simpleClassName(this));
buf.append('{');
StringBuilder buf = new StringBuilder()
.append(StringUtil.simpleClassName(this))
.append('{');
AbstractChannelHandlerContext ctx = head.next;
for (;;) {
if (ctx == tail) {
break;
}
buf.append('(');
buf.append(ctx.name());
buf.append(" = ");
buf.append(ctx.handler().getClass().getName());
buf.append(')');
buf.append('(')
.append(ctx.name())
.append(" = ")
.append(ctx.handler().getClass().getName())
.append(')');
ctx = ctx.next;
if (ctx == tail) {