Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 84358 invoked from network); 4 Jan 2007 18:05:55 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Jan 2007 18:05:55 -0000 Received: (qmail 91398 invoked by uid 500); 4 Jan 2007 17:58:33 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 91328 invoked by uid 500); 4 Jan 2007 17:58:33 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 90958 invoked by uid 99); 4 Jan 2007 17:58:32 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Jan 2007 09:58:28 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Jan 2007 09:48:35 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 59E8C1A9832; Thu, 4 Jan 2007 09:47:11 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r492652 [11/13] - /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ Date: Thu, 04 Jan 2007 17:47:05 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070104174711.59E8C1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java?view=diff&rev=492652&r1=492651&r2=492652 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java (original) +++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java Thu Jan 4 09:47:01 2007 @@ -31,258 +31,247 @@ * readable format on the stream. No IOExceptions are thrown by this class. * Instead, callers should call checkError() to see if a problem has been * encountered in this Writer. - * */ public class PrintWriter extends Writer { - /** - * The writer to output data to. - */ - protected Writer out; - - /** - * indicates whether or not this PrintWriter has incurred an error. - */ - private boolean ioError; - - /** - * indicates whether or not this PrintWriter should flush its contents after - * printing a new line. - */ - private boolean autoflush; + /** + * The writer to output data to. + */ + protected Writer out; + + /** + * indicates whether or not this PrintWriter has incurred an error. + */ + private boolean ioError; + + /** + * indicates whether or not this PrintWriter should flush its contents after + * printing a new line. + */ + private boolean autoflush; - private final String lineSeparator = AccessController + private final String lineSeparator = AccessController .doPrivileged(new PriviAction("line.separator")); //$NON-NLS-1$ - /** - * Constructs a new PrintWriter on the OutputStream out. All - * writes to the target can now take place through this PrintWriter. By - * default, the PrintWriter is set to not autoflush when println() is - * called. - * - * @param out - * the the OutputStream to provide convenience methods on. - */ - public PrintWriter(OutputStream out) { - this(new OutputStreamWriter(out), false); - } - - /** - * Constructs a new PrintWriter on the OutputStream out. All - * writes to the target can now take place through this PrintWriter. By - * default, the PrintWriter is set to not autoflush when println() is - * called. - * - * @param out - * the the OutputStream to provide convenience methods on. - * @param autoflush - * whether to flush when println() is called. - * - */ - public PrintWriter(OutputStream out, boolean autoflush) { - this(new OutputStreamWriter(out), autoflush); - } - - /** - * Constructs a new PrintWriter on the Writer wr. All writes - * to the target can now take place through this PrintWriter. By default, - * the PrintWriter is set to not autoflush when println() is called. - * - * @param wr - * the Writer to provide convenience methods on. - * - */ - public PrintWriter(Writer wr) { - this(wr, false); - } - - /** - * Constructs a new PrintWriter on the given writer. All writes - * to the target can now take place through this PrintWriter. By default, - * the PrintWriter is set to not autoflush when println() is called. - * - * @param wr - * the Writer to provide convenience methods on. - * @param autoflush - * whether to flush when println() is called. - * - */ - public PrintWriter(Writer wr, boolean autoflush) { - super(wr); - this.autoflush = autoflush; - out = wr; - } + /** + * Constructs a new PrintWriter on the OutputStream out. All + * writes to the target can now take place through this PrintWriter. By + * default, the PrintWriter is set to not autoflush when println() is + * called. + * + * @param out + * the the OutputStream to provide convenience methods on. + */ + public PrintWriter(OutputStream out) { + this(new OutputStreamWriter(out), false); + } + + /** + * Constructs a new PrintWriter on the OutputStream out. All + * writes to the target can now take place through this PrintWriter. By + * default, the PrintWriter is set to not autoflush when println() is + * called. + * + * @param out + * the the OutputStream to provide convenience methods on. + * @param autoflush + * whether to flush when println() is called. + */ + public PrintWriter(OutputStream out, boolean autoflush) { + this(new OutputStreamWriter(out), autoflush); + } + + /** + * Constructs a new PrintWriter on the Writer wr. All writes + * to the target can now take place through this PrintWriter. By default, + * the PrintWriter is set to not autoflush when println() is called. + * + * @param wr + * the Writer to provide convenience methods on. + */ + public PrintWriter(Writer wr) { + this(wr, false); + } + + /** + * Constructs a new PrintWriter on the given writer. All writes to the + * target can now take place through this PrintWriter. By default, the + * PrintWriter is set to not autoflush when println() is called. + * + * @param wr + * the Writer to provide convenience methods on. + * @param autoflush + * whether to flush when println() is called. + */ + public PrintWriter(Writer wr, boolean autoflush) { + super(wr); + this.autoflush = autoflush; + out = wr; + } /** * Constructs a new PrintWriter on the File file. The - * automatic flushing is set to false. An intermediate - * OutputStreamWriter will use the default for the current JVM - * instance charset to encode characters. + * automatic flushing is set to false. An intermediate + * OutputStreamWriter will use the default for the current + * JVM instance charset to encode characters. * * @param file * This writer's buffered destination. * @throws FileNotFoundException - * If there is no such a file or some other error occurs - * due to the given file opening. + * If there is no such a file or some other error occurs due to + * the given file opening. */ public PrintWriter(File file) throws FileNotFoundException { - this(new OutputStreamWriter( - new BufferedOutputStream(new FileOutputStream(file))), - false); + this(new OutputStreamWriter(new BufferedOutputStream( + new FileOutputStream(file))), false); } /** - * Constructs a new PrintWriter on the File file. The - * automatic flushing is set to false. An intermediate - * OutputStreamWriter will use a charset with the - * given name csn to encode characters. + * Constructs a new PrintWriter on the File file. The + * automatic flushing is set to false. An intermediate + * OutputStreamWriter will use a charset with the given name + * csn to encode characters. * * @param file * This writer's buffered destination. * @param csn * A charset name. * @throws FileNotFoundException - * If there is no such a file or some other error occurs - * due to the given file opening. + * If there is no such a file or some other error occurs due to + * the given file opening. * @throws UnsupportedEncodingException - * If a charset with the given name is not supported. + * If a charset with the given name is not supported. */ - public PrintWriter(File file, String csn) - throws FileNotFoundException, UnsupportedEncodingException { - this(new OutputStreamWriter( - new BufferedOutputStream(new FileOutputStream(file)), csn), - false); + public PrintWriter(File file, String csn) throws FileNotFoundException, + UnsupportedEncodingException { + this(new OutputStreamWriter(new BufferedOutputStream( + new FileOutputStream(file)), csn), false); } /** * Constructs a new PrintWriter on a file with the given file name - * fileName. The automatic flushing is set to - * false. An intermediate OutputStreamWriter - * will use the default for the current JVM instance charset to - * encode characters. + * fileName. The automatic flushing is set to + * false. An intermediate OutputStreamWriter + * will use the default for the current JVM instance charset to encode + * characters. * * @param fileName * The name of file which is this writer's buffered destination. * @throws FileNotFoundException - * If there is no such a file or some other error occurs - * due to the given file opening. + * If there is no such a file or some other error occurs due to + * the given file opening. */ public PrintWriter(String fileName) throws FileNotFoundException { - this(new OutputStreamWriter( - new BufferedOutputStream(new FileOutputStream(fileName))), - false); + this(new OutputStreamWriter(new BufferedOutputStream( + new FileOutputStream(fileName))), false); } /** * Constructs a new PrintWriter on a file with the given file name - * fileName. The automatic flushing is set to - * false. An intermediate OutputStreamWriter - * will use a charset with the given name csn to - * encode characters. + * fileName. The automatic flushing is set to + * false. An intermediate OutputStreamWriter + * will use a charset with the given name csn to encode + * characters. * * @param fileName * The name of file which is this writer's buffered destination. * @param csn * A charset name. * @throws FileNotFoundException - * If there is no such a file or some other error occurs - * due to the given file opening. + * If there is no such a file or some other error occurs due to + * the given file opening. * @throws UnsupportedEncodingException - * If a charset with the given name is not supported. + * If a charset with the given name is not supported. */ public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { - this(new OutputStreamWriter( - new BufferedOutputStream(new FileOutputStream(fileName)), csn), - false); + this(new OutputStreamWriter(new BufferedOutputStream( + new FileOutputStream(fileName)), csn), false); } /** - * Answers a boolean indicating whether or not this PrintWriter has - * encountered an error. If so, the receiver should probably be closed since - * further writes will not actually take place. A side effect of calling - * checkError is that the target Writer is flushed. - * - * @return boolean has an error occurred in this PrintWriter. - */ - public boolean checkError() { - if (out != null) { + * Answers a boolean indicating whether or not this PrintWriter has + * encountered an error. If so, the receiver should probably be closed since + * further writes will not actually take place. A side effect of calling + * checkError is that the target Writer is flushed. + * + * @return boolean has an error occurred in this PrintWriter. + */ + public boolean checkError() { + if (out != null) { flush(); } - return ioError; - } + return ioError; + } - /** - * Close this PrintWriter. This implementation flushes and then closes the - * target writer. If an error occurs, set an error in this PrintWriter to - * true. - * - */ - @Override + /** + * Close this PrintWriter. This implementation flushes and then closes the + * target writer. If an error occurs, set an error in this PrintWriter to + * true. + */ + @Override public void close() { - synchronized (lock) { - if (out != null) { - try { - out.close(); - } catch (IOException e) { - setError(); - } - out = null; - } - } - } - - /** - * Flush this PrintWriter to ensure all pending data is sent out to the - * target Writer. This implementation flushes the target writer. If an error - * occurs, set an error in this PrintWriter to true. - */ - @Override + synchronized (lock) { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + setError(); + } + out = null; + } + } + } + + /** + * Flush this PrintWriter to ensure all pending data is sent out to the + * target Writer. This implementation flushes the target writer. If an error + * occurs, set an error in this PrintWriter to true. + */ + @Override public void flush() { - synchronized (lock) { - if (out != null) { - try { - out.flush(); - } catch (IOException e) { - setError(); - } - } else { - setError(); - } - } - } - - /** - * Writes a string formatted by an intermediate Formatter - * to this writer using the given format string and arguments. A call to - * this method flushes the buffered output, if the automatic flushing - * is enabled. + synchronized (lock) { + if (out != null) { + try { + out.flush(); + } catch (IOException e) { + setError(); + } + } else { + setError(); + } + } + } + + /** + * Writes a string formatted by an intermediate Formatter to + * this writer using the given format string and arguments. A call to this + * method flushes the buffered output, if the automatic flushing is enabled. *

- * The method uses the default for the current JVM instance locale, as if - * it is specified by the Locale.getDefault() call. + * The method uses the default for the current JVM instance locale, as if it + * is specified by the Locale.getDefault() call. * * @param format * A format string. * @param args - * The arguments list. If there are more arguments than those - * specified by the format string, then the additional - * arguments are ignored. + * The arguments list. If there are more arguments than those + * specified by the format string, then the additional arguments + * are ignored. * @return This writer. * @throws IllegalFormatException - * If the format string is illegal or incompatible with the - * arguments or the arguments are less than those required by - * the format string or any other illegal situation. + * If the format string is illegal or incompatible with the + * arguments or the arguments are less than those required by + * the format string or any other illegal situation. * @throws NullPointerException - * If the given format is null. + * If the given format is null. */ public PrintWriter format(String format, Object... args) { return format(Locale.getDefault(), format, args); } /** - * Writes a string formatted by an intermediate Formatter - * to this writer using the given format string and arguments. A call to - * this method flushes the buffered output, if the automatic flushing - * is enabled. + * Writes a string formatted by an intermediate Formatter to + * this writer using the given format string and arguments. A call to this + * method flushes the buffered output, if the automatic flushing is enabled. * * @param l * The locale used in the method. If locale is null, then no @@ -290,16 +279,16 @@ * @param format * A format string. * @param args - * The arguments list. If there are more arguments than those - * specified by the format string, then the additional - * arguments are ignored. + * The arguments list. If there are more arguments than those + * specified by the format string, then the additional arguments + * are ignored. * @return This writer. * @throws IllegalFormatException - * If the format string is illegal or incompatible with the - * arguments or the arguments are less than those required by - * the format string or any other illegal situation. + * If the format string is illegal or incompatible with the + * arguments or the arguments are less than those required by + * the format string or any other illegal situation. * @throws NullPointerException - * If the given format is null. + * If the given format is null. */ public PrintWriter format(Locale l, String format, Object... args) { if (format == null) { @@ -313,34 +302,34 @@ } /** - * Prints a formatted string. The behavior of this method is the same - * as this writer's format(String format, Object... args) + * Prints a formatted string. The behavior of this method is the same as + * this writer's format(String format, Object... args) * method. *

- * The method uses the default for the current JVM instance locale, as if - * it is specified by the Locale.getDefault() call. + * The method uses the default for the current JVM instance locale, as if it + * is specified by the Locale.getDefault() call. * * @param format * A format string. * @param args - * The arguments list. If there are more arguments than those - * specified by the format string, then the additional - * arguments are ignored. + * The arguments list. If there are more arguments than those + * specified by the format string, then the additional arguments + * are ignored. * @return This writer. * @throws IllegalFormatException - * If the format string is illegal or incompatible with the - * arguments or the arguments are less than those required by - * the format string or any other illegal situation. + * If the format string is illegal or incompatible with the + * arguments or the arguments are less than those required by + * the format string or any other illegal situation. * @throws NullPointerException - * If the given format is null. + * If the given format is null. */ public PrintWriter printf(String format, Object... args) { return format(format, args); } /** - * Prints a formatted string. The behavior of this method is the same - * as this writer's + * Prints a formatted string. The behavior of this method is the same as + * this writer's * format(Locale l, String format, Object... args) method. * * @param l @@ -349,408 +338,411 @@ * @param format * A format string. * @param args - * The arguments list. If there are more arguments than those - * specified by the format string, then the additional - * arguments are ignored. + * The arguments list. If there are more arguments than those + * specified by the format string, then the additional arguments + * are ignored. * @return * @throws IllegalFormatException - * If the format string is illegal or incompatible with the - * arguments or the arguments are less than those required by - * the format string or any other illegal situation. + * If the format string is illegal or incompatible with the + * arguments or the arguments are less than those required by + * the format string or any other illegal situation. * @throws NullPointerException - * If the given format is null. + * If the given format is null. */ public PrintWriter printf(Locale l, String format, Object... args) { return format(l, format, args); } - private void newline() { - print(lineSeparator); - if (autoflush) { + /** + * Print a new line String onto the writer, flushing if autoflush enabled. + */ + private void newline() { + print(lineSeparator); + if (autoflush) { flush(); } - } + } + + /** + * Prints the String representation of the character array parameter + * charArray to the target Writer. + * + * @param charArray + * the character array to print on this Writer. + */ + public void print(char[] charArray) { + print(new String(charArray, 0, charArray.length)); + } + + /** + * Prints the String representation of the character parameter + * ch to the target Writer. + * + * @param ch + * the character to print on this Writer. + */ + public void print(char ch) { + print(String.valueOf(ch)); + } + + /** + * Prints the String representation of the double parameter + * dnum to the target Writer. + * + * @param dnum + * the double to print on this Writer. + */ + public void print(double dnum) { + print(String.valueOf(dnum)); + } + + /** + * Prints the String representation of the float parameter + * fnum to the target Writer. + * + * @param fnum + * the float to print on this Writer. + */ + public void print(float fnum) { + print(String.valueOf(fnum)); + } + + /** + * Prints the String representation of the int parameter + * inum to the target Writer. + * + * @param inum + * the int to print on this Writer. + */ + public void print(int inum) { + print(String.valueOf(inum)); + } - /** - * Prints the String representation of the character array parameter - * charArray to the target Writer. - * - * @param charArray - * the character array to print on this Writer. - */ - public void print(char[] charArray) { - print(new String(charArray, 0, charArray.length)); - } - - /** - * Prints the String representation of the character parameter - * ch to the target Writer. - * - * @param ch - * the character to print on this Writer. - */ - public void print(char ch) { - print(String.valueOf(ch)); - } - - /** - * Prints the String representation of the double parameter - * dnum to the target Writer. - * - * @param dnum - * the double to print on this Writer. - */ - public void print(double dnum) { - print(String.valueOf(dnum)); - } - - /** - * Prints the String representation of the float parameter - * fnum to the target Writer. - * - * @param fnum - * the float to print on this Writer. - */ - public void print(float fnum) { - print(String.valueOf(fnum)); - } - - /** - * Prints the String representation of the int parameter - * inum to the target Writer. - * - * @param inum - * the int to print on this Writer. - */ - public void print(int inum) { - print(String.valueOf(inum)); - } - - /** - * Prints the String representation of the long parameter - * lnum to the target Writer. - * - * @param lnum - * the long to print on this Writer. - */ - public void print(long lnum) { - print(String.valueOf(lnum)); - } - - /** - * Prints the String representation of the Object parameter obj - * to the target Writer. - * - * @param obj - * the Object to print on this Writer. - */ - public void print(Object obj) { - print(String.valueOf(obj)); - } - - /** - * Prints the String representation of the String parameter - * str to the target Writer. - * - * @param str - * the String to print on this Writer. - */ - public void print(String str) { - write(str != null ? str : String.valueOf((Object) null)); - } - - /** - * Prints the String representation of the boolean parameter - * bool to the target Writer. - * - * @param bool - * the boolean to print on this Writer. - */ - public void print(boolean bool) { - print(String.valueOf(bool)); - } - - /** - * Prints the String representation of the System property - * "line.separator" to the target Writer. - */ - public void println() { - synchronized (lock) { - newline(); - } - } - - /** - * Prints the String representation of the character array parameter - * charArray to the target Writer followed by the System - * property "line.separator". - * - * @param charArray - * the character array to print on this Writer. - */ - public void println(char[] charArray) { - println(new String(charArray, 0, charArray.length)); - } - - /** - * Prints the String representation of the character parameter - * ch to the target Writer followed by the System property - * "line.separator". - * - * @param ch - * the character to print on this Writer. - */ - public void println(char ch) { - println(String.valueOf(ch)); - } - - /** - * Prints the String representation of the double parameter - * dnum to the target Writer followed by the System property - * "line.separator". - * - * @param dnum - * the double to print on this Writer. - */ - public void println(double dnum) { - println(String.valueOf(dnum)); - } - - /** - * Prints the String representation of the float parameter - * fnum to the target Writer followed by the System property - * "line.separator". - * - * @param fnum - * the float to print on this Writer. - */ - public void println(float fnum) { - println(String.valueOf(fnum)); - } - - /** - * Prints the String representation of the int parameter - * inum to the target Writer followed by the System property - * "line.separator". - * - * @param inum - * the int to print on this Writer. - */ - public void println(int inum) { - println(String.valueOf(inum)); - } - - /** - * Prints the String representation of the long parameter - * lnum to the target Writer followed by the System property - * "line.separator". - * - * @param lnum - * the long to print on this Writer. - */ - public void println(long lnum) { - println(String.valueOf(lnum)); - } - - /** - * Prints the String representation of the Object parameter - * obj to the target Writer followed by the System property - * "line.separator". - * - * @param obj - * the Object to print on this Writer. - */ - public void println(Object obj) { - println(String.valueOf(obj)); - } - - /** - * Prints the String representation of the String parameter - * str to the target Writer followed by the System property - * "line.separator". - * - * @param str - * the String to print on this Writer. - */ - public void println(String str) { - synchronized (lock) { - print(str); - newline(); - } - } - - /** - * Prints the String representation of the boolean parameter - * bool to the target Writer followed by the System property - * "line.separator". - * - * @param bool - * the boolean to print on this Writer. - */ - public void println(boolean bool) { - println(String.valueOf(bool)); - } - - /** - * Set the flag indicating that this PrintWriter has encountered an IO - * error. - */ - protected void setError() { - synchronized (lock) { - ioError = true; - } - } - - /** - * Writes the entire character buffer buf to this Writer. - * - * @param buf - * the non-null array containing characters to write. - */ - @Override + /** + * Prints the String representation of the long parameter + * lnum to the target Writer. + * + * @param lnum + * the long to print on this Writer. + */ + public void print(long lnum) { + print(String.valueOf(lnum)); + } + + /** + * Prints the String representation of the Object parameter obj + * to the target Writer. + * + * @param obj + * the Object to print on this Writer. + */ + public void print(Object obj) { + print(String.valueOf(obj)); + } + + /** + * Prints the String representation of the String parameter + * str to the target Writer. + * + * @param str + * the String to print on this Writer. + */ + public void print(String str) { + write(str != null ? str : String.valueOf((Object) null)); + } + + /** + * Prints the String representation of the boolean parameter + * bool to the target Writer. + * + * @param bool + * the boolean to print on this Writer. + */ + public void print(boolean bool) { + print(String.valueOf(bool)); + } + + /** + * Prints the String representation of the System property + * "line.separator" to the target Writer. + */ + public void println() { + synchronized (lock) { + newline(); + } + } + + /** + * Prints the String representation of the character array parameter + * charArray to the target Writer followed by the System + * property "line.separator". + * + * @param charArray + * the character array to print on this Writer. + */ + public void println(char[] charArray) { + println(new String(charArray, 0, charArray.length)); + } + + /** + * Prints the String representation of the character parameter + * ch to the target Writer followed by the System property + * "line.separator". + * + * @param ch + * the character to print on this Writer. + */ + public void println(char ch) { + println(String.valueOf(ch)); + } + + /** + * Prints the String representation of the double parameter + * dnum to the target Writer followed by the System property + * "line.separator". + * + * @param dnum + * the double to print on this Writer. + */ + public void println(double dnum) { + println(String.valueOf(dnum)); + } + + /** + * Prints the String representation of the float parameter + * fnum to the target Writer followed by the System property + * "line.separator". + * + * @param fnum + * the float to print on this Writer. + */ + public void println(float fnum) { + println(String.valueOf(fnum)); + } + + /** + * Prints the String representation of the int parameter + * inum to the target Writer followed by the System property + * "line.separator". + * + * @param inum + * the int to print on this Writer. + */ + public void println(int inum) { + println(String.valueOf(inum)); + } + + /** + * Prints the String representation of the long parameter + * lnum to the target Writer followed by the System property + * "line.separator". + * + * @param lnum + * the long to print on this Writer. + */ + public void println(long lnum) { + println(String.valueOf(lnum)); + } + + /** + * Prints the String representation of the Object parameter + * obj to the target Writer followed by the System property + * "line.separator". + * + * @param obj + * the Object to print on this Writer. + */ + public void println(Object obj) { + println(String.valueOf(obj)); + } + + /** + * Prints the String representation of the String parameter + * str to the target Writer followed by the System property + * "line.separator". + * + * @param str + * the String to print on this Writer. + */ + public void println(String str) { + synchronized (lock) { + print(str); + newline(); + } + } + + /** + * Prints the String representation of the boolean parameter + * bool to the target Writer followed by the System property + * "line.separator". + * + * @param bool + * the boolean to print on this Writer. + */ + public void println(boolean bool) { + println(String.valueOf(bool)); + } + + /** + * Set the flag indicating that this PrintWriter has encountered an IO + * error. + */ + protected void setError() { + synchronized (lock) { + ioError = true; + } + } + + /** + * Writes the entire character buffer buf to this Writer. + * + * @param buf + * the non-null array containing characters to write. + */ + @Override public void write(char buf[]) { - write(buf, 0, buf.length); - } + write(buf, 0, buf.length); + } - /** - * Writes count characters starting at offset - * in buf - * to this Writer. - * - * @param buf the non-null array containing characters to write. - * @param offset offset in buf to retrieve characters - * @param count maximum number of characters to write - * - * @throws ArrayIndexOutOfBoundsException If offset or count are outside of bounds. - */ - @Override + /** + * Writes count characters starting at offset + * in buf + * to this Writer. + * + * @param buf the non-null array containing characters to write. + * @param offset offset in buf to retrieve characters + * @param count maximum number of characters to write + * + * @throws ArrayIndexOutOfBoundsException If offset or count are outside of bounds. + */ + @Override public void write(char buf[], int offset, int count) { - doWrite(buf, offset, count); - } + doWrite(buf, offset, count); + } - /** - * Writes the specified character to this Writer. This implementation writes - * the low order two bytes to the target writer. - * - * @param oneChar - * The character to write - */ - @Override + /** + * Writes the specified character to this Writer. This implementation writes + * the low order two bytes to the target writer. + * + * @param oneChar + * The character to write + */ + @Override public void write(int oneChar) { - doWrite(new char[] { (char) oneChar }, 0, 1); - } - - private final void doWrite(char buf[], int offset, int count) { - synchronized (lock) { - if (out != null) { - try { - out.write(buf, offset, count); - } catch (IOException e) { - setError(); - } - } else { - setError(); - } - } - } - - /** - * Writes the characters from the String str to this Writer. - * - * @param str - * the non-null String containing the characters to write. - */ - @Override + doWrite(new char[] { (char) oneChar }, 0, 1); + } + + private final void doWrite(char buf[], int offset, int count) { + synchronized (lock) { + if (out != null) { + try { + out.write(buf, offset, count); + } catch (IOException e) { + setError(); + } + } else { + setError(); + } + } + } + + /** + * Writes the characters from the String str to this Writer. + * + * @param str + * the non-null String containing the characters to write. + */ + @Override public void write(String str) { - write(str.toCharArray()); - } + write(str.toCharArray()); + } - /** - * Writes count characters from the String str - * starting at offset to this Writer. - * - * @param str - * the non-null String containing the characters to write. - * @param offset - * where in str to get chars from. - * @param count - * how many characters to write. - * - * @throws ArrayIndexOutOfBoundsException - * If offset or count are outside of bounds. - */ - @Override + /** + * Writes count characters from the String str + * starting at offset to this Writer. + * + * @param str + * the non-null String containing the characters to write. + * @param offset + * where in str to get chars from. + * @param count + * how many characters to write. + * + * @throws ArrayIndexOutOfBoundsException + * If offset or count are outside of bounds. + */ + @Override public void write(String str, int offset, int count) { - write(str.substring(offset, offset + count).toCharArray()); - } + write(str.substring(offset, offset + count).toCharArray()); + } - /** - * Append a char cto the PrintWriter. The - * PrintWriter.append(c) works the same way as - * PrintWriter.write(c). - * - * @override Writer.append - * @param c - * The character appended to the PrintWriter. - * @return The PrintWriter. - */ - @Override + /** + * Append a char cto the PrintWriter. The + * PrintWriter.append(c) works the same way as + * PrintWriter.write(c). + * + * @override Writer.append + * @param c + * The character appended to the PrintWriter. + * @return The PrintWriter. + */ + @Override public PrintWriter append(char c) { - write(c); - return this; - } - - /** - * Append a CharSequence csq to the PrintWriter. The - * PrintWriter.append(csq) works the same way as - * PrintWriter.write(csq.toString()). If csq - * is null, then "null" will be substituted for csq - * - * @override Writer.append - * @param csq - * The CharSequence appended to the PrintWriter. - * @return The PrintWriter - */ - @Override + write(c); + return this; + } + + /** + * Append a CharSequence csq to the PrintWriter. The + * PrintWriter.append(csq) works the same way as + * PrintWriter.write(csq.toString()). If csq + * is null, then "null" will be substituted for csq + * + * @override Writer.append + * @param csq + * The CharSequence appended to the PrintWriter. + * @return The PrintWriter + */ + @Override public PrintWriter append(CharSequence csq) { - if (null == csq) { - append(TOKEN_NULL, 0, TOKEN_NULL.length()); - } else { - append(csq, 0, csq.length()); - } - return this; - } - - /** - * Append a subsequence of a CharSequence csq to the - * PrintWriter. The first char and the last char of the subsequence is - * specified by the parameter start and end. - * The PrintWriter.append(csq) works the same way as - * PrintWriter.write(csq.subSequence(start,end).toString).If - * csq is null, then "null" will be substituted for - * csq. - * - * @override Writer.append - * @param csq - * The CharSequence appended to the PrintWriter. - * @param start - * The index of the first char in the CharSequence appended to - * the PrintWriter. - * @param end - * The index of the char after the last one in the CharSequence - * appended to the PrintWriter. - * @return The PrintWriter. - * @throws IndexOutOfBoundsException - * If start is less than end, end is greater than the length of - * the CharSequence, or start or end is negative. - */ - @Override + if (null == csq) { + append(TOKEN_NULL, 0, TOKEN_NULL.length()); + } else { + append(csq, 0, csq.length()); + } + return this; + } + + /** + * Append a subsequence of a CharSequence csq to the + * PrintWriter. The first char and the last char of the subsequence is + * specified by the parameter start and end. + * The PrintWriter.append(csq) works the same way as + * PrintWriter.write(csq.subSequence(start,end).toString).If + * csq is null, then "null" will be substituted for + * csq. + * + * @override Writer.append + * @param csq + * The CharSequence appended to the PrintWriter. + * @param start + * The index of the first char in the CharSequence appended to + * the PrintWriter. + * @param end + * The index of the char after the last one in the CharSequence + * appended to the PrintWriter. + * @return The PrintWriter. + * @throws IndexOutOfBoundsException + * If start is less than end, end is greater than the length of + * the CharSequence, or start or end is negative. + */ + @Override public PrintWriter append(CharSequence csq, int start, int end) { - if (null == csq) { - csq = TOKEN_NULL; - } - String output = csq.subSequence(start, end).toString(); - write(output, 0, output.length()); - return this; - } + if (null == csq) { + csq = TOKEN_NULL; + } + String output = csq.subSequence(start, end).toString(); + write(output, 0, output.length()); + return this; + } } Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java?view=diff&rev=492652&r1=492651&r2=492652 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java (original) +++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java Thu Jan 4 09:47:01 2007 @@ -25,7 +25,6 @@ * useful. There is a progammable limit to the number of bytes which may be * pushed back. If the buffer of pushed back bytes is empty, bytes are read from * the source input stream. - * */ public class PushbackInputStream extends FilterInputStream { /** @@ -46,7 +45,6 @@ * * @param in * the InputStream to allow pushback operations on. - * */ public PushbackInputStream(InputStream in) { super(in); @@ -65,12 +63,11 @@ */ public PushbackInputStream(InputStream in, int size) { super(in); - if (size > 0) { - buf = (in == null) ? null : new byte[size]; - pos = size; - } else { + if (size <= 0) { throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$ } + buf = (in == null) ? null : new byte[size]; + pos = size; } /** @@ -86,10 +83,10 @@ */ @Override public int available() throws IOException { - if (buf != null) { - return buf.length - pos + in.available(); + if (buf == null) { + throw new IOException(); } - throw new IOException(); + return buf.length - pos + in.available(); } /** @@ -134,16 +131,16 @@ */ @Override public int read() throws IOException { - if (buf != null) { - // Is there a pushback byte available? - if (pos < buf.length) { - return (buf[pos++] & 0xFF); - } - // Assume read() in the InputStream will return low-order byte or -1 - // if end of stream. - return in.read(); + if (buf == null) { + throw new IOException(); } - throw new IOException(); + // Is there a pushback byte available? + if (pos < buf.length) { + return (buf[pos++] & 0xFF); + } + // Assume read() in the InputStream will return low-order byte or -1 + // if end of stream. + return in.read(); } /** @@ -167,39 +164,41 @@ */ @Override public int read(byte[] buffer, int offset, int length) throws IOException { - if (buf != null && buffer != null) { - // avoid int overflow - if (0 <= offset && offset <= buffer.length && 0 <= length - && length <= buffer.length - offset) { - int copiedBytes = 0, copyLength = 0, newOffset = offset; - // Are there pushback bytes available? - if (pos < buf.length) { - copyLength = (buf.length - pos >= length) ? length - : buf.length - pos; - System.arraycopy(buf, pos, buffer, newOffset, copyLength); - newOffset += copyLength; - copiedBytes += copyLength; - // Use up the bytes in the local buffer - pos += copyLength; - } - // Have we copied enough? - if (copyLength == length) { - return length; - } - int inCopied = in.read(buffer, newOffset, length - copiedBytes); - if (inCopied > 0) { - return inCopied + copiedBytes; - } - if (copiedBytes == 0) { - return inCopied; - } - return copiedBytes; - } - throw new ArrayIndexOutOfBoundsException(); - } else if (buf == null) { + if (buf == null) { throw new IOException(); } - throw new NullPointerException(); + if (buffer == null) { + throw new NullPointerException(); + } + // avoid int overflow + if (offset < 0 || offset > buffer.length || length < 0 + || length > buffer.length - offset) { + throw new ArrayIndexOutOfBoundsException(); + } + + int copiedBytes = 0, copyLength = 0, newOffset = offset; + // Are there pushback bytes available? + if (pos < buf.length) { + copyLength = (buf.length - pos >= length) ? length : buf.length + - pos; + System.arraycopy(buf, pos, buffer, newOffset, copyLength); + newOffset += copyLength; + copiedBytes += copyLength; + // Use up the bytes in the local buffer + pos += copyLength; + } + // Have we copied enough? + if (copyLength == length) { + return length; + } + int inCopied = in.read(buffer, newOffset, length - copiedBytes); + if (inCopied > 0) { + return inCopied + copiedBytes; + } + if (copiedBytes == 0) { + return inCopied; + } + return copiedBytes; } /** @@ -219,22 +218,21 @@ */ @Override public long skip(long count) throws IOException { - if (in != null) { - if (count <= 0) { - return 0; - } - int numSkipped = 0; - if (pos < buf.length) { - numSkipped += (count < buf.length - pos) ? - count : buf.length - pos; - pos += numSkipped; - } - if (numSkipped < count) { - numSkipped += in.skip(count - numSkipped); - } - return numSkipped; + if (in == null) { + throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + } + if (count <= 0) { + return 0; + } + int numSkipped = 0; + if (pos < buf.length) { + numSkipped += (count < buf.length - pos) ? count : buf.length - pos; + pos += numSkipped; + } + if (numSkipped < count) { + numSkipped += in.skip(count - numSkipped); } - throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + return numSkipped; } /** @@ -279,14 +277,14 @@ throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ } // avoid int overflow - if (0 <= offset && offset <= buffer.length && 0 <= length - && length <= buffer.length - offset) { - for (int i = offset + length - 1; i >= offset; i--) { - unread(buffer[i]); - } - } else { + if (offset < 0 || offset > buffer.length || length < 0 + || length > buffer.length - offset) { throw new ArrayIndexOutOfBoundsException(); } + + for (int i = offset + length - 1; i >= offset; i--) { + unread(buffer[i]); + } } /** @@ -301,15 +299,13 @@ * If the pushback buffer is already full. */ public void unread(int oneByte) throws IOException { - if (buf != null) { - if (pos != 0) { - buf[--pos] = (byte) oneByte; - } else { - throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ - } - } else { + if (buf == null) { throw new IOException(); } + if (pos == 0) { + throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ + } + buf[--pos] = (byte) oneByte; } /** @@ -334,7 +330,6 @@ * @throws IOException * If the method is called */ - @Override public void reset() throws IOException { throw new IOException(); Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java?view=diff&rev=492652&r1=492651&r2=492652 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java (original) +++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java Thu Jan 4 09:47:01 2007 @@ -68,12 +68,11 @@ */ public PushbackReader(Reader in, int size) { super(in); - if (size > 0) { - buf = new char[size]; - pos = size; - } else { + if (size <= 0) { throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$ } + buf = new char[size]; + pos = size; } /** @@ -134,18 +133,18 @@ @Override public int read() throws IOException { synchronized (lock) { - if (buf != null) { - /* Is there a pushback character available? */ - if (pos < buf.length) { - return buf[pos++]; - } - /** - * Assume read() in the InputStream will return 2 lowest-order - * bytes or -1 if end of stream. - */ - return in.read(); + if (buf == null) { + throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + } + /* Is there a pushback character available? */ + if (pos < buf.length) { + return buf[pos++]; } - throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + /** + * Assume read() in the InputStream will return 2 lowest-order bytes + * or -1 if end of stream. + */ + return in.read(); } } @@ -170,41 +169,42 @@ */ @Override public int read(char[] buffer, int offset, int count) throws IOException { - synchronized (lock) { - if (null == buf) { - throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ - } - // avoid int overflow - if (offset < 0 || count < 0 || offset > buffer.length - count) { - throw new IndexOutOfBoundsException(); - } - int copiedChars = 0; - int copyLength = 0; - int newOffset = offset; - /* Are there pushback chars available? */ - if (pos < buf.length) { - copyLength = (buf.length - pos >= count) ? count : buf.length - - pos; - System.arraycopy(buf, pos, buffer, newOffset, copyLength); - newOffset += copyLength; - copiedChars += copyLength; - /* Use up the chars in the local buffer */ - pos += copyLength; - } - /* Have we copied enough? */ - if (copyLength == count) { - return count; - } - int inCopied = in.read(buffer, newOffset, count - copiedChars); - if (inCopied > 0) { - return inCopied + copiedChars; - } - if (copiedChars == 0) { - return inCopied; - } - return copiedChars; - } - } + synchronized (lock) { + if (null == buf) { + throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + } + // avoid int overflow + if (offset < 0 || count < 0 || offset > buffer.length - count) { + throw new IndexOutOfBoundsException(); + } + + int copiedChars = 0; + int copyLength = 0; + int newOffset = offset; + /* Are there pushback chars available? */ + if (pos < buf.length) { + copyLength = (buf.length - pos >= count) ? count : buf.length + - pos; + System.arraycopy(buf, pos, buffer, newOffset, copyLength); + newOffset += copyLength; + copiedChars += copyLength; + /* Use up the chars in the local buffer */ + pos += copyLength; + } + /* Have we copied enough? */ + if (copyLength == count) { + return count; + } + int inCopied = in.read(buffer, newOffset, count - copiedChars); + if (inCopied > 0) { + return inCopied + copiedChars; + } + if (copiedChars == 0) { + return inCopied; + } + return copiedChars; + } + } /** * Answers a boolean indicating whether or not this @@ -224,10 +224,10 @@ @Override public boolean ready() throws IOException { synchronized (lock) { - if (buf != null) { - return (buf.length - pos > 0 || in.ready()); + if (buf == null) { + throw new IOException(Msg.getString("K0080")); //$NON-NLS-1$ } - throw new IOException(Msg.getString("K0080")); //$NON-NLS-1$ + return (buf.length - pos > 0 || in.ready()); } } @@ -279,26 +279,26 @@ * If the pushback buffer becomes, or is, full. */ public void unread(char[] buffer, int offset, int count) throws IOException { - synchronized (lock) { - if (buf == null) { - throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ - } - if (count > pos) { - // Pushback buffer full - throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ - } - if (buffer == null) { - throw new NullPointerException(); - } - // avoid int overflow - if (offset < 0 || count < 0 || offset > buffer.length - count) { - throw new ArrayIndexOutOfBoundsException(); - } - for (int i = offset + count - 1; i >= offset; i--) { - unread(buffer[i]); - } - } - } + synchronized (lock) { + if (buf == null) { + throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + } + if (count > pos) { + // Pushback buffer full + throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ + } + if (buffer == null) { + throw new NullPointerException(); + } + // avoid int overflow + if (offset < 0 || count < 0 || offset > buffer.length - count) { + throw new ArrayIndexOutOfBoundsException(); + } + for (int i = offset + count - 1; i >= offset; i--) { + unread(buffer[i]); + } + } + } /** * Push back one char. Takes the char oneChar @@ -313,15 +313,13 @@ */ public void unread(int oneChar) throws IOException { synchronized (lock) { - if (buf != null) { - if (pos != 0) { - buf[--pos] = (char) oneChar; - } else { - throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ - } - } else { - throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + if (buf == null) { + throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + } + if (pos == 0) { + throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$ } + buf[--pos] = (char) oneChar; } } @@ -346,26 +344,26 @@ throw new IllegalArgumentException(); } synchronized (lock) { - if (buf != null) { - if (count == 0) { - return 0; - } - long inSkipped; - int availableFromBuffer = buf.length - pos; - if (availableFromBuffer > 0) { - long requiredFromIn = count - availableFromBuffer; - if (requiredFromIn <= 0) { - pos += count; - return count; - } - pos += availableFromBuffer; - inSkipped = in.skip(requiredFromIn); - } else { - inSkipped = in.skip(count); + if (buf == null) { + throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + } + if (count == 0) { + return 0; + } + long inSkipped; + int availableFromBuffer = buf.length - pos; + if (availableFromBuffer > 0) { + long requiredFromIn = count - availableFromBuffer; + if (requiredFromIn <= 0) { + pos += count; + return count; } - return inSkipped + availableFromBuffer; + pos += availableFromBuffer; + inSkipped = in.skip(requiredFromIn); + } else { + inSkipped = in.skip(count); } - throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ + return inSkipped + availableFromBuffer; } } } Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java?view=diff&rev=492652&r1=492651&r2=492652 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java (original) +++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java Thu Jan 4 09:47:01 2007 @@ -91,7 +91,6 @@ // Sync file, but not necessarily metadata options = IFileSystem.O_RDWRSYNC; } - } else { throw new IllegalArgumentException(Msg.getString("K0081")); //$NON-NLS-1$ } @@ -114,6 +113,7 @@ try { fd.sync(); } catch (IOException e) { + // Ignored } } } @@ -126,7 +126,6 @@ * "r" for read access only, or "rw" for * read/write access. * - * * @param fileName * the filename of the file to open. * @param mode @@ -208,6 +207,13 @@ return fileSystem.seek(fd.descriptor, 0L, IFileSystem.SEEK_CUR); } + /** + * Checks to see if the file is currently open. Returns silently if it is, + * and throws an exception if it is not. + * + * @throws IOException + * the receiver is closed. + */ private synchronized void openCheck() throws IOException { if (fd.descriptor < 0) { throw new IOException(); @@ -328,10 +334,10 @@ */ public final boolean readBoolean() throws IOException { int temp = this.read(); - if (temp >= 0) { - return temp != 0; + if (temp < 0) { + throw new EOFException(); } - throw new EOFException(); + return temp != 0; } /** @@ -346,10 +352,10 @@ */ public final byte readByte() throws IOException { int temp = this.read(); - if (temp >= 0) { - return (byte) temp; + if (temp < 0) { + throw new EOFException(); } - throw new EOFException(); + return (byte) temp; } /** @@ -364,10 +370,10 @@ */ public final char readChar() throws IOException { byte[] buffer = new byte[2]; - if (read(buffer, 0, buffer.length) == buffer.length) { - return (char) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff)); + if (read(buffer, 0, buffer.length) != buffer.length) { + throw new EOFException(); } - throw new EOFException(); + return (char) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff)); } /** @@ -437,25 +443,22 @@ */ public final void readFully(byte[] buffer, int offset, int count) throws IOException { - if (buffer != null) { - // avoid int overflow - if (0 <= offset && offset <= buffer.length && 0 <= count - && count <= buffer.length - offset) { - while (count > 0) { - int result = read(buffer, offset, count); - if (result >= 0) { - offset += result; - count -= result; - } else { - throw new EOFException(); - } - } - } else { - throw new IndexOutOfBoundsException(); - } - } else { + if (buffer == null) { throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$ } + // avoid int overflow + if (offset < 0 || offset > buffer.length || count < 0 + || count > buffer.length - offset) { + throw new IndexOutOfBoundsException(); + } + while (count > 0) { + int result = read(buffer, offset, count); + if (result < 0) { + throw new EOFException(); + } + offset += result; + count -= result; + } } /** @@ -470,11 +473,11 @@ */ public final int readInt() throws IOException { byte[] buffer = new byte[4]; - if (read(buffer, 0, buffer.length) == buffer.length) { - return ((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) << 16) - + ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff); + if (read(buffer, 0, buffer.length) != buffer.length) { + throw new EOFException(); } - throw new EOFException(); + return ((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) << 16) + + ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff); } /** @@ -533,15 +536,15 @@ */ public final long readLong() throws IOException { byte[] buffer = new byte[8]; - if (read(buffer, 0, buffer.length) == buffer.length) { - return ((long) (((buffer[0] & 0xff) << 24) - + ((buffer[1] & 0xff) << 16) + ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff)) << 32) - + ((long) (buffer[4] & 0xff) << 24) - + ((buffer[5] & 0xff) << 16) - + ((buffer[6] & 0xff) << 8) - + (buffer[7] & 0xff); + if (read(buffer, 0, buffer.length) != buffer.length) { + throw new EOFException(); } - throw new EOFException(); + return ((long) (((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) << 16) + + ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff)) << 32) + + ((long) (buffer[4] & 0xff) << 24) + + ((buffer[5] & 0xff) << 16) + + ((buffer[6] & 0xff) << 8) + + (buffer[7] & 0xff); } /** @@ -556,10 +559,10 @@ */ public final short readShort() throws IOException { byte[] buffer = new byte[2]; - if (read(buffer, 0, buffer.length) == buffer.length) { - return (short) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff)); + if (read(buffer, 0, buffer.length) != buffer.length) { + throw new EOFException(); } - throw new EOFException(); + return (short) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff)); } /** @@ -575,10 +578,10 @@ */ public final int readUnsignedByte() throws IOException { int temp = this.read(); - if (temp >= 0) { - return temp; + if (temp < 0) { + throw new EOFException(); } - throw new EOFException(); + return temp; } /** @@ -595,10 +598,10 @@ */ public final int readUnsignedShort() throws IOException { byte[] buffer = new byte[2]; - if (read(buffer, 0, buffer.length) == buffer.length) { - return ((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff); + if (read(buffer, 0, buffer.length) != buffer.length) { + throw new EOFException(); } - throw new EOFException(); + return ((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff); } /** @@ -617,10 +620,10 @@ return ""; //$NON-NLS-1$ } byte[] buf = new byte[utfSize]; - if (read(buf, 0, buf.length) == buf.length) { - return Util.convertFromUTF8(buf, 0, utfSize); + if (read(buf, 0, buf.length) != buf.length) { + throw new EOFException(); } - throw new EOFException(); + return Util.convertFromUTF8(buf, 0, utfSize); } /** @@ -1014,27 +1017,26 @@ utfCount += 3; } } - if (utfCount <= 65535) { - byte utfBytes[] = new byte[utfCount + 2]; - int utfIndex = 2; - for (int i = 0; i < length; i++) { - int charValue = str.charAt(i); - if (charValue > 0 && charValue <= 127) { - utfBytes[utfIndex++] = (byte) charValue; - } else if (charValue <= 2047) { - utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); - } else { - utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); - } - } - utfBytes[0] = (byte) (utfCount >> 8); - utfBytes[1] = (byte) utfCount; - write(utfBytes); - } else { + if (utfCount > 65535) { throw new UTFDataFormatException(Msg.getString("K0068")); //$NON-NLS-1$ } + byte utfBytes[] = new byte[utfCount + 2]; + int utfIndex = 2; + for (int i = 0; i < length; i++) { + int charValue = str.charAt(i); + if (charValue > 0 && charValue <= 127) { + utfBytes[utfIndex++] = (byte) charValue; + } else if (charValue <= 2047) { + utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6))); + utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); + } else { + utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12))); + utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6))); + utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); + } + } + utfBytes[0] = (byte) (utfCount >> 8); + utfBytes[1] = (byte) utfCount; + write(utfBytes); } } Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Reader.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Reader.java?view=diff&rev=492652&r1=492651&r2=492652 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Reader.java (original) +++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Reader.java Thu Jan 4 09:47:01 2007 @@ -18,6 +18,7 @@ package java.io; import java.nio.CharBuffer; +import java.nio.ReadOnlyBufferException; /** * Reader is an Abstract class for reading Character Streams. Subclasses of @@ -27,236 +28,236 @@ * @see Writer */ public abstract class Reader implements Readable, Closeable { - /** - * The object used to synchronize access to the reader. - */ - protected Object lock; - - /** - * Constructs a new character stream Reader using this as the - * Object to synchronize critical regions around. - */ - protected Reader() { - super(); - lock = this; - } - - /** - * Constructs a new character stream Reader using lock as the - * Object to synchronize critical regions around. - * - * @param lock - * the Object to synchronize critical regions - * around. - */ - protected Reader(Object lock) { - if (lock != null) - this.lock = lock; - else - throw new NullPointerException(); - } - - /** - * Close this Reader. This must be implemented by any concrete subclasses. - * The implementation should free any resources associated with the Reader. - * - * @throws IOException - * If an error occurs attempting to close this Reader. - */ - public abstract void close() throws IOException; - - /** - * Set a Mark position in this Reader. The parameter readLimit - * indicates how many characters can be read before a mark is invalidated. - * Sending reset() will reposition the reader back to the marked position - * provided readLimit has not been surpassed. - *

- * This default implementation simply throws IOException and concrete - * subclasses must provide their own implementations. - * - * @param readLimit - * an int representing how many characters must be read before - * invalidating the mark. - * - * @throws IOException - * If an error occurs attempting mark this Reader. - */ - public void mark(int readLimit) throws IOException { - throw new IOException(); - } - - /** - * Answers a boolean indicating whether or not this Reader supports mark() - * and reset(). This class a default implementation which answers false. - * - * @return true if mark() and reset() are supported, - * false otherwise. This implementation returns - * false. - */ - public boolean markSupported() { - return false; - } - - /** - * Reads a single character from this reader and returns the result as an - * int. The 2 higher-order characters are set to 0. If the end of reader was - * encountered then return -1. - * - * @return the character read or -1 if end of reader. - * - * @throws IOException - * If the Reader is already closed or some other IO error - * occurs. - */ - public int read() throws IOException { - synchronized (lock) { - char charArray[] = new char[1]; - if (read(charArray, 0, 1) != -1) - return charArray[0]; - return -1; - } - } - - /** - * Reads characters from this Reader and stores them in the character array - * buf starting at offset 0. Returns the number of characters - * actually read or -1 if the end of reader was encountered. - * - * @param buf - * character array to store the read characters - * @return how many characters were successfully read in or else -1 if the - * end of the reader was detected. - * - * @throws IOException - * If the Reader is already closed or some other IO error - * occurs. - */ - public int read(char buf[]) throws IOException { - return read(buf, 0, buf.length); - } - - /** - * Reads at most count characters from this Reader and stores - * them at offset in the character array buf. - * Returns the number of characters actually read or -1 if the end of reader - * was encountered. - * - * @param buf - * character array to store the read characters - * @param offset - * offset in buf to store the read characters - * @param count - * how many characters should be read in - * @return how many characters were successfully read in or else -1 if the - * end of the reader was detected. - * - * @throws IOException - * If the Reader is already closed or some other IO error - * occurs. - */ - public abstract int read(char buf[], int offset, int count) - throws IOException; - - /** - * Answers a boolean indicating whether or not this Reader is - * ready to be read without blocking. If the result is true, - * the next read() will not block. If the result is - * false this Reader may or may not block when - * read() is sent. - * - * @return true if the receiver will not block when - * read() is called, false if unknown - * or blocking will occur. - * - * @throws IOException - * If the Reader is already closed or some other IO error - * occurs. - */ - public boolean ready() throws IOException { - return false; - } - - /** - * Reset this Readers position to the last mark() location. - * Invocations of read()/skip() will occur from this new - * location. If this Reader was not marked, the implementation of - * reset() is implementation specific. See the comment for - * the specific Reader subclass for implementation details. The default - * action is to throw IOException. - * - * @throws IOException - * If a problem occured or the receiver does not support - * mark()/reset(). - */ - public void reset() throws IOException { - throw new IOException(); - } - - /** - * Skips count number of characters in this Reader. - * Subsequent read()'s will not return these characters - * unless reset() is used. This method may perform multiple - * reads to read count characters. - * - * @param count - * how many characters should be passed over - * @return how many characters were successfully passed over - * - * @throws IOException - * If the Reader is closed when the call is made or if an IO - * error occurs during the operation. - */ - public long skip(long count) throws IOException { - if (count >= 0) { - synchronized (lock) { - long skipped = 0; - int toRead = count < 512 ? (int) count : 512; - char charsSkipped[] = new char[toRead]; - while (skipped < count) { - int read = read(charsSkipped, 0, toRead); - if (read == -1) { - return skipped; - } - skipped += read; - if (read < toRead) { - return skipped; - } - if (count - skipped < toRead) { - toRead = (int) (count - skipped); - } - } - return skipped; - } - } - throw new IllegalArgumentException(); - } - - /** - * Read chars from the Reader and then put them to the target - * CharBuffer. Only put method is called on the target. - * - * @param target - * the destination CharBuffer - * @return the actual number of chars put to the target. -1 - * when the Reader has reached the end before the method is called. - * @throws IOException - * if any I/O error raises in the procedure - * @throws NullPointerException - * if the target CharBuffer is null - * @throws ReadOnlyBufferException - * if the target CharBuffer is readonly - * - */ - public int read(CharBuffer target) throws IOException { - if (null == target) { - throw new NullPointerException(); - } - int length = target.length(); - char[] buf = new char[length]; - length = Math.min(length, read(buf)); - if (length > 0) { - target.put(buf, 0, length); - } - return length; - } + /** + * The object used to synchronize access to the reader. + */ + protected Object lock; + + /** + * Constructs a new character stream Reader using this as the + * Object to synchronize critical regions around. + */ + protected Reader() { + super(); + lock = this; + } + + /** + * Constructs a new character stream Reader using lock as the + * Object to synchronize critical regions around. + * + * @param lock + * the Object to synchronize critical regions + * around. + */ + protected Reader(Object lock) { + if (lock == null) { + throw new NullPointerException(); + } + this.lock = lock; + } + + /** + * Close this Reader. This must be implemented by any concrete subclasses. + * The implementation should free any resources associated with the Reader. + * + * @throws IOException + * If an error occurs attempting to close this Reader. + */ + public abstract void close() throws IOException; + + /** + * Set a Mark position in this Reader. The parameter readLimit + * indicates how many characters can be read before a mark is invalidated. + * Sending reset() will reposition the reader back to the marked position + * provided readLimit has not been surpassed. + *

+ * This default implementation simply throws IOException and concrete + * subclasses must provide their own implementations. + * + * @param readLimit + * an int representing how many characters must be read before + * invalidating the mark. + * + * @throws IOException + * If an error occurs attempting mark this Reader. + */ + public void mark(int readLimit) throws IOException { + throw new IOException(); + } + + /** + * Answers a boolean indicating whether or not this Reader supports mark() + * and reset(). This class a default implementation which answers false. + * + * @return true if mark() and reset() are supported, + * false otherwise. This implementation returns + * false. + */ + public boolean markSupported() { + return false; + } + + /** + * Reads a single character from this reader and returns the result as an + * int. The 2 higher-order characters are set to 0. If the end of reader was + * encountered then return -1. + * + * @return the character read or -1 if end of reader. + * + * @throws IOException + * If the Reader is already closed or some other IO error + * occurs. + */ + public int read() throws IOException { + synchronized (lock) { + char charArray[] = new char[1]; + if (read(charArray, 0, 1) != -1) { + return charArray[0]; + } + return -1; + } + } + + /** + * Reads characters from this Reader and stores them in the character array + * buf starting at offset 0. Returns the number of characters + * actually read or -1 if the end of reader was encountered. + * + * @param buf + * character array to store the read characters + * @return how many characters were successfully read in or else -1 if the + * end of the reader was detected. + * + * @throws IOException + * If the Reader is already closed or some other IO error + * occurs. + */ + public int read(char buf[]) throws IOException { + return read(buf, 0, buf.length); + } + + /** + * Reads at most count characters from this Reader and stores + * them at offset in the character array buf. + * Returns the number of characters actually read or -1 if the end of reader + * was encountered. + * + * @param buf + * character array to store the read characters + * @param offset + * offset in buf to store the read characters + * @param count + * how many characters should be read in + * @return how many characters were successfully read in or else -1 if the + * end of the reader was detected. + * + * @throws IOException + * If the Reader is already closed or some other IO error + * occurs. + */ + public abstract int read(char buf[], int offset, int count) + throws IOException; + + /** + * Answers a boolean indicating whether or not this Reader is + * ready to be read without blocking. If the result is true, + * the next read() will not block. If the result is + * false this Reader may or may not block when + * read() is sent. + * + * @return true if the receiver will not block when + * read() is called, false if unknown + * or blocking will occur. + * + * @throws IOException + * If the Reader is already closed or some other IO error + * occurs. + */ + public boolean ready() throws IOException { + return false; + } + + /** + * Reset this Readers position to the last mark() location. + * Invocations of read()/skip() will occur from this new + * location. If this Reader was not marked, the implementation of + * reset() is implementation specific. See the comment for + * the specific Reader subclass for implementation details. The default + * action is to throw IOException. + * + * @throws IOException + * If a problem occured or the receiver does not support + * mark()/reset(). + */ + public void reset() throws IOException { + throw new IOException(); + } + + /** + * Skips count number of characters in this Reader. + * Subsequent read()'s will not return these characters + * unless reset() is used. This method may perform multiple + * reads to read count characters. + * + * @param count + * how many characters should be passed over + * @return how many characters were successfully passed over + * + * @throws IOException + * If the Reader is closed when the call is made or if an IO + * error occurs during the operation. + */ + public long skip(long count) throws IOException { + if (count < 0) { + throw new IllegalArgumentException(); + } + synchronized (lock) { + long skipped = 0; + int toRead = count < 512 ? (int) count : 512; + char charsSkipped[] = new char[toRead]; + while (skipped < count) { + int read = read(charsSkipped, 0, toRead); + if (read == -1) { + return skipped; + } + skipped += read; + if (read < toRead) { + return skipped; + } + if (count - skipped < toRead) { + toRead = (int) (count - skipped); + } + } + return skipped; + } + } + + /** + * Read chars from the Reader and then put them to the target + * CharBuffer. Only put method is called on the target. + * + * @param target + * the destination CharBuffer + * @return the actual number of chars put to the target. -1 + * when the Reader has reached the end before the method is called. + * @throws IOException + * if any I/O error raises in the procedure + * @throws NullPointerException + * if the target CharBuffer is null + * @throws ReadOnlyBufferException + * if the target CharBuffer is readonly + */ + public int read(CharBuffer target) throws IOException { + if (null == target) { + throw new NullPointerException(); + } + int length = target.length(); + char[] buf = new char[length]; + length = Math.min(length, read(buf)); + if (length > 0) { + target.put(buf, 0, length); + } + return length; + } }