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 * 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 * 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 * 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. 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 0N/A * OutputStream that sends the output to the underlying stream using chunked 0N/A * encoding as specified in RFC 2068. 0N/A /* Default chunk size (including chunk header) if not specified */ 722N/A private static final byte[]
CRLF = {
'\r',
'\n'};
0N/A /* internal buffer */ 722N/A /* size of data (excluding footers and headers) already stored in buf */ 722N/A /* current index in buf (i.e. buf[count] */ 722N/A /* number of bytes to be filled up to complete a data chunk 722N/A * currently being built */ 0N/A /* underlying stream */ 0N/A /* the chunk size we use */ 722N/A /* header for a complete Chunk */ 0N/A /* return the size of the header for a particular chunk size */ 722N/A /* return a header for a particular chunk size */ 722N/A /* This should never happen */ 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 * 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 /* start with an initial buffer */ 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 /* flush a completed chunk to underlying stream */ 722N/A /* complete the last chunk and flush it to underlying stream */ 722N/A /* adjust a header start index in case the header of the last 722N/A * chunk is shorter then preferedHeaderSize */ 722N/A //send the last chunk to underlying stream 722N/A //send an empty chunk (containing just a header) to underlying stream 0N/A /* Check that the output stream is still open */ 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 /* 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 /* enough data to complete a chunk */ 722N/A /* not enough data to build a chunk */ 722N/A /* do not write header if not enough bytes to build a chunk yet */ 722N/A /* do not write header if not enough bytes to build a chunk yet */ 0N/A /* if we have buffer a chunked send it */ 0N/A /* send a zero length chunk */ 0N/A /* don't close the underlying stream */