25 package sun.net.www.http;
26
27 import java.io.*;
28
29 /**
30 * OutputStream that sends the output to the underlying stream using chunked
31 * encoding as specified in RFC 2068.
32 */
33 public class ChunkedOutputStream extends PrintStream {
34
35 /* Default chunk size (including chunk header) if not specified */
36 static final int DEFAULT_CHUNK_SIZE = 4096;
37 private static final byte[] CRLF = {'\r', '\n'};
38 private static final int CRLF_SIZE = CRLF.length;
39 private static final byte[] FOOTER = CRLF;
40 private static final int FOOTER_SIZE = CRLF_SIZE;
41 private static final byte[] EMPTY_CHUNK_HEADER = getHeader(0);
42 private static final int EMPTY_CHUNK_HEADER_SIZE = getHeaderSize(0);
43
44 /* internal buffer */
45 private byte buf[];
46 /* size of data (excluding footers and headers) already stored in buf */
47 private int size;
48 /* current index in buf (i.e. buf[count] */
49 private int count;
50 /* number of bytes to be filled up to complete a data chunk
51 * currently being built */
52 private int spaceInCurrentChunk;
53
54 /* underlying stream */
55 private PrintStream out;
56
57 /* the chunk size we use */
58 private int preferredChunkDataSize;
59 private int preferedHeaderSize;
60 private int preferredChunkGrossSize;
61 /* header for a complete Chunk */
62 private byte[] completeHeader;
63
64 /* return the size of the header for a particular chunk size */
65 private static int getHeaderSize(int size) {
180 }
181
182 /* Check that the output stream is still open */
183 private void ensureOpen() {
184 if (out == null)
185 setError();
186 }
187
188 /*
189 * Writes data from b[] to an internal buffer and stores the data as data
190 * chunks of a following format: {Data length in Hex}{CRLF}{data}{CRLF}
191 * The size of the data is preferredChunkSize. As soon as a completed chunk
192 * is read from b[] a process of reading from b[] suspends, the chunk gets
193 * flushed to the underlying stream and then the reading process from b[]
194 * continues. When there is no more sufficient data in b[] to build up a
195 * chunk of preferredChunkSize size the data get stored as an incomplete
196 * chunk of a following format: {space for data length}{CRLF}{data}
197 * The size of the data is of course smaller than preferredChunkSize.
198 */
199 @Override
200 public synchronized void write(byte b[], int off, int len) {
201 ensureOpen();
202 if ((off < 0) || (off > b.length) || (len < 0) ||
203 ((off + len) > b.length) || ((off + len) < 0)) {
204 throw new IndexOutOfBoundsException();
205 } else if (len == 0) {
206 return;
207 }
208
209 /* if b[] contains enough data then one loop cycle creates one complete
210 * data chunk with a header, body and a footer, and then flushes the
211 * chunk to the underlying stream. Otherwise, the last loop cycle
212 * creates incomplete data chunk with empty header and with no footer
213 * and stores this incomplete chunk in an internal buffer buf[]
214 */
215 int bytesToWrite = len;
216 int inputIndex = off; /* the index of the byte[] currently being written */
217
218 do {
219 /* enough data to complete a chunk */
220 if (bytesToWrite >= spaceInCurrentChunk) {
243 /* not enough data to build a chunk */
244 else {
245 /* header */
246 /* do not write header if not enough bytes to build a chunk yet */
247
248 /* data */
249 System.arraycopy(b, inputIndex, buf, count, bytesToWrite);
250 count += bytesToWrite;
251 size += bytesToWrite;
252 spaceInCurrentChunk -= bytesToWrite;
253 bytesToWrite = 0;
254
255 /* footer */
256 /* do not write header if not enough bytes to build a chunk yet */
257 }
258 } while (bytesToWrite > 0);
259 }
260
261 @Override
262 public synchronized void write(int _b) {
263 byte b[] = {(byte)_b};
264 write(b, 0, 1);
265 }
266
267 public synchronized void reset() {
268 count = preferedHeaderSize;
269 size = 0;
270 spaceInCurrentChunk = preferredChunkDataSize;
271 }
272
273 public int size() {
274 return size;
275 }
276
277 @Override
278 public synchronized void close() {
279 ensureOpen();
280
281 /* if we have buffer a chunked send it */
282 if (size > 0) {
283 flush(true);
|
25 package sun.net.www.http;
26
27 import java.io.*;
28
29 /**
30 * OutputStream that sends the output to the underlying stream using chunked
31 * encoding as specified in RFC 2068.
32 */
33 public class ChunkedOutputStream extends PrintStream {
34
35 /* Default chunk size (including chunk header) if not specified */
36 static final int DEFAULT_CHUNK_SIZE = 4096;
37 private static final byte[] CRLF = {'\r', '\n'};
38 private static final int CRLF_SIZE = CRLF.length;
39 private static final byte[] FOOTER = CRLF;
40 private static final int FOOTER_SIZE = CRLF_SIZE;
41 private static final byte[] EMPTY_CHUNK_HEADER = getHeader(0);
42 private static final int EMPTY_CHUNK_HEADER_SIZE = getHeaderSize(0);
43
44 /* internal buffer */
45 private byte[] buf;
46 /* size of data (excluding footers and headers) already stored in buf */
47 private int size;
48 /* current index in buf (i.e. buf[count] */
49 private int count;
50 /* number of bytes to be filled up to complete a data chunk
51 * currently being built */
52 private int spaceInCurrentChunk;
53
54 /* underlying stream */
55 private PrintStream out;
56
57 /* the chunk size we use */
58 private int preferredChunkDataSize;
59 private int preferedHeaderSize;
60 private int preferredChunkGrossSize;
61 /* header for a complete Chunk */
62 private byte[] completeHeader;
63
64 /* return the size of the header for a particular chunk size */
65 private static int getHeaderSize(int size) {
180 }
181
182 /* Check that the output stream is still open */
183 private void ensureOpen() {
184 if (out == null)
185 setError();
186 }
187
188 /*
189 * Writes data from b[] to an internal buffer and stores the data as data
190 * chunks of a following format: {Data length in Hex}{CRLF}{data}{CRLF}
191 * The size of the data is preferredChunkSize. As soon as a completed chunk
192 * is read from b[] a process of reading from b[] suspends, the chunk gets
193 * flushed to the underlying stream and then the reading process from b[]
194 * continues. When there is no more sufficient data in b[] to build up a
195 * chunk of preferredChunkSize size the data get stored as an incomplete
196 * chunk of a following format: {space for data length}{CRLF}{data}
197 * The size of the data is of course smaller than preferredChunkSize.
198 */
199 @Override
200 public synchronized void write(byte[] b, int off, int len) {
201 ensureOpen();
202 if ((off < 0) || (off > b.length) || (len < 0) ||
203 ((off + len) > b.length) || ((off + len) < 0)) {
204 throw new IndexOutOfBoundsException();
205 } else if (len == 0) {
206 return;
207 }
208
209 /* if b[] contains enough data then one loop cycle creates one complete
210 * data chunk with a header, body and a footer, and then flushes the
211 * chunk to the underlying stream. Otherwise, the last loop cycle
212 * creates incomplete data chunk with empty header and with no footer
213 * and stores this incomplete chunk in an internal buffer buf[]
214 */
215 int bytesToWrite = len;
216 int inputIndex = off; /* the index of the byte[] currently being written */
217
218 do {
219 /* enough data to complete a chunk */
220 if (bytesToWrite >= spaceInCurrentChunk) {
243 /* not enough data to build a chunk */
244 else {
245 /* header */
246 /* do not write header if not enough bytes to build a chunk yet */
247
248 /* data */
249 System.arraycopy(b, inputIndex, buf, count, bytesToWrite);
250 count += bytesToWrite;
251 size += bytesToWrite;
252 spaceInCurrentChunk -= bytesToWrite;
253 bytesToWrite = 0;
254
255 /* footer */
256 /* do not write header if not enough bytes to build a chunk yet */
257 }
258 } while (bytesToWrite > 0);
259 }
260
261 @Override
262 public synchronized void write(int _b) {
263 byte[] b = {(byte)_b};
264 write(b, 0, 1);
265 }
266
267 public synchronized void reset() {
268 count = preferedHeaderSize;
269 size = 0;
270 spaceInCurrentChunk = preferredChunkDataSize;
271 }
272
273 public int size() {
274 return size;
275 }
276
277 @Override
278 public synchronized void close() {
279 ensureOpen();
280
281 /* if we have buffer a chunked send it */
282 if (size > 0) {
283 flush(true);
|