docs: private static int getChunkSizeFromInputStream(final InputStream in)
        throws IOException {
  
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          // States: 0=normal, 1=\r was scanned, 2=inside quoted string, -1=end
          int state = 0;
          while (state != -1) {
          int b = in.read();
              if (b == -1) {
                  throw new IOException("chunked stream ended unexpectedly");
              }
              switch (state) {
                  case 0:
                      switch (b) {
                          case '\r':
                              state = 1;
                              break;
                          case '\"':
                              state = 2;
                              /* fall through */
                          default:
                              baos.write(b);
                      }
                      break;
  
                  case 1:
                      if (b == '\n') {
                          state = -1;
                      } else {
                          // this was not CRLF
                          throw new IOException("Protocol violation: Unexpected"
                              + " single newline character in chunk size");
                      }
                      break;
  
                  case 2:
                      switch (b) {
                          case '\\':
                              b = in.read();
                              baos.write(b);
                              break;
                          case '\"':
                              state = 0;
                              /* fall through */
                          default:
                              baos.write(b);
                      }
                      break;
                  default: throw new RuntimeException("assertion failed");
              }
          }
  
          //parse data
          String dataString = new String(baos.toByteArray(), StandardCharsets.US_ASCII);
          int separator = dataString.indexOf(';');
          dataString = (separator > 0)
              ? dataString.substring(0, separator).trim()
              : dataString.trim();
  
          int result;
          try {
              result = Integer.parseInt(dataString.trim(), 16);
          } catch (NumberFormatException e) {
              throw new IOException ("Bad chunk size: " + dataString);
          }
          return result;
      }