/** * Reads the next characters from the message into an array and * returns the number of characters read. Returns -1 if the end of the * message has been reached. * @param buffer The character array in which to store the characters. * @return The number of characters read. Returns -1 if the * end of the message has been reached. * @throws IOException If an error occurs in reading the underlying * stream. */ @Override public int read(char[] buffer) throws IOException { return read(buffer, 0, buffer.length); }
/** * Closes the message for reading. This doesn't actually close the * underlying stream. The underlying stream may still be used for * communicating with the server and therefore is not closed. * <p> * If the end of the message has not yet been reached, this method * will read the remainder of the message until it reaches the end, * so that the underlying stream may continue to be used properly * for communicating with the server. If you do not fully read * a message, you MUST close it, otherwise your program will likely * hang or behave improperly. * @throws IOException If an error occurs while reading the * underlying stream. */ @Override public void close() throws IOException { synchronized (lock) { if (!eof) { while (read() != -1) { // read to EOF } } eof = true; atBeginning = false; } }
/** * Read a line of text. * A line is considered to be terminated by carriage return followed immediately by a linefeed. * This contrasts with BufferedReader which also allows other combinations. * @since 3.0 */ @Override public String readLine() throws IOException { StringBuilder sb = new StringBuilder(); int intch; synchronized(lock) { // make thread-safe (hopefully!) while((intch = read()) != -1) { if (intch == LF && atBeginning) { return sb.substring(0, sb.length()-1); } sb.append((char) intch); } } String string = sb.toString(); if (string.length() == 0) { // immediate EOF return null; } // Should not happen - EOF without CRLF //new Throwable(string).printStackTrace(); return string; } }
/** * Reads the next characters from the message into an array and * returns the number of characters read. Returns -1 if the end of the * message has been reached. * @param buffer The character array in which to store the characters. * @return The number of characters read. Returns -1 if the * end of the message has been reached. * @exception IOException If an error occurs in reading the underlying * stream. */ public int read(char[] buffer) throws IOException { return read(buffer, 0, buffer.length); }
/** * Reads the next characters from the message into an array and * returns the number of characters read. Returns -1 if the end of the * message has been reached. * @param buffer The character array in which to store the characters. * @return The number of characters read. Returns -1 if the * end of the message has been reached. * @exception IOException If an error occurs in reading the underlying * stream. */ @Override public int read(char[] buffer) throws IOException { return read(buffer, 0, buffer.length); }