1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java,v 1.16 2004/05/13 04:03:25 mbecke Exp $
  3. * $Revision: 1.16 $
  4. * $Date: 2004/05/13 04:03:25 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient;
  30. import java.io.IOException;
  31. import java.io.OutputStream;
  32. import org.apache.commons.httpclient.util.EncodingUtil;
  33. /**
  34. * Implements HTTP chunking support. Writes are buffered to an internal buffer (2048 default size).
  35. * Chunks are guaranteed to be at least as large as the buffer size (except for the last chunk).
  36. *
  37. * @author Mohammad Rezaei, Goldman, Sachs & Co.
  38. */
  39. public class ChunkedOutputStream extends OutputStream {
  40. // ------------------------------------------------------- Static Variables
  41. private static final byte CRLF[] = new byte[] {(byte) 13, (byte) 10};
  42. /** End chunk */
  43. private static final byte ENDCHUNK[] = CRLF;
  44. /** 0 */
  45. private static final byte ZERO[] = new byte[] {(byte) '0'};
  46. // ----------------------------------------------------- Instance Variables
  47. private OutputStream stream = null;
  48. private byte[] cache;
  49. private int cachePosition = 0;
  50. private boolean wroteLastChunk = false;
  51. // ----------------------------------------------------------- Constructors
  52. /**
  53. * Wraps a stream and chunks the output.
  54. * @param stream to wrap
  55. * @param bufferSize minimum chunk size (excluding last chunk)
  56. * @throws IOException
  57. *
  58. * @since 3.0
  59. */
  60. public ChunkedOutputStream(OutputStream stream, int bufferSize) throws IOException {
  61. this.cache = new byte[bufferSize];
  62. this.stream = stream;
  63. }
  64. /**
  65. * Wraps a stream and chunks the output. The default buffer size of 2048 was chosen because
  66. * the chunk overhead is less than 0.5%
  67. * @param stream
  68. * @throws IOException
  69. */
  70. public ChunkedOutputStream(OutputStream stream) throws IOException {
  71. this(stream, 2048);
  72. }
  73. // ----------------------------------------------------------- Internal methods
  74. /**
  75. * Writes the cache out onto the underlying stream
  76. * @throws IOException
  77. *
  78. * @since 3.0
  79. */
  80. protected void flushCache() throws IOException {
  81. if (cachePosition > 0) {
  82. byte chunkHeader[] = EncodingUtil.getAsciiBytes(
  83. Integer.toHexString(cachePosition) + "\r\n");
  84. stream.write(chunkHeader, 0, chunkHeader.length);
  85. stream.write(cache, 0, cachePosition);
  86. stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  87. cachePosition = 0;
  88. }
  89. }
  90. /**
  91. * Writes the cache and bufferToAppend to the underlying stream
  92. * as one large chunk
  93. * @param bufferToAppend
  94. * @param off
  95. * @param len
  96. * @throws IOException
  97. *
  98. * @since 3.0
  99. */
  100. protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException {
  101. byte chunkHeader[] = EncodingUtil.getAsciiBytes(
  102. Integer.toHexString(cachePosition + len) + "\r\n");
  103. stream.write(chunkHeader, 0, chunkHeader.length);
  104. stream.write(cache, 0, cachePosition);
  105. stream.write(bufferToAppend, off, len);
  106. stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  107. cachePosition = 0;
  108. }
  109. protected void writeClosingChunk() throws IOException {
  110. // Write the final chunk.
  111. stream.write(ZERO, 0, ZERO.length);
  112. stream.write(CRLF, 0, CRLF.length);
  113. stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  114. }
  115. // ----------------------------------------------------------- Public Methods
  116. /**
  117. * Must be called to ensure the internal cache is flushed and the closing chunk is written.
  118. * @throws IOException
  119. *
  120. * @since 3.0
  121. */
  122. public void finish() throws IOException {
  123. if (!wroteLastChunk) {
  124. flushCache();
  125. writeClosingChunk();
  126. wroteLastChunk = true;
  127. }
  128. }
  129. // -------------------------------------------- OutputStream Methods
  130. public void write(int b) throws IOException {
  131. cache[cachePosition] = (byte) b;
  132. cachePosition++;
  133. if (cachePosition == cache.length) flushCache();
  134. }
  135. /**
  136. * Writes the array. If the array does not fit within the buffer, it is
  137. * not split, but rather written out as one large chunk.
  138. * @param b
  139. * @throws IOException
  140. *
  141. * @since 3.0
  142. */
  143. public void write(byte b[]) throws IOException {
  144. this.write(b, 0, b.length);
  145. }
  146. public void write(byte src[], int off, int len) throws IOException {
  147. if (len >= cache.length - cachePosition) {
  148. flushCacheWithAppend(src, off, len);
  149. } else {
  150. System.arraycopy(src, off, cache, cachePosition, len);
  151. cachePosition += len;
  152. }
  153. }
  154. /**
  155. * Flushes the underlying stream, but leaves the internal buffer alone.
  156. * @throws IOException
  157. */
  158. public void flush() throws IOException {
  159. stream.flush();
  160. }
  161. /**
  162. * Finishes writing to the underlying stream, but does NOT close the underlying stream.
  163. * @throws IOException
  164. */
  165. public void close() throws IOException {
  166. finish();
  167. super.close();
  168. }
  169. }