code: public static String readLine(InputStream in, byte[] buffer, int limit)
          throws IllegalArgumentException, IOException, LineTooLongException
      {
          if (!in.markSupported()) {
              throw new IllegalArgumentException
                  ("InputStream doesn't support marking: " + in.getClass());
          }
  
          String line = null;
  
          int cursor = 0;
          int len = buffer.length;
  
          int count = 0;
          int c;
      loop:
          while ((c = in.read()) >= 0) {
              if (limit >= 0 && ++count > limit) {
                  throw new LineTooLongException(limit);
              }
  
              switch (c) {
              case '\r':
                  in.mark(1);
                  if (in.read() != '\n') {
                      in.reset();
                  }
                  // fall through
              case '\n':
                  if (line == null && cursor == 0) {
                      return "";
                  }
                  break loop;
              default:
                  if (cursor >= len) {
                      if (line == null) {
                          line = new String(buffer, "8859_1");
                      }
                      else {
                          line = line.concat(new String(buffer, "8859_1"));
                      }
                      cursor = 0;
                  }
                  buffer[cursor++] = (byte)c;
              }
          }
  
          if (cursor > 0) {
              if (line == null) {
                  line = new String(buffer, 0, cursor, "8859_1");
              }
              else {
                  line = line.concat(new String(buffer, 0, cursor, "8859_1"));
              }
          }
  
          return line;
      }
docs: Reads a line from an HTTP InputStream, using the given buffer for
  temporary storage.
  
  @param in stream to read from
  @param buffer temporary buffer to use
  @throws IllegalArgumentException if the given InputStream doesn't
  support marking
  @throws LineTooLongException when line is longer than the limit