You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/18 03:45:39 UTC

svn commit: r432462 [17/21] - /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java Thu Aug 17 18:45:35 2006
@@ -1,744 +1,744 @@
-/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-import java.security.AccessController;
-import java.util.Formatter;
-import java.util.IllegalFormatException;
-import java.util.Locale;
-
-import org.apache.harmony.luni.util.Msg;
-import org.apache.harmony.luni.util.PriviAction;
-
-/**
- * PrintWriter is a class which takes either an OutputStream or Writer and
- * provides convenience methods for printing common data types in a human
- * 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.
-	 */
-	boolean ioError;
-
-	/**
-	 * indicates whether or not this PrintWriter should flush its contents after
-	 * printing a new line.
-	 */
-	boolean autoflush;
-
-	private final String lineSeparator = AccessController
-            .doPrivileged(new PriviAction<String>("line.separator")); //$NON-NLS-1$
-
-	/**
-	 * Constructs a new PrintWriter on the OutputStream <code>out</code>. 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 <code>out</code>. 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 <code>wr</code>. 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 <code>file</code>. The
-     * automatic flushing is set to <code>false</code>. An intermediate 
-     * <code>OutputStreamWriter</code> 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.
-     */
-    public PrintWriter(File file) throws FileNotFoundException {
-        this(new OutputStreamWriter(
-                new BufferedOutputStream(new FileOutputStream(file))),
-                false);
-    }
-
-    /**
-     * Constructs a new PrintWriter on the File <code>file</code>. The 
-     * automatic flushing is set to <code>false</code>. An intermediate 
-     * <code>OutputStreamWriter</code> will use a charset with the 
-     * given name <code>csn</code> 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.
-     * @throws UnsupportedEncodingException
-     *            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);
-    }
-
-    /**
-     * Constructs a new PrintWriter on a file with the given file name
-     * <code>fileName</code>. The automatic flushing is set to 
-     * <code>false</code>. An intermediate <code>OutputStreamWriter</code> 
-     * 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.
-     */
-    public PrintWriter(String fileName) throws FileNotFoundException {
-        this(new OutputStreamWriter(
-                new BufferedOutputStream(new FileOutputStream(fileName))),
-                false);
-    }
-
-    /**
-     * Constructs a new PrintWriter on a file with the given file name
-     * <code>fileName</code>. The automatic flushing is set to 
-     * <code>false</code>. An intermediate <code>OutputStreamWriter</code> 
-     * will use a charset with the given name <code>csn</code> 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.
-     * @throws UnsupportedEncodingException
-     *            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);
-    }
-
-    /**
-	 * 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;
-	}
-
-	/**
-	 * Close this PrintWriter. This implementation flushes and then closes the
-	 * target writer. If an error occurs, set an error in this PrintWriter to
-	 * <code>true</code>.
-	 * 
-	 */
-	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 <code>true</code>.
-	 */
-	public void flush() {
-		synchronized (lock) {
-			if (out != null) {
-				try {
-					out.flush();
-				} catch (IOException e) {
-					setError();
-				}
-			} else {
-				setError();
-			}
-		}
-	}
-
-    /**
-     * Writes a string formatted by an intermediate <code>Formatter</code> 
-     * 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.
-     * <p>
-     * The method uses the default for the current JVM instance locale, as if
-     * it is specified by the <code>Locale.getDefault()</code> 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.
-     * @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.
-     * @throws NullPointerException
-     *            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 <code>Formatter</code> 
-     * 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
-     *            localization will be applied.
-     * @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.
-     * @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.
-     * @throws NullPointerException
-     *            If the given format is null.
-     */
-    public PrintWriter format(Locale l, String format, Object... args) {
-        if (format == null) {
-            throw new NullPointerException(Msg.getString("K0351")); //$NON-NLS-1$
-        }
-        new Formatter(this, l).format(format, args);
-        if (autoflush) {
-            flush();
-        }
-        return this;
-    }
-
-    /**
-     * Prints a formatted string. The behavior of this method is the same 
-     * as this writer's <code>format(String format, Object... args)</code> 
-     * method.
-     * <p>
-     * The method uses the default for the current JVM instance locale, as if
-     * it is specified by the <code>Locale.getDefault()</code> 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.
-     * @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.
-     * @throws NullPointerException
-     *            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 
-     * <code>format(Locale l, String format, Object... args)</code> method.
-     * 
-     * @param l
-     *            The locale used in the method. If locale is null, then no
-     *            localization will be applied.
-     * @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.
-     * @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.
-     * @throws NullPointerException
-     *            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) {
-            flush();
-        }
-	}
-
-	/**
-	 * Prints the String representation of the character array parameter
-	 * <code>charArray</code> 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
-	 * <code>ch</code> 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 <code>double</code> parameter
-	 * <code>dnum</code> to the target Writer.
-	 * 
-	 * @param dnum
-	 *            the <code>double</code> to print on this Writer.
-	 */
-	public void print(double dnum) {
-		print(String.valueOf(dnum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>float</code> parameter
-	 * <code>fnum</code> to the target Writer.
-	 * 
-	 * @param fnum
-	 *            the <code>float</code> to print on this Writer.
-	 */
-	public void print(float fnum) {
-		print(String.valueOf(fnum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>int</code> parameter
-	 * <code>inum</code> to the target Writer.
-	 * 
-	 * @param inum
-	 *            the <code>int</code> to print on this Writer.
-	 */
-	public void print(int inum) {
-		print(String.valueOf(inum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>long</code> parameter
-	 * <code>lnum</code> to the target Writer.
-	 * 
-	 * @param lnum
-	 *            the <code>long</code> to print on this Writer.
-	 */
-	public void print(long lnum) {
-		print(String.valueOf(lnum));
-	}
-
-	/**
-	 * Prints the String representation of the Object parameter <code>obj</code>
-	 * 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 <code>String</code> parameter
-	 * <code>str</code> to the target Writer.
-	 * 
-	 * @param str
-	 *            the <code>String</code> to print on this Writer.
-	 */
-	public void print(String str) {
-		write(str != null ? str : String.valueOf((Object) null));
-	}
-
-	/**
-	 * Prints the String representation of the <code>boolean</code> parameter
-	 * <code>bool</code> to the target Writer.
-	 * 
-	 * @param bool
-	 *            the <code>boolean</code> to print on this Writer.
-	 */
-	public void print(boolean bool) {
-		print(String.valueOf(bool));
-	}
-
-	/**
-	 * Prints the String representation of the System property
-	 * <code>"line.separator"</code> to the target Writer.
-	 */
-	public void println() {
-		synchronized (lock) {
-			newline();
-		}
-	}
-
-	/**
-	 * Prints the String representation of the character array parameter
-	 * <code>charArray</code> to the target Writer followed by the System
-	 * property <code>"line.separator"</code>.
-	 * 
-	 * @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
-	 * <code>ch</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param ch
-	 *            the character to print on this Writer.
-	 */
-	public void println(char ch) {
-		println(String.valueOf(ch));
-	}
-
-	/**
-	 * Prints the String representation of the <code>double</code> parameter
-	 * <code>dnum</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param dnum
-	 *            the double to print on this Writer.
-	 */
-	public void println(double dnum) {
-		println(String.valueOf(dnum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>float</code> parameter
-	 * <code>fnum</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param fnum
-	 *            the float to print on this Writer.
-	 */
-	public void println(float fnum) {
-		println(String.valueOf(fnum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>int</code> parameter
-	 * <code>inum</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param inum
-	 *            the int to print on this Writer.
-	 */
-	public void println(int inum) {
-		println(String.valueOf(inum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>long</code> parameter
-	 * <code>lnum</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param lnum
-	 *            the long to print on this Writer.
-	 */
-	public void println(long lnum) {
-		println(String.valueOf(lnum));
-	}
-
-	/**
-	 * Prints the String representation of the <code>Object</code> parameter
-	 * <code>obj</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param obj
-	 *            the <code>Object</code> to print on this Writer.
-	 */
-	public void println(Object obj) {
-		println(String.valueOf(obj));
-	}
-
-	/**
-	 * Prints the String representation of the <code>String</code> parameter
-	 * <code>str</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @param str
-	 *            the <code>String</code> to print on this Writer.
-	 */
-	public void println(String str) {
-		synchronized (lock) {
-			print(str);
-			newline();
-		}
-	}
-
-	/**
-	 * Prints the String representation of the <code>boolean</code> parameter
-	 * <code>bool</code> to the target Writer followed by the System property
-	 * <code>"line.separator"</code>.
-	 * 
-	 * @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.
-	 */
-	public void write(char buf[]) {
-		write(buf, 0, buf.length);
-	}
-
-	/**
-	 * Writes <code>count</code> characters starting at <code>offset</code>
-	 * in <code>buf<code>
-	 * 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.
-	 */
-	public void write(char buf[], int offset, int 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
-	 */
-	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 <code>str</code> to this Writer.
-	 * 
-	 * @param str
-	 *            the non-null String containing the characters to write.
-	 */
-	public void write(String str) {
-		write(str.toCharArray());
-	}
-
-	/**
-	 * Writes <code>count</code> characters from the String <code>str</code>
-	 * starting at <code>offset</code> to this Writer.
-	 * 
-	 * @param str
-	 *            the non-null String containing the characters to write.
-	 * @param offset
-	 *            where in <code>str</code> to get chars from.
-	 * @param count
-	 *            how many characters to write.
-	 * 
-	 * @throws ArrayIndexOutOfBoundsException
-	 *             If offset or count are outside of bounds.
-	 */
-	public void write(String str, int offset, int count) {
-		write(str.substring(offset, offset + count).toCharArray());
-	}
-
-	/**
-	 * Append a char <code>c</code>to the PrintWriter. The
-	 * PrintWriter.append(<code>c</code>) works the same way as
-	 * PrintWriter.write(<code>c</code>).
-	 * 
-	 * @override Writer.append
-	 * @param c
-	 *            The character appended to the PrintWriter.
-	 * @return The PrintWriter.
-	 */
-	public PrintWriter append(char c) {
-		write(c);
-		return this;
-	}
-
-	/**
-	 * Append a CharSequence <code>csq</code> to the PrintWriter. The
-	 * PrintWriter.append(<code>csq</code>) works the same way as
-	 * PrintWriter.write(<code>csq</code>.toString()). If <code>csq</code>
-	 * is null, then "null" will be substituted for <code>csq</code>
-	 * 
-	 * @override Writer.append
-	 * @param csq
-	 *            The CharSequence appended to the PrintWriter.
-	 * @return The PrintWriter
-	 */
-	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 <code>csq</code> to the
-	 * PrintWriter. The first char and the last char of the subsequence is
-	 * specified by the parameter <code>start</code> and <code>end</code>.
-	 * The PrintWriter.append(<code>csq</code>) works the same way as
-	 * PrintWriter.write(<code>csq</code>.subSequence(<code>start</code>,<code>end</code>).toString).If
-	 * <code>csq</code> is null, then "null" will be substituted for
-	 * <code>csq</code>.
-	 * 
-	 * @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.
-	 */
-	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;
-	}
-}
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+import java.security.AccessController;
+import java.util.Formatter;
+import java.util.IllegalFormatException;
+import java.util.Locale;
+
+import org.apache.harmony.luni.util.Msg;
+import org.apache.harmony.luni.util.PriviAction;
+
+/**
+ * PrintWriter is a class which takes either an OutputStream or Writer and
+ * provides convenience methods for printing common data types in a human
+ * 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.
+	 */
+	boolean ioError;
+
+	/**
+	 * indicates whether or not this PrintWriter should flush its contents after
+	 * printing a new line.
+	 */
+	boolean autoflush;
+
+	private final String lineSeparator = AccessController
+            .doPrivileged(new PriviAction<String>("line.separator")); //$NON-NLS-1$
+
+	/**
+	 * Constructs a new PrintWriter on the OutputStream <code>out</code>. 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 <code>out</code>. 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 <code>wr</code>. 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 <code>file</code>. The
+     * automatic flushing is set to <code>false</code>. An intermediate 
+     * <code>OutputStreamWriter</code> 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.
+     */
+    public PrintWriter(File file) throws FileNotFoundException {
+        this(new OutputStreamWriter(
+                new BufferedOutputStream(new FileOutputStream(file))),
+                false);
+    }
+
+    /**
+     * Constructs a new PrintWriter on the File <code>file</code>. The 
+     * automatic flushing is set to <code>false</code>. An intermediate 
+     * <code>OutputStreamWriter</code> will use a charset with the 
+     * given name <code>csn</code> 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.
+     * @throws UnsupportedEncodingException
+     *            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);
+    }
+
+    /**
+     * Constructs a new PrintWriter on a file with the given file name
+     * <code>fileName</code>. The automatic flushing is set to 
+     * <code>false</code>. An intermediate <code>OutputStreamWriter</code> 
+     * 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.
+     */
+    public PrintWriter(String fileName) throws FileNotFoundException {
+        this(new OutputStreamWriter(
+                new BufferedOutputStream(new FileOutputStream(fileName))),
+                false);
+    }
+
+    /**
+     * Constructs a new PrintWriter on a file with the given file name
+     * <code>fileName</code>. The automatic flushing is set to 
+     * <code>false</code>. An intermediate <code>OutputStreamWriter</code> 
+     * will use a charset with the given name <code>csn</code> 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.
+     * @throws UnsupportedEncodingException
+     *            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);
+    }
+
+    /**
+	 * 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;
+	}
+
+	/**
+	 * Close this PrintWriter. This implementation flushes and then closes the
+	 * target writer. If an error occurs, set an error in this PrintWriter to
+	 * <code>true</code>.
+	 * 
+	 */
+	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 <code>true</code>.
+	 */
+	public void flush() {
+		synchronized (lock) {
+			if (out != null) {
+				try {
+					out.flush();
+				} catch (IOException e) {
+					setError();
+				}
+			} else {
+				setError();
+			}
+		}
+	}
+
+    /**
+     * Writes a string formatted by an intermediate <code>Formatter</code> 
+     * 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.
+     * <p>
+     * The method uses the default for the current JVM instance locale, as if
+     * it is specified by the <code>Locale.getDefault()</code> 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.
+     * @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.
+     * @throws NullPointerException
+     *            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 <code>Formatter</code> 
+     * 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
+     *            localization will be applied.
+     * @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.
+     * @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.
+     * @throws NullPointerException
+     *            If the given format is null.
+     */
+    public PrintWriter format(Locale l, String format, Object... args) {
+        if (format == null) {
+            throw new NullPointerException(Msg.getString("K0351")); //$NON-NLS-1$
+        }
+        new Formatter(this, l).format(format, args);
+        if (autoflush) {
+            flush();
+        }
+        return this;
+    }
+
+    /**
+     * Prints a formatted string. The behavior of this method is the same 
+     * as this writer's <code>format(String format, Object... args)</code> 
+     * method.
+     * <p>
+     * The method uses the default for the current JVM instance locale, as if
+     * it is specified by the <code>Locale.getDefault()</code> 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.
+     * @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.
+     * @throws NullPointerException
+     *            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 
+     * <code>format(Locale l, String format, Object... args)</code> method.
+     * 
+     * @param l
+     *            The locale used in the method. If locale is null, then no
+     *            localization will be applied.
+     * @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.
+     * @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.
+     * @throws NullPointerException
+     *            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) {
+            flush();
+        }
+	}
+
+	/**
+	 * Prints the String representation of the character array parameter
+	 * <code>charArray</code> 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
+	 * <code>ch</code> 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 <code>double</code> parameter
+	 * <code>dnum</code> to the target Writer.
+	 * 
+	 * @param dnum
+	 *            the <code>double</code> to print on this Writer.
+	 */
+	public void print(double dnum) {
+		print(String.valueOf(dnum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>float</code> parameter
+	 * <code>fnum</code> to the target Writer.
+	 * 
+	 * @param fnum
+	 *            the <code>float</code> to print on this Writer.
+	 */
+	public void print(float fnum) {
+		print(String.valueOf(fnum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>int</code> parameter
+	 * <code>inum</code> to the target Writer.
+	 * 
+	 * @param inum
+	 *            the <code>int</code> to print on this Writer.
+	 */
+	public void print(int inum) {
+		print(String.valueOf(inum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>long</code> parameter
+	 * <code>lnum</code> to the target Writer.
+	 * 
+	 * @param lnum
+	 *            the <code>long</code> to print on this Writer.
+	 */
+	public void print(long lnum) {
+		print(String.valueOf(lnum));
+	}
+
+	/**
+	 * Prints the String representation of the Object parameter <code>obj</code>
+	 * 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 <code>String</code> parameter
+	 * <code>str</code> to the target Writer.
+	 * 
+	 * @param str
+	 *            the <code>String</code> to print on this Writer.
+	 */
+	public void print(String str) {
+		write(str != null ? str : String.valueOf((Object) null));
+	}
+
+	/**
+	 * Prints the String representation of the <code>boolean</code> parameter
+	 * <code>bool</code> to the target Writer.
+	 * 
+	 * @param bool
+	 *            the <code>boolean</code> to print on this Writer.
+	 */
+	public void print(boolean bool) {
+		print(String.valueOf(bool));
+	}
+
+	/**
+	 * Prints the String representation of the System property
+	 * <code>"line.separator"</code> to the target Writer.
+	 */
+	public void println() {
+		synchronized (lock) {
+			newline();
+		}
+	}
+
+	/**
+	 * Prints the String representation of the character array parameter
+	 * <code>charArray</code> to the target Writer followed by the System
+	 * property <code>"line.separator"</code>.
+	 * 
+	 * @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
+	 * <code>ch</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param ch
+	 *            the character to print on this Writer.
+	 */
+	public void println(char ch) {
+		println(String.valueOf(ch));
+	}
+
+	/**
+	 * Prints the String representation of the <code>double</code> parameter
+	 * <code>dnum</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param dnum
+	 *            the double to print on this Writer.
+	 */
+	public void println(double dnum) {
+		println(String.valueOf(dnum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>float</code> parameter
+	 * <code>fnum</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param fnum
+	 *            the float to print on this Writer.
+	 */
+	public void println(float fnum) {
+		println(String.valueOf(fnum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>int</code> parameter
+	 * <code>inum</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param inum
+	 *            the int to print on this Writer.
+	 */
+	public void println(int inum) {
+		println(String.valueOf(inum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>long</code> parameter
+	 * <code>lnum</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param lnum
+	 *            the long to print on this Writer.
+	 */
+	public void println(long lnum) {
+		println(String.valueOf(lnum));
+	}
+
+	/**
+	 * Prints the String representation of the <code>Object</code> parameter
+	 * <code>obj</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param obj
+	 *            the <code>Object</code> to print on this Writer.
+	 */
+	public void println(Object obj) {
+		println(String.valueOf(obj));
+	}
+
+	/**
+	 * Prints the String representation of the <code>String</code> parameter
+	 * <code>str</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @param str
+	 *            the <code>String</code> to print on this Writer.
+	 */
+	public void println(String str) {
+		synchronized (lock) {
+			print(str);
+			newline();
+		}
+	}
+
+	/**
+	 * Prints the String representation of the <code>boolean</code> parameter
+	 * <code>bool</code> to the target Writer followed by the System property
+	 * <code>"line.separator"</code>.
+	 * 
+	 * @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.
+	 */
+	public void write(char buf[]) {
+		write(buf, 0, buf.length);
+	}
+
+	/**
+	 * Writes <code>count</code> characters starting at <code>offset</code>
+	 * in <code>buf<code>
+	 * 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.
+	 */
+	public void write(char buf[], int offset, int 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
+	 */
+	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 <code>str</code> to this Writer.
+	 * 
+	 * @param str
+	 *            the non-null String containing the characters to write.
+	 */
+	public void write(String str) {
+		write(str.toCharArray());
+	}
+
+	/**
+	 * Writes <code>count</code> characters from the String <code>str</code>
+	 * starting at <code>offset</code> to this Writer.
+	 * 
+	 * @param str
+	 *            the non-null String containing the characters to write.
+	 * @param offset
+	 *            where in <code>str</code> to get chars from.
+	 * @param count
+	 *            how many characters to write.
+	 * 
+	 * @throws ArrayIndexOutOfBoundsException
+	 *             If offset or count are outside of bounds.
+	 */
+	public void write(String str, int offset, int count) {
+		write(str.substring(offset, offset + count).toCharArray());
+	}
+
+	/**
+	 * Append a char <code>c</code>to the PrintWriter. The
+	 * PrintWriter.append(<code>c</code>) works the same way as
+	 * PrintWriter.write(<code>c</code>).
+	 * 
+	 * @override Writer.append
+	 * @param c
+	 *            The character appended to the PrintWriter.
+	 * @return The PrintWriter.
+	 */
+	public PrintWriter append(char c) {
+		write(c);
+		return this;
+	}
+
+	/**
+	 * Append a CharSequence <code>csq</code> to the PrintWriter. The
+	 * PrintWriter.append(<code>csq</code>) works the same way as
+	 * PrintWriter.write(<code>csq</code>.toString()). If <code>csq</code>
+	 * is null, then "null" will be substituted for <code>csq</code>
+	 * 
+	 * @override Writer.append
+	 * @param csq
+	 *            The CharSequence appended to the PrintWriter.
+	 * @return The PrintWriter
+	 */
+	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 <code>csq</code> to the
+	 * PrintWriter. The first char and the last char of the subsequence is
+	 * specified by the parameter <code>start</code> and <code>end</code>.
+	 * The PrintWriter.append(<code>csq</code>) works the same way as
+	 * PrintWriter.write(<code>csq</code>.subSequence(<code>start</code>,<code>end</code>).toString).If
+	 * <code>csq</code> is null, then "null" will be substituted for
+	 * <code>csq</code>.
+	 * 
+	 * @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.
+	 */
+	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;
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PrintWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java Thu Aug 17 18:45:35 2006
@@ -1,340 +1,340 @@
-/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-import org.apache.harmony.luni.util.Msg;
-
-/**
- * PushbackInputStream is a filter class which allows bytes read to be pushed
- * back into the stream so that they can be reread. Parsers may find this
- * 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 {
-    /**
-     * The <code>byte</code> array containing the bytes to read.
-     */
-    protected byte[] buf;
-
-    /**
-     * The current position within the byte array <code>buf</code>. A value
-     * equal to buf.length indicates no bytes available. A value of 0 indicates
-     * the buffer is full.
-     */
-    protected int pos;
-
-    /**
-     * Constructs a new PushbackInputStream on the InputStream <code>in</code>.
-     * The size of the pushback buffer is set to the default, or 1 byte.
-     * 
-     * @param in
-     *            the InputStream to allow pushback operations on.
-     * 
-     */
-    public PushbackInputStream(InputStream in) {
-        super(in);
-        buf = (in == null) ? null : new byte[1];
-        pos = 1;
-    }
-
-    /**
-     * Constructs a new PushbackInputStream on the InputStream <code>in</code>.
-     * The size of the pushback buffer is set to <code>size</code>.
-     * 
-     * @param in
-     *            the InputStream to allow pushback operations on.
-     * @param size
-     *            the size of the pushback buffer (<code>size>=0</code>).
-     */
-    public PushbackInputStream(InputStream in, int size) {
-        super(in);
-        if (size > 0) {
-            buf = (in == null) ? null : new byte[size];
-            pos = size;
-        } else {
-            throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
-        }
-    }
-
-    /**
-     * Answers a int representing then number of bytes that are available before
-     * this PushbackInputStream will block. This method returns the number of
-     * bytes available in the pushback buffer plus those available in the target
-     * stream.
-     * 
-     * @return int the number of bytes available before blocking.
-     * 
-     * @throws java.io.IOException
-     *             If an error occurs in this stream.
-     */
-    @Override
-    public int available() throws IOException {
-        if (buf != null) {
-            return buf.length - pos + in.available();
-        }
-        throw new IOException();
-    }
-
-    /**
-     * Close this PushbackInputStream. This implementation closes the target
-     * stream.
-     * 
-     * @throws IOException
-     *             If an error occurs attempting to close this stream.
-     */
-    @Override
-    public void close() throws IOException {
-        if (in != null) {
-            in.close();
-            in = null;
-            buf = null;
-        }
-    }
-
-    /**
-     * Answers a boolean indicating whether or not this PushbackInputStream
-     * supports mark() and reset(). This implementation always answers false
-     * since PushbackInputStreams do not support mark/reset.
-     * 
-     * @return boolean indicates whether or not mark() and reset() are
-     *         supported.
-     */
-    @Override
-    public boolean markSupported() {
-        return false;
-    }
-
-    /**
-     * Reads a single byte from this PushbackInputStream and returns the result
-     * as an int. The low-order byte is returned or -1 of the end of stream was
-     * encountered. If the pushback buffer does not contain any available bytes
-     * then a byte from the target input stream is returned.
-     * 
-     * @return int The byte read or -1 if end of stream.
-     * 
-     * @throws IOException
-     *             If an IOException occurs.
-     */
-    @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();
-        }
-        throw new IOException();
-    }
-
-    /**
-     * Reads at most <code>length</code> bytes from this PushbackInputStream
-     * and stores them in byte array <code>buffer</code> starting at
-     * <code>offset</code>. Answer the number of bytes actually read or -1 if
-     * no bytes were read and end of stream was encountered. This implementation
-     * reads bytes from the pushback buffer first, then the target stream if
-     * more bytes are required to satisfy <code>count</code>.
-     * 
-     * @param buffer
-     *            the byte array in which to store the read bytes.
-     * @param offset
-     *            the offset in <code>buffer</code> to store the read bytes.
-     * @param length
-     *            the maximum number of bytes to store in <code>buffer</code>.
-     * @return the number of bytes actually read or -1 if end of stream.
-     * 
-     * @throws IOException
-     *             If an IOException occurs.
-     */
-    @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) {
-            throw new IOException();
-        }
-        throw new NullPointerException();
-    }
-
-    /**
-     * Skips <code>count</code> number of bytes in this PushbackInputStream.
-     * Subsequent <code>read()</code>'s will not return these bytes unless
-     * <code>reset()</code> is used. This implementation skips
-     * <code>count</code> number of bytes in the buffer and/or the target
-     * stream.
-     * 
-     * @param count
-     *            the number of bytes to skip.
-     * @return the number of bytes actually skipped.
-     * 
-     * @throws IOException
-     *             If the stream is already closed or another IOException
-     *             occurs.
-     */
-    @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;
-        }
-        throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
-    }
-
-    /**
-     * Push back all the bytes in <code>buffer</code>. The bytes are pushed
-     * so that they would be read back buffer[0], buffer[1], etc. If the push
-     * back buffer cannot handle the entire contents of <code>buffer</code>,
-     * an IOException will be thrown. Some of the buffer may already be in the
-     * buffer after the exception is thrown.
-     * 
-     * @param buffer
-     *            the byte array containing bytes to push back into the stream.
-     * 
-     * @throws IOException
-     *             If the pushback buffer becomes, or is, full.
-     */
-    public void unread(byte[] buffer) throws IOException {
-        unread(buffer, 0, buffer.length);
-    }
-
-    /**
-     * Push back <code>length</code> number of bytes in <code>buffer</code>
-     * starting at <code>offset</code>. The bytes are pushed so that they
-     * would be read back buffer[offset], buffer[offset+1], etc. If the push
-     * back buffer cannot handle the bytes copied from <code>buffer</code>,
-     * an IOException will be thrown. Some of the bytes may already be in the
-     * buffer after the exception is thrown.
-     * 
-     * @param buffer
-     *            the byte array containing bytes to push back into the stream.
-     * @param offset
-     *            the location to start taking bytes to push back.
-     * @param length
-     *            the number of bytes to push back.
-     * 
-     * @throws IOException
-     *             If the pushback buffer becomes, or is, full.
-     */
-    public void unread(byte[] buffer, int offset, int length)
-            throws IOException {
-        if (length > pos) {
-            // Pushback buffer full
-            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 {
-            throw new ArrayIndexOutOfBoundsException();
-        }
-    }
-
-    /**
-     * Push back one <code>byte</code>. Takes the byte <code>oneByte</code>
-     * and puts in in the local buffer of bytes to read back before accessing
-     * the target input stream.
-     * 
-     * @param oneByte
-     *            the byte to push back into the stream.
-     * 
-     * @throws IOException
-     *             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 {
-            throw new IOException();
-        }
-    }
-
-    /**
-     * Make a mark of the current position in the stream but the mark method
-     * does nothing.
-     * 
-     * @param readlimit
-     *            the maximum number of bytes that are able to be read before
-     *            the mark becomes invalid
-     * @override the method mark in FilterInputStream
-     */
-    @Override
-    public void mark(int readlimit) {
-        return;
-    }
-
-    /**
-     * Reset current position to the mark made previously int the stream, but
-     * the reset method will throw IOException and do nothing else if called.
-     * 
-     * @override the method reset in FilterInputStream
-     * @throws IOException
-     *             If the method is called
-     */
-
-    @Override
-    public void reset() throws IOException {
-        throw new IOException();
-    }
-}
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+import org.apache.harmony.luni.util.Msg;
+
+/**
+ * PushbackInputStream is a filter class which allows bytes read to be pushed
+ * back into the stream so that they can be reread. Parsers may find this
+ * 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 {
+    /**
+     * The <code>byte</code> array containing the bytes to read.
+     */
+    protected byte[] buf;
+
+    /**
+     * The current position within the byte array <code>buf</code>. A value
+     * equal to buf.length indicates no bytes available. A value of 0 indicates
+     * the buffer is full.
+     */
+    protected int pos;
+
+    /**
+     * Constructs a new PushbackInputStream on the InputStream <code>in</code>.
+     * The size of the pushback buffer is set to the default, or 1 byte.
+     * 
+     * @param in
+     *            the InputStream to allow pushback operations on.
+     * 
+     */
+    public PushbackInputStream(InputStream in) {
+        super(in);
+        buf = (in == null) ? null : new byte[1];
+        pos = 1;
+    }
+
+    /**
+     * Constructs a new PushbackInputStream on the InputStream <code>in</code>.
+     * The size of the pushback buffer is set to <code>size</code>.
+     * 
+     * @param in
+     *            the InputStream to allow pushback operations on.
+     * @param size
+     *            the size of the pushback buffer (<code>size>=0</code>).
+     */
+    public PushbackInputStream(InputStream in, int size) {
+        super(in);
+        if (size > 0) {
+            buf = (in == null) ? null : new byte[size];
+            pos = size;
+        } else {
+            throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
+        }
+    }
+
+    /**
+     * Answers a int representing then number of bytes that are available before
+     * this PushbackInputStream will block. This method returns the number of
+     * bytes available in the pushback buffer plus those available in the target
+     * stream.
+     * 
+     * @return int the number of bytes available before blocking.
+     * 
+     * @throws java.io.IOException
+     *             If an error occurs in this stream.
+     */
+    @Override
+    public int available() throws IOException {
+        if (buf != null) {
+            return buf.length - pos + in.available();
+        }
+        throw new IOException();
+    }
+
+    /**
+     * Close this PushbackInputStream. This implementation closes the target
+     * stream.
+     * 
+     * @throws IOException
+     *             If an error occurs attempting to close this stream.
+     */
+    @Override
+    public void close() throws IOException {
+        if (in != null) {
+            in.close();
+            in = null;
+            buf = null;
+        }
+    }
+
+    /**
+     * Answers a boolean indicating whether or not this PushbackInputStream
+     * supports mark() and reset(). This implementation always answers false
+     * since PushbackInputStreams do not support mark/reset.
+     * 
+     * @return boolean indicates whether or not mark() and reset() are
+     *         supported.
+     */
+    @Override
+    public boolean markSupported() {
+        return false;
+    }
+
+    /**
+     * Reads a single byte from this PushbackInputStream and returns the result
+     * as an int. The low-order byte is returned or -1 of the end of stream was
+     * encountered. If the pushback buffer does not contain any available bytes
+     * then a byte from the target input stream is returned.
+     * 
+     * @return int The byte read or -1 if end of stream.
+     * 
+     * @throws IOException
+     *             If an IOException occurs.
+     */
+    @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();
+        }
+        throw new IOException();
+    }
+
+    /**
+     * Reads at most <code>length</code> bytes from this PushbackInputStream
+     * and stores them in byte array <code>buffer</code> starting at
+     * <code>offset</code>. Answer the number of bytes actually read or -1 if
+     * no bytes were read and end of stream was encountered. This implementation
+     * reads bytes from the pushback buffer first, then the target stream if
+     * more bytes are required to satisfy <code>count</code>.
+     * 
+     * @param buffer
+     *            the byte array in which to store the read bytes.
+     * @param offset
+     *            the offset in <code>buffer</code> to store the read bytes.
+     * @param length
+     *            the maximum number of bytes to store in <code>buffer</code>.
+     * @return the number of bytes actually read or -1 if end of stream.
+     * 
+     * @throws IOException
+     *             If an IOException occurs.
+     */
+    @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) {
+            throw new IOException();
+        }
+        throw new NullPointerException();
+    }
+
+    /**
+     * Skips <code>count</code> number of bytes in this PushbackInputStream.
+     * Subsequent <code>read()</code>'s will not return these bytes unless
+     * <code>reset()</code> is used. This implementation skips
+     * <code>count</code> number of bytes in the buffer and/or the target
+     * stream.
+     * 
+     * @param count
+     *            the number of bytes to skip.
+     * @return the number of bytes actually skipped.
+     * 
+     * @throws IOException
+     *             If the stream is already closed or another IOException
+     *             occurs.
+     */
+    @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;
+        }
+        throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+    }
+
+    /**
+     * Push back all the bytes in <code>buffer</code>. The bytes are pushed
+     * so that they would be read back buffer[0], buffer[1], etc. If the push
+     * back buffer cannot handle the entire contents of <code>buffer</code>,
+     * an IOException will be thrown. Some of the buffer may already be in the
+     * buffer after the exception is thrown.
+     * 
+     * @param buffer
+     *            the byte array containing bytes to push back into the stream.
+     * 
+     * @throws IOException
+     *             If the pushback buffer becomes, or is, full.
+     */
+    public void unread(byte[] buffer) throws IOException {
+        unread(buffer, 0, buffer.length);
+    }
+
+    /**
+     * Push back <code>length</code> number of bytes in <code>buffer</code>
+     * starting at <code>offset</code>. The bytes are pushed so that they
+     * would be read back buffer[offset], buffer[offset+1], etc. If the push
+     * back buffer cannot handle the bytes copied from <code>buffer</code>,
+     * an IOException will be thrown. Some of the bytes may already be in the
+     * buffer after the exception is thrown.
+     * 
+     * @param buffer
+     *            the byte array containing bytes to push back into the stream.
+     * @param offset
+     *            the location to start taking bytes to push back.
+     * @param length
+     *            the number of bytes to push back.
+     * 
+     * @throws IOException
+     *             If the pushback buffer becomes, or is, full.
+     */
+    public void unread(byte[] buffer, int offset, int length)
+            throws IOException {
+        if (length > pos) {
+            // Pushback buffer full
+            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 {
+            throw new ArrayIndexOutOfBoundsException();
+        }
+    }
+
+    /**
+     * Push back one <code>byte</code>. Takes the byte <code>oneByte</code>
+     * and puts in in the local buffer of bytes to read back before accessing
+     * the target input stream.
+     * 
+     * @param oneByte
+     *            the byte to push back into the stream.
+     * 
+     * @throws IOException
+     *             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 {
+            throw new IOException();
+        }
+    }
+
+    /**
+     * Make a mark of the current position in the stream but the mark method
+     * does nothing.
+     * 
+     * @param readlimit
+     *            the maximum number of bytes that are able to be read before
+     *            the mark becomes invalid
+     * @override the method mark in FilterInputStream
+     */
+    @Override
+    public void mark(int readlimit) {
+        return;
+    }
+
+    /**
+     * Reset current position to the mark made previously int the stream, but
+     * the reset method will throw IOException and do nothing else if called.
+     * 
+     * @override the method reset in FilterInputStream
+     * @throws IOException
+     *             If the method is called
+     */
+
+    @Override
+    public void reset() throws IOException {
+        throw new IOException();
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native