Index: /src/main/java/java/io/Writer.java =================================================================== --- /src/main/java/java/io/Writer.java (revision 388012) +++ /src/main/java/java/io/Writer.java (working copy) @@ -1,4 +1,4 @@ -/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable +/* 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. @@ -23,7 +23,9 @@ * * @see Reader */ -public abstract class Writer { +public abstract class Writer implements Appendable, Closeable, Flushable { + static final String TOKEN_NULL = "null"; + /** The object used to syncronize access to the writer. */ protected Object lock; @@ -164,4 +166,74 @@ } else throw new StringIndexOutOfBoundsException(); } + + /** + * Append a char cto the Writer. The Writer.append(c) + * works the same as Writer.write(c). + * + * @param c + * The character appended to the Writer. + * @return The Writer. + * @throws IOException + * If any IOException raises during the procedure. + */ + public Writer append(char c) throws IOException { + write(c); + return this; + } + + /** + * Append a CharSequence csq to the Writer. The + * Writer.append(csq) works the same way as Writer.write(csq.toString()). + * If csq is null, then "null" will be substituted for + * csq. + * + * @param csq + * The CharSequence appended to the Writer. + * @return The Writer. + * @throws IOException + * If any IOException raises during the procedure. + */ + public Writer append(CharSequence csq) throws IOException { + if (null == csq) { + write(TOKEN_NULL); + } else { + write(csq.toString()); + } + return this; + } + + /** + * Append a subsequence of a CharSequence csq to the Writer. + * The first char and the last char of the subsequnce is specified by the + * parameter start and end. The + * Writer.append(csq) works the same way as Writer.write (csqcsq.subSequence(start,end).toString). + * If csq is null, then "null" will be substituted for + * csq. + * + * @param csq + * The CharSequence appended to the Writaer. + * @param start + * The index of the first char in the CharSequence appended to + * the Writer. + * @param end + * The index of the char after the last one in the CharSequence + * appended to the Writer. + * @return The Writer. + * @throws IndexOutOfBoundsException + * If start is less than end, end is greater than the length of + * the CharSequence, or start or end is negative. + * @throws IOException + * If any IOException raises during the procedure. + */ + public Writer append(CharSequence csq, int start, int end) + throws IOException { + if (null == csq) { + write(TOKEN_NULL.substring(start, end)); + } else { + write(csq.subSequence(start, end).toString()); + } + return this; + } + } Index: /src/main/java/java/io/PrintStream.java =================================================================== --- /src/main/java/java/io/PrintStream.java (revision 388012) +++ /src/main/java/java/io/PrintStream.java (working copy) @@ -1,4 +1,4 @@ -/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable +/* 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. @@ -30,7 +30,8 @@ * a problem has been encountered in this Stream. * */ -public class PrintStream extends FilterOutputStream { +public class PrintStream extends FilterOutputStream implements Appendable, + Closeable { /** * protect writes to the underlying stream. @@ -53,6 +54,10 @@ private final String lineSeparator = (String) AccessController .doPrivileged(new PriviAction("line.separator")); //$NON-NLS-1$ + static final String TOKEN_NULL = "null"; //$NON-NLS-1$ + + // private Formatter formatter; + /** * Constructs a new PrintStream on the OutputStream out. All * writes to the target can now take place through this PrintStream. By @@ -116,6 +121,95 @@ } /** + * Constructs a new PrintStream on the file file. All writes + * to the target can now take place through this PrintStream. Its encoding + * character set is the default charset in the VM. + * + * @param file + * the file to provide convenience methods on. + * @throws FileNotFoundException + * if the file does not exist or cannot be opened to write. Or the file cannot be created or any problem when open the file to write. + * @throws SecurityException + * if the security manager exists and denies the write to the + * file. + */ + public PrintStream(File file) throws FileNotFoundException { + super(new FileOutputStream(file)); + } + + /** + * Constructs a new PrintStream on the file file. All writes + * to the target can now take place through this PrintStream. Its encoding + * character set name is csn. + * + * @param file + * the file to provide convenience methods on. + * @param csn + * the character set name + * @throws FileNotFoundException + * if the file does not exist or cannot be opened to write. Or + * the file cannot be created or any problem when open the file + * to write. + * @throws SecurityException + * if the security manager exists and denies the write to the + * file. + * @throws UnsupportedEncodingException + * if the chosen character set is not supported + */ + public PrintStream(File file, String csn) throws FileNotFoundException, + UnsupportedEncodingException { + super(new FileOutputStream(file)); + if (csn == null) + throw new NullPointerException(); + if (!Charset.isSupported(csn)) + throw new UnsupportedEncodingException(); + encoding = csn; + } + + /** + * Constructs a new PrintStream on the file the name of which isfileName. + * All writes to the target can now take place through this PrintStream. Its + * encoding character set is the default charset in the VM. + * + * @param file + * the file to provide convenience methods on. + * @throws FileNotFoundException + * if the file does not exist or cannot be opened to write. Or + * the file cannot be created or any problem when open the file + * to write. + * @throws SecurityException + * if the security manager exists and denies the write to the + * file. + */ + public PrintStream(String fileName) throws FileNotFoundException { + this(new File(fileName)); + } + + /** + * Constructs a new PrintStream on the file the name of which isfileName. + * All writes to the target can now take place through this PrintStream. Its + * encoding character set name is csn. + * + * @param file + * the file to provide convenience methods on. + * @param csn + * the character set name + * @throws FileNotFoundException + * if the file does not exist or cannot be opened to write. Or + * the file cannot be created or any problem when open the file + * to write. + * @throws SecurityException + * if the security manager exists and denies the write to the + * file. + * @throws UnsupportedEncodingException + * if the chosen character set is not supported + */ + public PrintStream(String fileName, String csn) + throws FileNotFoundException, UnsupportedEncodingException { + this(new File(fileName), csn); + } + + /** * Answers a boolean indicating whether or not this PrintStream has * encountered an error. If so, the receiver should probably be closed since * futher writes will not actually take place. A side effect of calling @@ -483,4 +577,69 @@ } } } + + /** + * Append a char c to the PrintStream. The + * PrintStream.append(c) works the same way as + * PrintStream.print(c). + * + * @param c + * The character appended to the PrintStream. + * @return The PrintStream. + */ + public PrintStream append(char c) { + print(c); + return this; + } + + /** + * Append a CharSequence csq to the PrintStream. The + * PrintStream.append(csq) works the same way as + * PrintStream.print(csq.toString()). If csq + * is null, then a CharSequence just contains then "null" will be + * substituted for csq. + * + * @param csq + * The CharSequence appended to the PrintStream. + * @return The PrintStream. + */ + public PrintStream append(CharSequence csq) { + if (null == csq) { + print(TOKEN_NULL); + } else { + print(csq.toString()); + } + return this; + } + + /** + * Append a subsequence of a CharSequence csq to the + * PrintStream. The first char and the last char of the subsequnce is + * specified by the parameter start and end. + * The PrintStream.append(csq) works the same way as + * PrintStream.print (csqcsq.subSequence(start,end).toString).If + * csq is null, then "null" will be substituted for + * csq. + * + * @param csq + * The CharSequence appended to the PrintStream. + * @param start + * The index of the first char in the CharSequence appended to + * the PrintStream. + * @param end + * The index of the char after the last one in the CharSequence + * appended to the PrintStream. + * @return The PrintStream. + * @throws IndexOutOfBoundsException + * If start is less than end, end is greater than the length of + * the CharSequence, or start or end is negative. + */ + public PrintStream append(CharSequence csq, int start, int end) { + if (null == csq) { + print(TOKEN_NULL.substring(start, end)); + } else { + print(csq.subSequence(start, end).toString()); + } + return this; + } } Index: /src/main/java/java/io/PrintWriter.java =================================================================== --- /src/main/java/java/io/PrintWriter.java (revision 388012) +++ /src/main/java/java/io/PrintWriter.java (working copy) @@ -1,4 +1,4 @@ -/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable +/* 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. @@ -474,4 +474,71 @@ public void write(String str, int offset, int count) { 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. + */ + 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 subsituted for csq + * + * @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 csq to the + * PrintWriter. The first char and the last char of the subsequnce 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. + */ + 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; + } } Index: /src/main/java/java/io/StringWriter.java =================================================================== --- /src/main/java/java/io/StringWriter.java (revision 388012) +++ /src/main/java/java/io/StringWriter.java (working copy) @@ -1,4 +1,4 @@ -/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable +/* 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. @@ -174,4 +174,71 @@ buf.append(sub); } } + + /** + * Append a char cto the StringWriter. The + * StringWriter.append(c) works the same way as + * StringWriter.write(c). + * + * @override Writer.append + * @param c + * The character appended to the StringWriter. + * @return The StringWriter. + */ + public StringWriter append(char c) { + write(c); + return this; + } + + /** + * Append a CharSequence csq to the StringWriter. The + * StringWriter.append(csq) works the same way as + * StringWriter.write(csq.toString()). If csq + * is null, then "null" will be substituted for csq. + * + * @override Writer.append + * @param csq + * The CharSequence appended to the StringWriter. + * @return The StringWriter + */ + public StringWriter 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 + * StringWriter. The first char and the last char of the subsequnce is + * specified by the parameter start and end. + * The StringWriter.append(csq) works the same way as + * StringWriter.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 StringWriter. + * @param start + * The index of the first char in the CharSequence appended to + * the StringWriter. + * @param end + * The index of the char after the last one in the CharSequence + * appended to the StringWriter. + * @return The StringWriter. + * @throws IndexOutOfBoundsException + * If start is less than end, end is greater than the length of + * the CharSequence, or start or end is negative. + */ + public StringWriter 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; + } } Index: /src/main/java/java/io/CharArrayWriter.java =================================================================== --- /src/main/java/java/io/CharArrayWriter.java (revision 388012) +++ /src/main/java/java/io/CharArrayWriter.java (working copy) @@ -1,4 +1,4 @@ -/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable +/* 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. @@ -23,6 +23,7 @@ * */ public class CharArrayWriter extends Writer { + /** * Buffer for characters */ @@ -224,4 +225,74 @@ out.write(buf, 0, count); } } + + + /** + * Append a char cto the CharArrayWriter. The + * CharArrayWriter.append(c) works the same way as + * CharArrayWriter.write(c). + * + * @override Writer.append + * @param c + * The character appended to the CharArrayWriter. + * @return The CharArrayWriter. + */ + public CharArrayWriter append(char c) { + write(c); + return this; + } + + /** + * Append a CharSequence csq to the CharArrayWriter. The + * CharArrayWriter.append(csq) works the same way as + * CharArrayWriter.write(csq.toString()). If + * csq is null, then then "null" will be substituted for + * csq. + * + * @override Writer.append + * @param csq + * The CharSequence appended to the CharArrayWriter. + * @return The CharArrayWriter + */ + public CharArrayWriter 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 + * CharArrayWriter. The first char and the last char of the subsequnce is + * specified by the parameter start and end. + * The CharArrayWriter.append(csq) works the same way as + * CharArrayWriter.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 CharArrayWriter. + * @param start + * The index of the first char in the CharSequence appended to + * the CharArrayWriter. + * @param end + * The index of the char after the last one in the CharSequence + * appended to the CharArrayWriter. + * @return The CharArrayWriter. + * @throws IndexOutOfBoundsException + * If start is less than end, end is greater than the length of + * the CharSequence, or start or end is negative. + */ + public CharArrayWriter 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; + } + }