public String readLine() throws IOException {
  StringBuilder line=new StringBuilder();
  int i;
  char c;
  i=read();
  if (i == -1)   return null;
  while (i > -1 && i != '\n' && i != '\r') {
    c=(char)(i & 0xFF);
    line=line.append(c);
    i=read();
  }
  if (i == '\r') {
    if ((i=read()) != '\n')     throw new IOException("Unexpected character " + Integer.toHexString(i) + ". Expected 0x0d. Had read "+ line);
  }
  return line.toString();
}

public String readLine ( ) throws IOException { StringBuilder line = new StringBuilder ( ) ; int i ; char c ; i = read ( ) ; if ( i == - 1 ) return null ; while ( i > - 1 && i != '\n' && i != '\r' ) { c = ( char ) ( i & 0xFF ) ; line = line . append ( c ) ; i = read ( ) ; } if ( i == '\r' ) { if ( ( i = read ( ) ) != '\n' ) throw new IOException ( "Unexpected character " + Integer . toHexString ( i ) + ". Expected 0x0d. Had read " + line ) ; } return line . toString ( ) ; }
a method to read a line from the stream up to and including the cr or crlf . we read character by character so that we don ' t read further than we should i . e . into the next line , which could be a message body , or the next message !
