code: int readInt(InputStream in) throws IOException {
          StringBuilder sb = new StringBuilder();
  
          // read to \n or EOF
          int n;
          byte buf[] = new byte[1];
          do {
              n = in.read(buf, 0, 1);
              if (n > 0) {
                  char c = (char)buf[0];
                  if (c == '\n') {
                      break;                  // EOL found
                  } else {
                      sb.append(c);
                  }
              }
          } while (n > 0);
  
          if (sb.length() == 0) {
              throw new IOException("Premature EOF");
          }
  
          int value;
          try {
              value = Integer.parseInt(sb.toString());
          } catch (NumberFormatException x) {
              throw new IOException("Non-numeric value found - int expected");
          }
          return value;
      }
docs: /*
  Utility method to read an 'int' from the input stream. Ideally
  we should be using java.util.Scanner here but this implementation
  guarantees not to read ahead.