/** * Writes this messages body to the given output stream. This method may only be called once * during the lifetime of this message. * * @param os an output stream * @exception IOException if there is an I/O error */ public void write(OutputStream os) throws IOException { Assert.isTrue(!inputRead); Assert.isTrue(!hasInputStream); int bytesRead = 0; int totalBytesRead = 0; byte[] buffer = bufferPool.getBuffer(); long contentLength = getContentLength(); try { while (bytesRead != -1 && (contentLength == -1 || contentLength > totalBytesRead)) { if (contentLength == -1) { bytesRead = is.read(buffer); } else { bytesRead = is.read(buffer, 0, (int) Math.min(buffer.length, contentLength - totalBytesRead)); } if (bytesRead == -1) { if (contentLength >= 0) { throw new IOException(Policy.bind("exception.unexpectedEndStream")); // $NON-NLS-1$ } } else { totalBytesRead += bytesRead; os.write(buffer, 0, bytesRead); } } } finally { bufferPool.putBuffer(buffer); inputRead = true; } }