jdk/src/share/classes/sun/net/www/http/ChunkedInputStream.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 16081 452341da6f0b
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.net.www.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A <code>ChunkedInputStream</code> provides a stream for reading a body of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * a http message that can be sent as a series of chunks, each with its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * size indicator. Optionally the last chunk can be followed by trailers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * containing entity-header fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A <code>ChunkedInputStream</code> is also <code>Hurryable</code> so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * can be hurried to the end of the stream if the bytes are available on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class ChunkedInputStream extends InputStream implements Hurryable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The <code>HttpClient</code> that should be notified when the chunked stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The <code>MessageHeader</code> that is populated with any optional trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * that appear after the last chunk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private MessageHeader responses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The size, in bytes, of the chunk that is currently being read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * This size is only valid if the current position in the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * input stream is inside a chunk (ie: state == STATE_READING_CHUNK).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int chunkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The number of bytes read from the underlying stream for the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * chunk. This value is always in the range <code>0</code> through to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <code>chunkSize</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int chunkRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The internal buffer array where chunk data is available for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * application to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private byte chunkData[] = new byte[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The current position in the buffer. It contains the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * of the next byte to read from <code>chunkData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private int chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The index one greater than the index of the last valid byte in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * buffer. This value is always in the range <code>0</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <code>chunkData.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private int chunkCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The internal buffer where bytes from the underlying stream can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * read. It may contain bytes representing chunk-size, chunk-data, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * trailer fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private byte rawData[] = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The current position in the buffer. It contains the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * of the next byte to read from <code>rawData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int rawPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * The index one greater than the index of the last valid byte in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * buffer. This value is always in the range <code>0</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>rawData.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int rawCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Indicates if an error was encountered when processing the chunked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private boolean error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Indicates if the chunked stream has been closed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <code>close</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   128
    /*
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   129
     * Maximum chunk header size of 2KB + 2 bytes for CRLF
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   130
     */
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   131
    private final static int MAX_CHUNK_HEADER_SIZE = 2050;
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   132
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * State to indicate that next field should be :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *  chunk-size [ chunk-extension ] CRLF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static final int STATE_AWAITING_CHUNK_HEADER    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * State to indicate that we are currently reading the chunk-data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final int STATE_READING_CHUNK            = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Indicates that a chunk has been completely read and the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * fields to be examine should be CRLF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static final int STATE_AWAITING_CHUNK_EOL       = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Indicates that all chunks have been read and the next field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * should be optional trailers or an indication that the chunked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * stream is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static final int STATE_AWAITING_TRAILERS        = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * State to indicate that the chunked stream is complete and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * no further bytes should be read from the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static final int STATE_DONE                     = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Indicates the current state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Check to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new IOException("stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Ensures there is <code>size</code> bytes available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>rawData</code>. This requires that we either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * shift the bytes in use to the begining of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * or allocate a large buffer with sufficient space available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private void ensureRawAvailable(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (rawCount + size > rawData.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            int used = rawCount - rawPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (used + size > rawData.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                byte tmp[] = new byte[used + size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (used > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    System.arraycopy(rawData, rawPos, tmp, 0, used);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                rawData = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (used > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    System.arraycopy(rawData, rawPos, rawData, 0, used);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            rawCount = used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            rawPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Close the underlying input stream by either returning it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * keep alive cache or closing the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * As a chunked stream is inheritly persistent (see HTTP 1.1 RFC) the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * underlying stream can be returned to the keep alive cache if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * stream can be completely read without error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private void closeUnderlying() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (!error && state == STATE_DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            hc.finished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (!hurry()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Attempt to read the remainder of a chunk directly into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * caller's buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Return the number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private int fastRead(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // assert state == STATE_READING_CHUNKS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int remaining = chunkSize - chunkRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int cnt = (remaining < len) ? remaining : len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (cnt > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            int nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                nread = in.read(b, off, cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (nread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                chunkRead += nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (chunkRead >= chunkSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    state = STATE_AWAITING_CHUNK_EOL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Process any outstanding bytes that have already been read into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <code>rawData</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * The parsing of the chunked stream is performed as a state machine with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>state</code> representing the current state of the processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns when either all the outstanding bytes in rawData have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * processed or there is insufficient bytes available to continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * processing. When the latter occurs <code>rawPos</code> will not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * been updated and thus the processing can be restarted once further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * bytes have been read into <code>rawData</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private void processRaw() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        while (state != STATE_DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                 * We are awaiting a line with a chunk header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                case STATE_AWAITING_CHUNK_HEADER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                     * Find \n to indicate end of chunk header. If not found when there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                     * insufficient bytes in the raw buffer to parse a chunk header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    pos = rawPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    while (pos < rawCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        if (rawData[pos] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        pos++;
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   298
                        if ((pos - rawPos) >= MAX_CHUNK_HEADER_SIZE) {
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   299
                            error = true;
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   300
                            throw new IOException("Chunk header too long");
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   301
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if (pos >= rawCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                     * Extract the chunk size from the header (ignoring extensions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    String header = new String(rawData, rawPos, pos-rawPos+1, "US-ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    for (i=0; i < header.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        if (Character.digit(header.charAt(i), 16) == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        chunkSize = Integer.parseInt(header.substring(0, i), 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        throw new IOException("Bogus chunk size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                     * Chunk has been parsed so move rawPos to first byte of chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    rawPos = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    chunkRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                     * A chunk size of 0 means EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    if (chunkSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        state = STATE_READING_CHUNK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        state = STATE_AWAITING_TRAILERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                 * We are awaiting raw entity data (some may have already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                 * read). chunkSize is the size of the chunk; chunkRead is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                 * total read from the underlying stream to date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                case STATE_READING_CHUNK :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    /* no data available yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    if (rawPos >= rawCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                     * Compute the number of bytes of chunk data available in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                     * raw buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    int copyLen = Math.min( chunkSize-chunkRead, rawCount-rawPos );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                     * Expand or compact chunkData if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    if (chunkData.length < chunkCount + copyLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        int cnt = chunkCount - chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        if (chunkData.length < cnt + copyLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                            byte tmp[] = new byte[cnt + copyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            System.arraycopy(chunkData, chunkPos, tmp, 0, cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            chunkData = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            System.arraycopy(chunkData, chunkPos, chunkData, 0, cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        chunkPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        chunkCount = cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                     * Copy the chunk data into chunkData so that it's available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                     * to the read methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    System.arraycopy(rawData, rawPos, chunkData, chunkCount, copyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    rawPos += copyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    chunkCount += copyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    chunkRead += copyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                     * If all the chunk has been copied into chunkData then the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                     * token should be CRLF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    if (chunkSize - chunkRead <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        state = STATE_AWAITING_CHUNK_EOL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                 * Awaiting CRLF after the chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                case STATE_AWAITING_CHUNK_EOL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    /* not available yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    if (rawPos + 1 >= rawCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    if (rawData[rawPos] != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        throw new IOException("missing CR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    if (rawData[rawPos+1] != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        throw new IOException("missing LF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    rawPos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                     * Move onto the next chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    state = STATE_AWAITING_CHUNK_HEADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                 * Last chunk has been read so not we're waiting for optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                 * trailers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                case STATE_AWAITING_TRAILERS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                     * Do we have an entire line in the raw buffer?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    pos = rawPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    while (pos < rawCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        if (rawData[pos] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    if (pos >= rawCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    if (pos == rawPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        throw new IOException("LF should be proceeded by CR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    if (rawData[pos-1] != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        throw new IOException("LF should be proceeded by CR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                     * Stream done so close underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if (pos == (rawPos + 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        state = STATE_DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        closeUnderlying();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                     * Extract any tailers and append them to the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                     * headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    String trailer = new String(rawData, rawPos, pos-rawPos, "US-ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    i = trailer.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        throw new IOException("Malformed tailer - format should be key:value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    String key = (trailer.substring(0, i)).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    String value = (trailer.substring(i+1, trailer.length())).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    responses.add(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                     * Move onto the next trailer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    rawPos = pos+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } /* switch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Reads any available bytes from the underlying stream into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <code>rawData</code> and returns the number of bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * chunk data available in <code>chunkData</code> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * application can read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private int readAheadNonBlocking() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * If there's anything available on the underlying stream then we read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * it into the raw buffer and process it. Processing ensures that any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * available chunk data is made available in chunkData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        int avail = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            /* ensure that there is space in rawData to read the available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            ensureRawAvailable(avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            int nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                nread = in.read(rawData, rawCount, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (nread < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                error = true;   /* premature EOF ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            rawCount += nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
             * Process the raw bytes that have been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            processRaw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         * Return the number of chunked bytes available to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return chunkCount - chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Reads from the underlying stream until there is chunk data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * available in <code>chunkData</code> for the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    private int readAheadBlocking() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
             * All of chunked response has been read to return EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (state == STATE_DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
             * We must read into the raw buffer so make sure there is space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
             * available. We use a size of 32 to avoid too much chunk data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
             * being read into the raw buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            ensureRawAvailable(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            int nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                nread = in.read(rawData, rawCount, rawData.length-rawCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
             * If we hit EOF it means there's a problem as we should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
             * attempt to read once the last chunk and trailers have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
             * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (nread < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                throw new IOException("Premature EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
             * Process the bytes from the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            rawCount += nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            processRaw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } while (chunkCount <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * Return the number of chunked bytes available to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return chunkCount - chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Read ahead in either blocking or non-blocking mode. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * is typically used when we run out of available bytes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <code>chunkData</code> or we need to determine how many bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * are available on the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private int readAhead(boolean allowBlocking) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * Last chunk already received - return EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (state == STATE_DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * Reset position/count if data in chunkData is exhausted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (chunkPos >= chunkCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            chunkCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            chunkPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * Read ahead blocking or non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (allowBlocking) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            return readAheadBlocking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return readAheadNonBlocking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Creates a <code>ChunkedInputStream</code> and saves its  arguments, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * later use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param   in   the underlying input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param   hc   the HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param   responses   the MessageHeader that should be populated with optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *                      trailers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public ChunkedInputStream(InputStream in, HttpClient hc, MessageHeader responses) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        /* save arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        this.responses = responses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * Set our initial state to indicate that we are first starting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * look for a chunk header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        state = STATE_AWAITING_CHUNK_HEADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * the general contract of the <code>read</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * method of <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *             stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public synchronized int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if (chunkPos >= chunkCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (readAhead(true) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return chunkData[chunkPos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Reads bytes from this stream into the specified byte array, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * the given offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @param      b     destination buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @param      off   offset at which to start storing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @param      len   maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @return     the number of bytes read, or <code>-1</code> if the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public synchronized int read(byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if ((off < 0) || (off > b.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            ((off + len) > b.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        int avail = chunkCount - chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (avail <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
             * Optimization: if we're in the middle of the chunk read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
             * directly from the underlying stream into the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
             * buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (state == STATE_READING_CHUNK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return fastRead( b, off, len );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
             * We're not in the middle of a chunk so we must read ahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
             * until there is some chunk data available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            avail = readAhead(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (avail < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                return -1;      /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        int cnt = (avail < len) ? avail : len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        System.arraycopy(chunkData, chunkPos, b, off, cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        chunkPos += cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Returns the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @return     the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *             stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    public synchronized int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        int avail = chunkCount - chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if(avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        avail = readAhead(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (avail < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            return avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Close the stream by either returning the connection to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * keep alive cache or closing the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * If the chunked response hasn't been completely read we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * try to "hurry" to the end of the response. If this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * possible (without blocking) then the connection can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * returned to the keep alive cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public synchronized void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        closeUnderlying();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * Hurry the input stream by reading everything from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * stream. If the last chunk (and optional trailers) can be read without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * blocking then the stream is considered hurried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 16081
diff changeset
   758
     * Note that if an error has occurred or we can't get to last chunk
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * without blocking then this stream can't be hurried and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public synchronized boolean hurry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (in == null || error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            readAhead(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        return (state == STATE_DONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
}