Author: olegk Date: Sat Dec 16 06:14:16 2006 New Revision: 487802 URL: http://svn.apache.org/viewvc?view=rev&rev=487802 Log: * Small tweaks to the original Odi's patch * Added #getResponseBody(int) method for consistency
Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpMethodBase.java Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpMethodBase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpMethodBase.java?view=diff&rev=487802&r1=487801&r2=487802 ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpMethodBase.java (original) +++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpMethodBase.java Sat Dec 16 06:14:16 2006 @@ -34,9 +34,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.InterruptedIOException; -import java.io.Reader; import java.util.Collection; import org.apache.commons.httpclient.auth.AuthState; @@ -699,6 +697,63 @@ } /** + * Returns the response body of the HTTP method, if any, as an array of bytes. + * If response body is not available or cannot be read, returns <tt>null</tt> + * + * Note: This will cause the entire response body to be buffered in memory. This method is + * safe if the content length of the response is unknown, because the amount of memory used + * is limited.<p> + * + * If the response is large this method involves lots of array copying and many object + * allocations, which makes it unsuitable for high-performance / low-footprint applications. + * Those applications should use [EMAIL PROTECTED] #getResponseBodyAsStream()}. + * + * @param maxlen the maximum content length to accept (number of bytes). + * @return The response body. + * + * @throws IOException If an I/O (transport) problem occurs while obtaining the + * response body. + */ + public byte[] getResponseBody(int maxlen) throws IOException { + if (maxlen < 0) throw new IllegalArgumentException("maxlen must be positive"); + if (this.responseBody == null) { + InputStream instream = getResponseBodyAsStream(); + if (instream != null) { + // we might already know that the content is larger + long contentLength = getResponseContentLength(); + if ((contentLength != -1) && (contentLength > maxlen)) { + throw new HttpContentTooLargeException( + "Content-Length is " + contentLength, maxlen); + } + + LOG.debug("Buffering response body"); + ByteArrayOutputStream rawdata = new ByteArrayOutputStream( + contentLength > 0 ? (int) contentLength : DEFAULT_INITIAL_BUFFER_SIZE); + byte[] buffer = new byte[2048]; + int pos = 0; + int len; + do { + len = instream.read(buffer, 0, Math.min(buffer.length, maxlen-pos)); + if (len == -1) break; + rawdata.write(buffer, 0, len); + pos += len; + } while (pos < maxlen); + + setResponseStream(null); + // check if there is even more data + if (pos == maxlen) { + if (instream.read() != -1) + throw new HttpContentTooLargeException( + "Content-Length not known but larger than " + + maxlen, maxlen); + } + this.responseBody = rawdata.toByteArray(); + } + } + return this.responseBody; + } + + /** * Returns the response body of the HTTP method, if any, as an [EMAIL PROTECTED] InputStream}. * If response body is not available, returns <tt>null</tt> * @@ -770,36 +825,15 @@ */ public String getResponseBodyAsString(int maxlen) throws IOException { if (maxlen < 0) throw new IllegalArgumentException("maxlen must be positive"); - - // we might already know that the content is larger - long contentLength = getResponseContentLength(); - if ((contentLength != -1) && (contentLength > maxlen)) { - throw new HttpContentTooLargeException("Content-Length is "+ contentLength, maxlen); + byte[] rawdata = null; + if (responseAvailable()) { + rawdata = getResponseBody(maxlen); } - - LOG.debug("Buffering response body"); - ByteArrayOutputStream rawdata = new ByteArrayOutputStream( - contentLength > 0 ? (int) contentLength : DEFAULT_INITIAL_BUFFER_SIZE); - InputStream in = getResponseBodyAsStream(); - if (in == null) return null; - byte[] buffer = new byte[2048]; - int pos = 0; - int len; - do { - len = in.read(buffer, 0, Math.min(buffer.length, maxlen-pos)); - if (len == -1) break; - rawdata.write(buffer, 0, len); - pos += len; - } while (pos < maxlen); - - // check if there is even more data - if (pos == maxlen) { - if (in.read() != -1) - throw new HttpContentTooLargeException("Content-Length not known but larger than " - + maxlen, maxlen); + if (rawdata != null) { + return EncodingUtil.getString(rawdata, getResponseCharSet()); + } else { + return null; } - - return EncodingUtil.getString(rawdata.toByteArray(), 0, pos, getResponseCharSet()); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]