jdk/src/share/classes/sun/net/www/http/ChunkedInputStream.java

Print this page
rev 5672 : 7186954: Improve connection performance
Reviewed-by: chegar, skoivu


 108 
 109     /**
 110      * The index one greater than the index of the last valid byte in the
 111      * buffer. This value is always in the range <code>0</code> through
 112      * <code>rawData.length</code>.
 113      */
 114     private int rawCount;
 115 
 116     /**
 117      * Indicates if an error was encountered when processing the chunked
 118      * stream.
 119      */
 120     private boolean error;
 121 
 122     /**
 123      * Indicates if the chunked stream has been closed using the
 124      * <code>close</code> method.
 125      */
 126     private boolean closed;
 127 





 128     /**
 129      * State to indicate that next field should be :-
 130      *  chunk-size [ chunk-extension ] CRLF
 131      */
 132     static final int STATE_AWAITING_CHUNK_HEADER    = 1;
 133 
 134     /**
 135      * State to indicate that we are currently reading the chunk-data.
 136      */
 137     static final int STATE_READING_CHUNK            = 2;
 138 
 139     /**
 140      * Indicates that a chunk has been completely read and the next
 141      * fields to be examine should be CRLF
 142      */
 143     static final int STATE_AWAITING_CHUNK_EOL       = 3;
 144 
 145     /**
 146      * Indicates that all chunks have been read and the next field
 147      * should be optional trailers or an indication that the chunked


 273         int i;
 274 
 275         while (state != STATE_DONE) {
 276 
 277             switch (state) {
 278 
 279                 /**
 280                  * We are awaiting a line with a chunk header
 281                  */
 282                 case STATE_AWAITING_CHUNK_HEADER:
 283                     /*
 284                      * Find \n to indicate end of chunk header. If not found when there is
 285                      * insufficient bytes in the raw buffer to parse a chunk header.
 286                      */
 287                     pos = rawPos;
 288                     while (pos < rawCount) {
 289                         if (rawData[pos] == '\n') {
 290                             break;
 291                         }
 292                         pos++;




 293                     }
 294                     if (pos >= rawCount) {
 295                         return;
 296                     }
 297 
 298                     /*
 299                      * Extract the chunk size from the header (ignoring extensions).
 300                      */
 301                     String header = new String(rawData, rawPos, pos-rawPos+1, "US-ASCII");
 302                     for (i=0; i < header.length(); i++) {
 303                         if (Character.digit(header.charAt(i), 16) == -1)
 304                             break;
 305                     }
 306                     try {
 307                         chunkSize = Integer.parseInt(header.substring(0, i), 16);
 308                     } catch (NumberFormatException e) {
 309                         error = true;
 310                         throw new IOException("Bogus chunk size");
 311                     }
 312 




 108 
 109     /**
 110      * The index one greater than the index of the last valid byte in the
 111      * buffer. This value is always in the range <code>0</code> through
 112      * <code>rawData.length</code>.
 113      */
 114     private int rawCount;
 115 
 116     /**
 117      * Indicates if an error was encountered when processing the chunked
 118      * stream.
 119      */
 120     private boolean error;
 121 
 122     /**
 123      * Indicates if the chunked stream has been closed using the
 124      * <code>close</code> method.
 125      */
 126     private boolean closed;
 127 
 128     /*
 129      * Maximum chunk header size of 2KB + 2 bytes for CRLF
 130      */
 131     private final static int MAX_CHUNK_HEADER_SIZE = 2050;
 132 
 133     /**
 134      * State to indicate that next field should be :-
 135      *  chunk-size [ chunk-extension ] CRLF
 136      */
 137     static final int STATE_AWAITING_CHUNK_HEADER    = 1;
 138 
 139     /**
 140      * State to indicate that we are currently reading the chunk-data.
 141      */
 142     static final int STATE_READING_CHUNK            = 2;
 143 
 144     /**
 145      * Indicates that a chunk has been completely read and the next
 146      * fields to be examine should be CRLF
 147      */
 148     static final int STATE_AWAITING_CHUNK_EOL       = 3;
 149 
 150     /**
 151      * Indicates that all chunks have been read and the next field
 152      * should be optional trailers or an indication that the chunked


 278         int i;
 279 
 280         while (state != STATE_DONE) {
 281 
 282             switch (state) {
 283 
 284                 /**
 285                  * We are awaiting a line with a chunk header
 286                  */
 287                 case STATE_AWAITING_CHUNK_HEADER:
 288                     /*
 289                      * Find \n to indicate end of chunk header. If not found when there is
 290                      * insufficient bytes in the raw buffer to parse a chunk header.
 291                      */
 292                     pos = rawPos;
 293                     while (pos < rawCount) {
 294                         if (rawData[pos] == '\n') {
 295                             break;
 296                         }
 297                         pos++;
 298                         if ((pos - rawPos) >= MAX_CHUNK_HEADER_SIZE) {
 299                             error = true;
 300                             throw new IOException("Chunk header too long");
 301                         }
 302                     }
 303                     if (pos >= rawCount) {
 304                         return;
 305                     }
 306 
 307                     /*
 308                      * Extract the chunk size from the header (ignoring extensions).
 309                      */
 310                     String header = new String(rawData, rawPos, pos-rawPos+1, "US-ASCII");
 311                     for (i=0; i < header.length(); i++) {
 312                         if (Character.digit(header.charAt(i), 16) == -1)
 313                             break;
 314                     }
 315                     try {
 316                         chunkSize = Integer.parseInt(header.substring(0, i), 16);
 317                     } catch (NumberFormatException e) {
 318                         error = true;
 319                         throw new IOException("Bogus chunk size");
 320                     }
 321