You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2018/08/14 07:51:43 UTC
[08/12] httpcomponents-core git commit: Better exception messages. No
need to break up exception message strings.
Better exception messages. No need to break up exception message
strings.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/ea64b18a
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/ea64b18a
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/ea64b18a
Branch: refs/heads/4.4.x
Commit: ea64b18a3d560b4758759a9290a42edf7d7978c7
Parents: 9dc8fe8
Author: Gary Gregory <ga...@gmail.com>
Authored: Mon Aug 13 17:10:46 2018 -0600
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Tue Aug 14 09:49:35 2018 +0200
----------------------------------------------------------------------
.../org/apache/http/impl/nio/codecs/ChunkDecoder.java | 10 +++++-----
.../http/impl/nio/codecs/LengthDelimitedDecoder.java | 14 +++++---------
.../org/apache/http/ConnectionClosedException.java | 12 ++++++++++++
.../java/org/apache/http/TruncatedChunkException.java | 12 ++++++++++++
.../org/apache/http/impl/io/ChunkedInputStream.java | 9 ++++-----
.../apache/http/impl/io/ContentLengthInputStream.java | 8 ++++----
.../http/impl/io/ContentLengthOutputStream.java | 4 ++--
7 files changed, 44 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
index ede54dd..5bc3291 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
@@ -135,8 +135,8 @@ public class ChunkDecoder extends AbstractContentDecoder {
}
this.pos = 0L;
} else if (this.endOfStream) {
- throw new ConnectionClosedException("Premature end of chunk coded message body: " +
- "closing chunk expected");
+ throw new ConnectionClosedException(
+ "Premature end of chunk coded message body: closing chunk expected");
}
}
@@ -223,9 +223,9 @@ public class ChunkDecoder extends AbstractContentDecoder {
if (!this.buffer.hasData() && this.endOfStream) {
this.state = COMPLETED;
this.completed = true;
- throw new TruncatedChunkException("Truncated chunk "
- + "( expected size: " + this.chunkSize
- + "; actual size: " + this.pos + ")");
+ throw new TruncatedChunkException(
+ "Truncated chunk (expected size: %,d; actual size: %,d)",
+ chunkSize, pos);
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java
index 64e223a..1493f3d 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java
@@ -87,19 +87,15 @@ public class LengthDelimitedDecoder extends AbstractContentDecoder
this.completed = true;
if (this.len < this.contentLength) {
throw new ConnectionClosedException(
- "Premature end of Content-Length delimited message body (expected: "
- + this.contentLength + "; received: " + this.len + ")");
+ "Premature end of Content-Length delimited message body (expected: %,d; received: %,d)",
+ contentLength, len);
}
}
this.len += bytesRead;
if (this.len >= this.contentLength) {
this.completed = true;
}
- if (this.completed && bytesRead == 0) {
- return -1;
- } else {
- return bytesRead;
- }
+ return this.completed && bytesRead == 0 ? -1 : bytesRead;
}
@Override
@@ -140,8 +136,8 @@ public class LengthDelimitedDecoder extends AbstractContentDecoder
this.completed = true;
if (this.len < this.contentLength) {
throw new ConnectionClosedException(
- "Premature end of Content-Length delimited message body (expected: "
- + this.contentLength + "; received: " + this.len + ")");
+ "Premature end of Content-Length delimited message body (expected: %,d; received: %,d)",
+ contentLength, len);
}
}
this.len += bytesRead;
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java b/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
index 0599181..7e406a5 100644
--- a/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
+++ b/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
@@ -56,4 +56,16 @@ public class ConnectionClosedException extends IOException {
super(HttpException.clean(message));
}
+ /**
+ * Constructs a new ConnectionClosedException with the specified detail message.
+ *
+ * @param format The exception detail message format; see {@link String#format(String, Object...)}.
+ * @param args The exception detail message arguments; see {@link String#format(String, Object...)}.
+ *
+ * @since 4.4.11
+ */
+ public ConnectionClosedException(final String format, final Object... args) {
+ super(HttpException.clean(String.format(format, args)));
+ }
+
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore/src/main/java/org/apache/http/TruncatedChunkException.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/TruncatedChunkException.java b/httpcore/src/main/java/org/apache/http/TruncatedChunkException.java
index 0458f8e..671efb4 100644
--- a/httpcore/src/main/java/org/apache/http/TruncatedChunkException.java
+++ b/httpcore/src/main/java/org/apache/http/TruncatedChunkException.java
@@ -45,4 +45,16 @@ public class TruncatedChunkException extends MalformedChunkCodingException {
super(message);
}
+ /**
+ * Constructs a new TruncatedChunkException with the specified detail message.
+ *
+ * @param format The exception detail message format; see {@link String#format(String, Object...)}.
+ * @param args The exception detail message arguments; see {@link String#format(String, Object...)}.
+ *
+ * @since 4.4.11
+ */
+ public TruncatedChunkException(final String format, final Object... args) {
+ super(HttpException.clean(String.format(format, args)));
+ }
+
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java b/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
index f4f62fa..68fe740 100644
--- a/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
+++ b/httpcore/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
@@ -195,9 +195,8 @@ public class ChunkedInputStream extends InputStream {
return readLen;
}
eof = true;
- throw new TruncatedChunkException("Truncated chunk "
- + "( expected size: " + chunkSize
- + "; actual size: " + pos + ")");
+ throw new TruncatedChunkException("Truncated chunk (expected size: %,d; actual size: %,d)",
+ chunkSize, pos);
}
/**
@@ -262,8 +261,8 @@ public class ChunkedInputStream extends InputStream {
this.buffer.clear();
final int bytesRead2 = this.in.readLine(this.buffer);
if (bytesRead2 == -1) {
- throw new ConnectionClosedException("Premature end of chunk coded message body: " +
- "closing chunk expected");
+ throw new ConnectionClosedException(
+ "Premature end of chunk coded message body: closing chunk expected");
}
int separator = this.buffer.indexOf(';');
if (separator < 0) {
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java b/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
index 6bfbacd..c4d8848 100644
--- a/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
+++ b/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthInputStream.java
@@ -138,8 +138,8 @@ public class ContentLengthInputStream extends InputStream {
if (b == -1) {
if (pos < contentLength) {
throw new ConnectionClosedException(
- "Premature end of Content-Length delimited message body (expected: "
- + contentLength + "; received: " + pos + ")");
+ "Premature end of Content-Length delimited message body (expected: %,d; received: %,d)",
+ contentLength, pos);
}
} else {
pos++;
@@ -176,8 +176,8 @@ public class ContentLengthInputStream extends InputStream {
final int readLen = this.in.read(b, off, chunk);
if (readLen == -1 && pos < contentLength) {
throw new ConnectionClosedException(
- "Premature end of Content-Length delimited message body (expected: "
- + contentLength + "; received: " + pos + ")");
+ "Premature end of Content-Length delimited message body (expected: %,d; received: %,d)",
+ contentLength, pos);
}
if (readLen > 0) {
pos += readLen;
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ea64b18a/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthOutputStream.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthOutputStream.java b/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthOutputStream.java
index 184c917..3dc10bd 100644
--- a/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthOutputStream.java
+++ b/httpcore/src/main/java/org/apache/http/impl/io/ContentLengthOutputStream.java
@@ -60,10 +60,10 @@ public class ContentLengthOutputStream extends OutputStream {
private final long contentLength;
/** Total bytes written */
- private long total = 0;
+ private long total;
/** True if the stream is closed. */
- private boolean closed = false;
+ private boolean closed;
/**
* Wraps a session output buffer and cuts off output after a defined number