src/java.base/share/classes/sun/net/www/http/ChunkedInputStream.java
author dfuchs
Fri, 30 Aug 2019 15:42:27 +0100
branchJDK-8229867-branch
changeset 57968 8595871a5446
parent 47216 71c04702a3d5
permissions -rw-r--r--
JDK-8229867: first prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2019, 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.*;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
    28
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A <code>ChunkedInputStream</code> provides a stream for reading a body of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * a http message that can be sent as a series of chunks, each with its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * size indicator. Optionally the last chunk can be followed by trailers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * containing entity-header fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A <code>ChunkedInputStream</code> is also <code>Hurryable</code> so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * can be hurried to the end of the stream if the bytes are available on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
    41
public class ChunkedInputStream extends InputStream implements Hurryable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * The underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * The <code>HttpClient</code> that should be notified when the chunked stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The <code>MessageHeader</code> that is populated with any optional trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * that appear after the last chunk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private MessageHeader responses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The size, in bytes, of the chunk that is currently being read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * This size is only valid if the current position in the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * input stream is inside a chunk (ie: state == STATE_READING_CHUNK).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int chunkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The number of bytes read from the underlying stream for the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * chunk. This value is always in the range <code>0</code> through to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>chunkSize</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int chunkRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The internal buffer array where chunk data is available for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * application to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private byte chunkData[] = new byte[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The current position in the buffer. It contains the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * of the next byte to read from <code>chunkData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private int chunkPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The index one greater than the index of the last valid byte in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * buffer. This value is always in the range <code>0</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <code>chunkData.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int chunkCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The internal buffer where bytes from the underlying stream can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * read. It may contain bytes representing chunk-size, chunk-data, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * trailer fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private byte rawData[] = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The current position in the buffer. It contains the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * of the next byte to read from <code>rawData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int rawPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The index one greater than the index of the last valid byte in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * buffer. This value is always in the range <code>0</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <code>rawData.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private int rawCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Indicates if an error was encountered when processing the chunked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private boolean error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Indicates if the chunked stream has been closed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <code>close</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   125
    final ReentrantLock readLock = new ReentrantLock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   126
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   127
    /*
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   128
     * Maximum chunk header size of 2KB + 2 bytes for CRLF
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   129
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 26720
diff changeset
   130
    private static final int MAX_CHUNK_HEADER_SIZE = 2050;
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 5506
diff changeset
   131
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
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 {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   316
                        chunkSize = Integer.parseInt(header, 0, i, 16);
2
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
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   648
    public int read() throws IOException {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   649
        readLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   650
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   651
            ensureOpen();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   652
            if (chunkPos >= chunkCount) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   653
                if (readAhead(true) <= 0) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   654
                    return -1;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   655
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   657
            return chunkData[chunkPos++] & 0xff;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   658
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   659
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Reads bytes from this stream into the specified byte array, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * the given offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param      b     destination buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @param      off   offset at which to start storing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param      len   maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @return     the number of bytes read, or <code>-1</code> if the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   675
    public int read(byte b[], int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   678
        readLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   679
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   680
            ensureOpen();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   681
            if ((off < 0) || (off > b.length) || (len < 0) ||
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   682
                    ((off + len) > b.length) || ((off + len) < 0)) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   683
                throw new IndexOutOfBoundsException();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   684
            } else if (len == 0) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   685
                return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   688
            int avail = chunkCount - chunkPos;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   689
            if (avail <= 0) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   690
                /*
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   691
                 * Optimization: if we're in the middle of the chunk read
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   692
                 * directly from the underlying stream into the caller's
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   693
                 * buffer
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   694
                 */
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   695
                if (state == STATE_READING_CHUNK) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   696
                    return fastRead(b, off, len);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   697
                }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   698
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   699
                /*
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   700
                 * We're not in the middle of a chunk so we must read ahead
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   701
                 * until there is some chunk data available.
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   702
                 */
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   703
                avail = readAhead(true);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   704
                if (avail < 0) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   705
                    return -1;      /* EOF */
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   706
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   708
            int cnt = (avail < len) ? avail : len;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   709
            System.arraycopy(chunkData, chunkPos, b, off, cnt);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   710
            chunkPos += cnt;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   711
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   712
            return cnt;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   713
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   714
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Returns the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return     the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *             stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   727
    public int available() throws IOException {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   728
        readLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   729
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   730
            ensureOpen();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   731
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   732
            int avail = chunkCount - chunkPos;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   733
            if (avail > 0) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   734
                return avail;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   735
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   737
            avail = readAhead(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   739
            if (avail < 0) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   740
                return 0;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   741
            } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   742
                return avail;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   743
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   744
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   745
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Close the stream by either returning the connection to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * keep alive cache or closing the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * If the chunked response hasn't been completely read we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * try to "hurry" to the end of the response. If this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * possible (without blocking) then the connection can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * returned to the keep alive cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   760
    public void close() throws IOException {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   761
        if (closed) return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   762
        readLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   763
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   764
            if (closed) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   765
                return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   766
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   767
            closeUnderlying();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   768
            closed = true;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   769
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   770
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Hurry the input stream by reading everything from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * stream. If the last chunk (and optional trailers) can be read without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * blocking then the stream is considered hurried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 16081
diff changeset
   779
     * Note that if an error has occurred or we can't get to last chunk
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * without blocking then this stream can't be hurried and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   783
    public boolean hurry() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   784
        readLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        try {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   786
            if (in == null || error) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   787
                return false;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   788
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   790
            try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   791
                readAhead(false);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   792
            } catch (Exception e) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   793
                return false;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   794
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   795
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   796
            if (error) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   797
                return false;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   798
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   799
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   800
            return (state == STATE_DONE);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   801
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 47216
diff changeset
   802
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
}