View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  
33  import org.apache.http.annotation.NotThreadSafe;
34  import org.apache.http.io.SessionOutputBuffer;
35  
36  /**
37   * Implements chunked transfer coding. The content is sent in small chunks.
38   * Entities transferred using this output stream can be of unlimited length.
39   * Writes are buffered to an internal buffer (2048 default size).
40   * <p>
41   * Note that this class NEVER closes the underlying stream, even when close
42   * gets called.  Instead, the stream will be marked as closed and no further
43   * output will be permitted.
44   *
45   *
46   * @since 4.0
47   */
48  @NotThreadSafe
49  public class ChunkedOutputStream extends OutputStream {
50  
51      // ----------------------------------------------------- Instance Variables
52      private final SessionOutputBuffer out;
53  
54      private byte[] cache;
55  
56      private int cachePosition = 0;
57  
58      private boolean wroteLastChunk = false;
59  
60      /** True if the stream is closed. */
61      private boolean closed = false;
62  
63      // ----------------------------------------------------------- Constructors
64      /**
65       * Wraps a session output buffer and chunk-encodes the output.
66       *
67       * @param out The session output buffer
68       * @param bufferSize The minimum chunk size (excluding last chunk)
69       * @throws IOException in case of an I/O error
70       */
71      public ChunkedOutputStream(final SessionOutputBuffer out, int bufferSize)
72              throws IOException {
73          super();
74          this.cache = new byte[bufferSize];
75          this.out = out;
76      }
77  
78      /**
79       * Wraps a session output buffer and chunks the output. The default buffer
80       * size of 2048 was chosen because the chunk overhead is less than 0.5%
81       *
82       * @param out       the output buffer to wrap
83       * @throws IOException in case of an I/O error
84       */
85      public ChunkedOutputStream(final SessionOutputBuffer out)
86              throws IOException {
87          this(out, 2048);
88      }
89  
90      // ----------------------------------------------------------- Internal methods
91      /**
92       * Writes the cache out onto the underlying stream
93       */
94      protected void flushCache() throws IOException {
95          if (this.cachePosition > 0) {
96              this.out.writeLine(Integer.toHexString(this.cachePosition));
97              this.out.write(this.cache, 0, this.cachePosition);
98              this.out.writeLine("");
99              this.cachePosition = 0;
100         }
101     }
102 
103     /**
104      * Writes the cache and bufferToAppend to the underlying stream
105      * as one large chunk
106      */
107     protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException {
108         this.out.writeLine(Integer.toHexString(this.cachePosition + len));
109         this.out.write(this.cache, 0, this.cachePosition);
110         this.out.write(bufferToAppend, off, len);
111         this.out.writeLine("");
112         this.cachePosition = 0;
113     }
114 
115     protected void writeClosingChunk() throws IOException {
116         // Write the final chunk.
117         this.out.writeLine("0");
118         this.out.writeLine("");
119     }
120 
121     // ----------------------------------------------------------- Public Methods
122     /**
123      * Must be called to ensure the internal cache is flushed and the closing
124      * chunk is written.
125      * @throws IOException in case of an I/O error
126      */
127     public void finish() throws IOException {
128         if (!this.wroteLastChunk) {
129             flushCache();
130             writeClosingChunk();
131             this.wroteLastChunk = true;
132         }
133     }
134 
135     // -------------------------------------------- OutputStream Methods
136     @Override
137     public void write(int b) throws IOException {
138         if (this.closed) {
139             throw new IOException("Attempted write to closed stream.");
140         }
141         this.cache[this.cachePosition] = (byte) b;
142         this.cachePosition++;
143         if (this.cachePosition == this.cache.length) flushCache();
144     }
145 
146     /**
147      * Writes the array. If the array does not fit within the buffer, it is
148      * not split, but rather written out as one large chunk.
149      */
150     @Override
151     public void write(byte b[]) throws IOException {
152         write(b, 0, b.length);
153     }
154 
155     /**
156      * Writes the array. If the array does not fit within the buffer, it is
157      * not split, but rather written out as one large chunk.
158      */
159     @Override
160     public void write(byte src[], int off, int len) throws IOException {
161         if (this.closed) {
162             throw new IOException("Attempted write to closed stream.");
163         }
164         if (len >= this.cache.length - this.cachePosition) {
165             flushCacheWithAppend(src, off, len);
166         } else {
167             System.arraycopy(src, off, cache, this.cachePosition, len);
168             this.cachePosition += len;
169         }
170     }
171 
172     /**
173      * Flushes the content buffer and the underlying stream.
174      */
175     @Override
176     public void flush() throws IOException {
177         flushCache();
178         this.out.flush();
179     }
180 
181     /**
182      * Finishes writing to the underlying stream, but does NOT close the underlying stream.
183      */
184     @Override
185     public void close() throws IOException {
186         if (!this.closed) {
187             this.closed = true;
188             finish();
189             this.out.flush();
190         }
191     }
192 }