0N/A/*
2362N/A * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.net.www.http;
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * OutputStream that sends the output to the underlying stream using chunked
0N/A * encoding as specified in RFC 2068.
0N/A */
0N/Apublic class ChunkedOutputStream extends PrintStream {
0N/A
0N/A /* Default chunk size (including chunk header) if not specified */
0N/A static final int DEFAULT_CHUNK_SIZE = 4096;
722N/A private static final byte[] CRLF = {'\r', '\n'};
722N/A private static final int CRLF_SIZE = CRLF.length;
722N/A private static final byte[] FOOTER = CRLF;
722N/A private static final int FOOTER_SIZE = CRLF_SIZE;
722N/A private static final byte[] EMPTY_CHUNK_HEADER = getHeader(0);
722N/A private static final int EMPTY_CHUNK_HEADER_SIZE = getHeaderSize(0);
0N/A
0N/A /* internal buffer */
0N/A private byte buf[];
722N/A /* size of data (excluding footers and headers) already stored in buf */
722N/A private int size;
722N/A /* current index in buf (i.e. buf[count] */
0N/A private int count;
722N/A /* number of bytes to be filled up to complete a data chunk
722N/A * currently being built */
722N/A private int spaceInCurrentChunk;
0N/A
0N/A /* underlying stream */
0N/A private PrintStream out;
0N/A
0N/A /* the chunk size we use */
722N/A private int preferredChunkDataSize;
722N/A private int preferedHeaderSize;
722N/A private int preferredChunkGrossSize;
722N/A /* header for a complete Chunk */
722N/A private byte[] completeHeader;
0N/A
0N/A /* return the size of the header for a particular chunk size */
722N/A private static int getHeaderSize(int size) {
722N/A return (Integer.toHexString(size)).length() + CRLF_SIZE;
722N/A }
722N/A
722N/A /* return a header for a particular chunk size */
722N/A private static byte[] getHeader(int size){
722N/A try {
722N/A String hexStr = Integer.toHexString(size);
722N/A byte[] hexBytes = hexStr.getBytes("US-ASCII");
722N/A byte[] header = new byte[getHeaderSize(size)];
722N/A for (int i=0; i<hexBytes.length; i++)
722N/A header[i] = hexBytes[i];
722N/A header[hexBytes.length] = CRLF[0];
722N/A header[hexBytes.length+1] = CRLF[1];
722N/A return header;
722N/A } catch (java.io.UnsupportedEncodingException e) {
722N/A /* This should never happen */
722N/A throw new InternalError(e.getMessage());
722N/A }
0N/A }
0N/A
0N/A public ChunkedOutputStream(PrintStream o) {
0N/A this(o, DEFAULT_CHUNK_SIZE);
0N/A }
0N/A
0N/A public ChunkedOutputStream(PrintStream o, int size) {
0N/A super(o);
0N/A out = o;
0N/A
0N/A if (size <= 0) {
0N/A size = DEFAULT_CHUNK_SIZE;
0N/A }
722N/A
0N/A /* Adjust the size to cater for the chunk header - eg: if the
0N/A * preferred chunk size is 1k this means the chunk size should
722N/A * be 1017 bytes (differs by 7 from preferred size because of
722N/A * 3 bytes for chunk size in hex and CRLF (header) and CRLF (footer)).
722N/A *
722N/A * If headerSize(adjusted_size) is shorter then headerSize(size)
722N/A * then try to use the extra byte unless headerSize(adjusted_size+1)
722N/A * increases back to headerSize(size)
0N/A */
0N/A if (size > 0) {
722N/A int adjusted_size = size - getHeaderSize(size) - FOOTER_SIZE;
722N/A if (getHeaderSize(adjusted_size+1) < getHeaderSize(size)){
0N/A adjusted_size++;
0N/A }
0N/A size = adjusted_size;
0N/A }
0N/A
0N/A if (size > 0) {
722N/A preferredChunkDataSize = size;
0N/A } else {
722N/A preferredChunkDataSize = DEFAULT_CHUNK_SIZE -
722N/A getHeaderSize(DEFAULT_CHUNK_SIZE) - FOOTER_SIZE;
0N/A }
0N/A
722N/A preferedHeaderSize = getHeaderSize(preferredChunkDataSize);
722N/A preferredChunkGrossSize = preferedHeaderSize + preferredChunkDataSize
722N/A + FOOTER_SIZE;
722N/A completeHeader = getHeader(preferredChunkDataSize);
722N/A
0N/A /* start with an initial buffer */
722N/A buf = new byte[preferredChunkDataSize + 32];
722N/A reset();
0N/A }
0N/A
0N/A /*
722N/A * Flush a buffered, completed chunk to an underlying stream. If the data in
722N/A * the buffer is insufficient to build up a chunk of "preferredChunkSize"
722N/A * then the data do not get flushed unless flushAll is true. If flushAll is
722N/A * true then the remaining data builds up a last chunk which size is smaller
722N/A * than preferredChunkSize, and then the last chunk gets flushed to
722N/A * underlying stream. If flushAll is true and there is no data in a buffer
722N/A * at all then an empty chunk (containing a header only) gets flushed to
722N/A * underlying stream.
0N/A */
722N/A private void flush(boolean flushAll) {
722N/A if (spaceInCurrentChunk == 0) {
722N/A /* flush a completed chunk to underlying stream */
722N/A out.write(buf, 0, preferredChunkGrossSize);
722N/A out.flush();
722N/A reset();
722N/A } else if (flushAll){
722N/A /* complete the last chunk and flush it to underlying stream */
722N/A if (size > 0){
722N/A /* adjust a header start index in case the header of the last
722N/A * chunk is shorter then preferedHeaderSize */
0N/A
722N/A int adjustedHeaderStartIndex = preferedHeaderSize -
722N/A getHeaderSize(size);
722N/A
722N/A /* write header */
722N/A System.arraycopy(getHeader(size), 0, buf,
722N/A adjustedHeaderStartIndex, getHeaderSize(size));
722N/A
722N/A /* write footer */
722N/A buf[count++] = FOOTER[0];
722N/A buf[count++] = FOOTER[1];
722N/A
722N/A //send the last chunk to underlying stream
722N/A out.write(buf, adjustedHeaderStartIndex, count - adjustedHeaderStartIndex);
0N/A } else {
722N/A //send an empty chunk (containing just a header) to underlying stream
722N/A out.write(EMPTY_CHUNK_HEADER, 0, EMPTY_CHUNK_HEADER_SIZE);
0N/A }
0N/A
0N/A out.flush();
722N/A reset();
722N/A }
0N/A }
0N/A
722N/A @Override
0N/A public boolean checkError() {
0N/A return out.checkError();
0N/A }
0N/A
0N/A /* Check that the output stream is still open */
0N/A private void ensureOpen() {
0N/A if (out == null)
0N/A setError();
0N/A }
0N/A
722N/A /*
722N/A * Writes data from b[] to an internal buffer and stores the data as data
722N/A * chunks of a following format: {Data length in Hex}{CRLF}{data}{CRLF}
722N/A * The size of the data is preferredChunkSize. As soon as a completed chunk
722N/A * is read from b[] a process of reading from b[] suspends, the chunk gets
722N/A * flushed to the underlying stream and then the reading process from b[]
722N/A * continues. When there is no more sufficient data in b[] to build up a
722N/A * chunk of preferredChunkSize size the data get stored as an incomplete
722N/A * chunk of a following format: {space for data length}{CRLF}{data}
722N/A * The size of the data is of course smaller than preferredChunkSize.
722N/A */
722N/A @Override
0N/A public synchronized void write(byte b[], int off, int len) {
0N/A ensureOpen();
0N/A if ((off < 0) || (off > b.length) || (len < 0) ||
0N/A ((off + len) > b.length) || ((off + len) < 0)) {
0N/A throw new IndexOutOfBoundsException();
0N/A } else if (len == 0) {
0N/A return;
0N/A }
0N/A
722N/A /* if b[] contains enough data then one loop cycle creates one complete
722N/A * data chunk with a header, body and a footer, and then flushes the
722N/A * chunk to the underlying stream. Otherwise, the last loop cycle
722N/A * creates incomplete data chunk with empty header and with no footer
722N/A * and stores this incomplete chunk in an internal buffer buf[]
722N/A */
722N/A int bytesToWrite = len;
722N/A int inputIndex = off; /* the index of the byte[] currently being written */
722N/A
722N/A do {
722N/A /* enough data to complete a chunk */
722N/A if (bytesToWrite >= spaceInCurrentChunk) {
65N/A
722N/A /* header */
722N/A for (int i=0; i<completeHeader.length; i++)
722N/A buf[i] = completeHeader[i];
722N/A
722N/A /* data */
722N/A System.arraycopy(b, inputIndex, buf, count, spaceInCurrentChunk);
722N/A inputIndex += spaceInCurrentChunk;
722N/A bytesToWrite -= spaceInCurrentChunk;
722N/A count += spaceInCurrentChunk;
65N/A
722N/A /* footer */
722N/A buf[count++] = FOOTER[0];
722N/A buf[count++] = FOOTER[1];
722N/A spaceInCurrentChunk = 0; //chunk is complete
722N/A
722N/A flush(false);
722N/A if (checkError()){
722N/A break;
722N/A }
0N/A }
65N/A
722N/A /* not enough data to build a chunk */
722N/A else {
722N/A /* header */
722N/A /* do not write header if not enough bytes to build a chunk yet */
0N/A
722N/A /* data */
722N/A System.arraycopy(b, inputIndex, buf, count, bytesToWrite);
722N/A count += bytesToWrite;
722N/A size += bytesToWrite;
722N/A spaceInCurrentChunk -= bytesToWrite;
722N/A bytesToWrite = 0;
722N/A
722N/A /* footer */
722N/A /* do not write header if not enough bytes to build a chunk yet */
0N/A }
722N/A } while (bytesToWrite > 0);
0N/A }
0N/A
722N/A @Override
722N/A public synchronized void write(int _b) {
722N/A byte b[] = {(byte)_b};
722N/A write(b, 0, 1);
0N/A }
0N/A
0N/A public synchronized void reset() {
722N/A count = preferedHeaderSize;
722N/A size = 0;
722N/A spaceInCurrentChunk = preferredChunkDataSize;
0N/A }
0N/A
0N/A public int size() {
722N/A return size;
0N/A }
0N/A
722N/A @Override
0N/A public synchronized void close() {
0N/A ensureOpen();
0N/A
0N/A /* if we have buffer a chunked send it */
722N/A if (size > 0) {
722N/A flush(true);
0N/A }
0N/A
0N/A /* send a zero length chunk */
722N/A flush(true);
0N/A
0N/A /* don't close the underlying stream */
0N/A out = null;
0N/A }
0N/A
722N/A @Override
0N/A public synchronized void flush() {
0N/A ensureOpen();
722N/A if (size > 0) {
722N/A flush(true);
0N/A }
0N/A }
0N/A}