jdk/src/share/classes/sun/net/httpserver/ChunkedInputStream.java

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


  24  */
  25 
  26 package sun.net.httpserver;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import com.sun.net.httpserver.*;
  31 import com.sun.net.httpserver.spi.*;
  32 
  33 class ChunkedInputStream extends LeftOverInputStream {
  34     ChunkedInputStream (ExchangeImpl t, InputStream src) {
  35         super (t, src);
  36     }
  37 
  38     private int remaining;
  39 
  40     /* true when a chunk header needs to be read */
  41 
  42     private boolean needToReadHeader = true;
  43 
  44     static char CR = '\r';
  45     static char LF = '\n';




  46 
  47     private int numeric (char[] arr, int nchars) throws IOException {
  48         assert arr.length >= nchars;
  49         int len = 0;
  50         for (int i=0; i<nchars; i++) {
  51             char c = arr[i];
  52             int val=0;
  53             if (c>='0' && c <='9') {
  54                 val = c - '0';
  55             } else if (c>='a' && c<= 'f') {
  56                 val = c - 'a' + 10;
  57             } else if (c>='A' && c<= 'F') {
  58                 val = c - 'A' + 10;
  59             } else {
  60                 throw new IOException ("invalid chunk length");
  61             }
  62             len = len * 16 + val;
  63         }
  64         return len;
  65     }
  66 
  67     /* read the chunk header line and return the chunk length
  68      * any chunk extensions are ignored
  69      */
  70     private int readChunkHeader () throws IOException {
  71         boolean gotCR = false;
  72         int c;
  73         char[] len_arr = new char [16];
  74         int len_size = 0;
  75         boolean end_of_len = false;

  76 
  77         while ((c=in.read())!= -1) {
  78             char ch = (char) c;
  79             if (len_size == len_arr.length -1) {



  80                 throw new IOException ("invalid chunk header");
  81             }
  82             if (gotCR) {
  83                 if (ch == LF) {
  84                     int l = numeric (len_arr, len_size);
  85                     return l;
  86                 } else {
  87                     gotCR = false;
  88                 }
  89                 if (!end_of_len) {
  90                     len_arr[len_size++] = ch;
  91                 }
  92             } else {
  93                 if (ch == CR) {
  94                     gotCR = true;
  95                 } else if (ch == ';') {
  96                     end_of_len = true;
  97                 } else if (!end_of_len) {
  98                     len_arr[len_size++] = ch;
  99                 }




  24  */
  25 
  26 package sun.net.httpserver;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import com.sun.net.httpserver.*;
  31 import com.sun.net.httpserver.spi.*;
  32 
  33 class ChunkedInputStream extends LeftOverInputStream {
  34     ChunkedInputStream (ExchangeImpl t, InputStream src) {
  35         super (t, src);
  36     }
  37 
  38     private int remaining;
  39 
  40     /* true when a chunk header needs to be read */
  41 
  42     private boolean needToReadHeader = true;
  43 
  44     final static char CR = '\r';
  45     final static char LF = '\n';
  46     /*
  47      * Maximum chunk header size of 2KB + 2 bytes for CRLF
  48      */
  49     private final static int MAX_CHUNK_HEADER_SIZE = 2050;
  50 
  51     private int numeric (char[] arr, int nchars) throws IOException {
  52         assert arr.length >= nchars;
  53         int len = 0;
  54         for (int i=0; i<nchars; i++) {
  55             char c = arr[i];
  56             int val=0;
  57             if (c>='0' && c <='9') {
  58                 val = c - '0';
  59             } else if (c>='a' && c<= 'f') {
  60                 val = c - 'a' + 10;
  61             } else if (c>='A' && c<= 'F') {
  62                 val = c - 'A' + 10;
  63             } else {
  64                 throw new IOException ("invalid chunk length");
  65             }
  66             len = len * 16 + val;
  67         }
  68         return len;
  69     }
  70 
  71     /* read the chunk header line and return the chunk length
  72      * any chunk extensions are ignored
  73      */
  74     private int readChunkHeader () throws IOException {
  75         boolean gotCR = false;
  76         int c;
  77         char[] len_arr = new char [16];
  78         int len_size = 0;
  79         boolean end_of_len = false;
  80         int read = 0;
  81 
  82         while ((c=in.read())!= -1) {
  83             char ch = (char) c;
  84             read++;
  85             if ((len_size == len_arr.length -1) ||
  86                 (read > MAX_CHUNK_HEADER_SIZE))
  87             {
  88                 throw new IOException ("invalid chunk header");
  89             }
  90             if (gotCR) {
  91                 if (ch == LF) {
  92                     int l = numeric (len_arr, len_size);
  93                     return l;
  94                 } else {
  95                     gotCR = false;
  96                 }
  97                 if (!end_of_len) {
  98                     len_arr[len_size++] = ch;
  99                 }
 100             } else {
 101                 if (ch == CR) {
 102                     gotCR = true;
 103                 } else if (ch == ';') {
 104                     end_of_len = true;
 105                 } else if (!end_of_len) {
 106                     len_arr[len_size++] = ch;
 107                 }