/hudson-core/src/main/java/hudson/util/ChunkedOutputStream.java

http://github.com/hudson/hudson · Java · 203 lines · 76 code · 22 blank · 105 comment · 6 complexity · 2407ff302e04ae55f3a3b7ab22659f96 MD5 · raw file

  1. /*
  2. * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/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: 480424 $
  4. * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
  5. *
  6. * ====================================================================
  7. *
  8. * Licensed to the Apache Software Foundation (ASF) under one or more
  9. * contributor license agreements. See the NOTICE file distributed with
  10. * this work for additional information regarding copyright ownership.
  11. * The ASF licenses this file to You under the Apache License, Version 2.0
  12. * (the "License"); you may not use this file except in compliance with
  13. * the License. You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ====================================================================
  23. *
  24. * This software consists of voluntary contributions made by many
  25. * individuals on behalf of the Apache Software Foundation. For more
  26. * information on the Apache Software Foundation, please see
  27. * <http://www.apache.org/>.
  28. *
  29. */
  30. package hudson.util;
  31. import java.io.IOException;
  32. import java.io.OutputStream;
  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[] = (Integer.toHexString(cachePosition) + "\r\n").getBytes("US-ASCII");
  83. stream.write(chunkHeader, 0, chunkHeader.length);
  84. stream.write(cache, 0, cachePosition);
  85. stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  86. cachePosition = 0;
  87. }
  88. }
  89. /**
  90. * Writes the cache and bufferToAppend to the underlying stream
  91. * as one large chunk
  92. * @param bufferToAppend
  93. * @param off
  94. * @param len
  95. * @throws IOException
  96. *
  97. * @since 3.0
  98. */
  99. protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException {
  100. byte chunkHeader[] = (Integer.toHexString(cachePosition + len) + "\r\n").getBytes("US-ASCII");
  101. stream.write(chunkHeader, 0, chunkHeader.length);
  102. stream.write(cache, 0, cachePosition);
  103. stream.write(bufferToAppend, off, len);
  104. stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  105. cachePosition = 0;
  106. }
  107. protected void writeClosingChunk() throws IOException {
  108. // Write the final chunk.
  109. stream.write(ZERO, 0, ZERO.length);
  110. stream.write(CRLF, 0, CRLF.length);
  111. stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  112. }
  113. // ----------------------------------------------------------- Public Methods
  114. /**
  115. * Must be called to ensure the internal cache is flushed and the closing chunk is written.
  116. * @throws IOException
  117. *
  118. * @since 3.0
  119. */
  120. public void finish() throws IOException {
  121. if (!wroteLastChunk) {
  122. flushCache();
  123. writeClosingChunk();
  124. wroteLastChunk = true;
  125. }
  126. }
  127. // -------------------------------------------- OutputStream Methods
  128. /**
  129. * Write the specified byte to our output stream.
  130. *
  131. * Note: Avoid this method as it will cause an inefficient single byte chunk.
  132. * Use write (byte[], int, int) instead.
  133. *
  134. * @param b The byte to be written
  135. * @throws IOException if an input/output error occurs
  136. */
  137. public void write(int b) throws IOException {
  138. cache[cachePosition] = (byte) b;
  139. cachePosition++;
  140. if (cachePosition == cache.length) flushCache();
  141. }
  142. /**
  143. * Writes the array. If the array does not fit within the buffer, it is
  144. * not split, but rather written out as one large chunk.
  145. * @param b
  146. * @throws IOException
  147. *
  148. * @since 3.0
  149. */
  150. @Override
  151. public void write(byte b[]) throws IOException {
  152. this.write(b, 0, b.length);
  153. }
  154. @Override
  155. public void write(byte src[], int off, int len) throws IOException {
  156. if (len >= cache.length - cachePosition) {
  157. flushCacheWithAppend(src, off, len);
  158. } else {
  159. System.arraycopy(src, off, cache, cachePosition, len);
  160. cachePosition += len;
  161. }
  162. }
  163. /**
  164. * Flushes the underlying stream, but leaves the internal buffer alone.
  165. * @throws IOException
  166. */
  167. @Override
  168. public void flush() throws IOException {
  169. flushCache(); // Kohsuke: flush should flush the cache
  170. stream.flush();
  171. }
  172. /**
  173. * Finishes writing to the underlying stream, but does NOT close the underlying stream.
  174. * @throws IOException
  175. */
  176. @Override
  177. public void close() throws IOException {
  178. finish();
  179. super.close();
  180. }
  181. }