You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/08/09 00:06:03 UTC
svn commit: r429848 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io:
PushbackReader.java Writer.java
Author: tellison
Date: Tue Aug 8 15:06:02 2006
New Revision: 429848
URL: http://svn.apache.org/viewvc?rev=429848&view=rev
Log:
Code tidy-up and fix minor compiler warnings.
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Writer.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java?rev=429848&r1=429847&r2=429848&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java Tue Aug 8 15:06:02 2006
@@ -15,6 +15,7 @@
package java.io;
+import org.apache.harmony.luni.util.Msg;
/**
* PushbackReader is a filter class which allows chars read to be pushed back
@@ -25,326 +26,339 @@
*
*/
public class PushbackReader extends FilterReader {
- /**
- * The <code>char</code> array containing the chars to read.
- */
- char[] buf;
-
- /**
- * The current position within the char array <code>buf</code>. A value
- * equal to buf.length indicates no chars available. A value of 0 indicates
- * the buffer is full.
- */
- int pos;
-
- /**
- * Constructs a new PushbackReader on the Reader <code>in</code>. The
- * size of the pushback buffer is set to the default, or 1 character.
- *
- * @param in
- * the Reader to allow pushback operations on.
- *
- */
- public PushbackReader(Reader in) {
- super(in);
- buf = new char[1];
- pos = 1;
- }
-
- /**
- * Constructs a new PushbackReader on the Reader <code>in</code>. The
- * size of the pushback buffer is set to <code>size</code> characters.
- *
- * @param in
- * the Reader to allow pushback operations on.
- * @param size
- * the size of the pushback buffer (<code>size>=0</code>) in
- * characters.
- */
- public PushbackReader(Reader in, int size) {
- super(in);
- if (size > 0) {
- buf = new char[size];
- pos = size;
- } else
- throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
- .getString("K0058")); //$NON-NLS-1$
- }
-
- /**
- * Close this PushbackReader. This implementation closes this reader,
- * releases the buffer used to pushback characters, and closes the target
- * reader.
- *
- * @throws IOException
- * If an error occurs attempting to close this Reader.
- */
- public void close() throws IOException {
- synchronized (lock) {
- buf = null;
- in.close();
- }
- }
-
- /**
- * Mark this PushbackReader. Since mark is not supported, this method will
- * always throw IOException.
- *
- * @param readAheadLimit
- * ignored, this method always throws IOException.
- *
- * @throws IOException
- * Since mark is not supported byt PushbackReader.
- */
- public void mark(int readAheadLimit) throws IOException {
- throw new IOException(org.apache.harmony.luni.util.Msg.getString("K007f")); //$NON-NLS-1$
- }
-
- /**
- * Answers a boolean indicating whether or not this PushbackReader supports
- * mark() and reset(). This implementation always answers false since
- * PushbackReaders do not support mark/reset.
- *
- * @return boolean indicates whether or not mark() and reset() are
- * supported.
- */
- public boolean markSupported() {
- return false;
- }
-
- /**
- * Reads a single character from this PushbackReader and returns the result
- * as an int. The 2 lowest-order bytes are returned or -1 of the end of
- * stream was encountered. If the pushback buffer does not contain any
- * available chars then a char from the target input reader is returned.
- *
- * @return int The char read or -1 if end of stream.
- *
- * @throws IOException
- * If an IOException occurs.
- */
- public int read() throws IOException {
- synchronized (lock) {
- if (buf != null) {
- /* Is there a pushback character available? */
- if (pos < buf.length) {
- return buf[pos++];
- }
- /**
- * Assume read() in the InputStream will return 2 lowest-order
- * bytes or -1 if end of stream.
- */
- return in.read();
- }
- throw new IOException();
- }
- }
-
- /**
- * Reads at most <code>count</code> chars from this PushbackReader and
- * stores them in char array <code>buffer</code> starting at
- * <code>offset</code>. Answer the number of chars actually read or -1 if
- * no chars were read and end of stream was encountered. This implementation
- * reads chars from the pushback buffer first, then the target stream if
- * more chars are required to satisfy <code>count</code>.
- *
- * @param buffer
- * the char array in which to store the read chars.
- * @param offset
- * the offset in <code>buffer</code> to store the read chars.
- * @param count
- * the maximum number of chars to store in <code>buffer</code>.
- * @return the number of chars actually read or -1 if end of stream.
- *
- * @throws IOException
- * If an IOException occurs.
- */
- public int read(char[] buffer, int offset, int count) throws IOException {
- // avoid int overflow
- if (0 <= offset && offset <= buffer.length && 0 <= count
- && count <= buffer.length - offset) {
- synchronized (lock) {
- if (buf != null) {
- int copiedChars = 0, copyLength = 0, newOffset = offset;
- /* Are there pushback chars available? */
- if (pos < buf.length) {
- copyLength = (buf.length - pos >= count) ? count
- : buf.length - pos;
- System.arraycopy(buf, pos, buffer, newOffset,
- copyLength);
- newOffset += copyLength;
- copiedChars += copyLength;
- /* Use up the chars in the local buffer */
- pos += copyLength;
- }
- /* Have we copied enough? */
- if (copyLength == count) {
- return count;
- }
- int inCopied = in.read(buffer, newOffset, count
- - copiedChars);
- if (inCopied > 0) {
- return inCopied + copiedChars;
- }
- if (copiedChars == 0) {
- return inCopied;
- }
- return copiedChars;
- }
- throw new IOException();
- }
- }
- throw new ArrayIndexOutOfBoundsException();
- }
-
- /**
- * Answers a <code>boolean</code> indicating whether or not this
- * PushbackReader is ready to be read without blocking. If the result is
- * <code>true</code>, the next <code>read()</code> will not block. If
- * the result is <code>false</code> this Reader may or may not block when
- * <code>read()</code> is sent.
- *
- * @return boolean <code>true</code> if the receiver will not block when
- * <code>read()</code> is called, <code>false</code> if unknown
- * or blocking will occur.
- *
- * @throws IOException
- * If the Reader is already closed or some other IO error
- * occurs.
- */
- public boolean ready() throws IOException {
- synchronized (lock) {
- if (buf != null)
- return (buf.length - pos > 0 || in.ready());
- throw new IOException(org.apache.harmony.luni.util.Msg.getString("K0080")); //$NON-NLS-1$
- }
- }
-
- /**
- * Resets this PushbackReader. Since mark is not supported, always throw
- * IOException.
- *
- * @throws IOException
- * Since mark is not supported.
- */
- public void reset() throws IOException {
- throw new IOException(org.apache.harmony.luni.util.Msg.getString("K007f")); //$NON-NLS-1$
- }
-
- /**
- * Push back all the chars in <code>buffer</code>. The chars 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 char array containing chars to push back into the reader.
- *
- * @throws IOException
- * If the pushback buffer becomes, or is, full.
- */
- public void unread(char[] buffer) throws IOException {
- unread(buffer, 0, buffer.length);
- }
-
- /**
- * Push back <code>count</code> number of chars in <code>buffer</code>
- * starting at <code>offset</code>. The chars are pushed so that they
- * would be read back buffer[offset], buffer[offset+1], etc. If the push
- * back buffer cannot handle the chars copied from <code>buffer</code>,
- * an IOException will be thrown. Some of the chars may already be in the
- * buffer after the exception is thrown.
- *
- * @param buffer
- * the char array containing chars to push back into the reader.
- * @param offset
- * the location to start taking chars to push back.
- * @param count
- * the number of chars to push back.
- *
- * @throws IOException
- * If the pushback buffer becomes, or is, full.
- */
- public void unread(char[] buffer, int offset, int count) throws IOException {
- if (count > pos)
- // Pushback buffer full
- throw new IOException(org.apache.harmony.luni.util.Msg.getString("K007e"));
- // avoid int overflow
- if (0 <= offset && offset <= buffer.length && 0 <= count
- && count <= buffer.length - offset) {
- synchronized (lock) {
- for (int i = offset + count - 1; i >= offset; i--)
- unread(buffer[i]);
- }
- } else
- throw new ArrayIndexOutOfBoundsException();
- }
-
- /**
- * Push back one <code>char</code>. Takes the char <code>oneChar</code>
- * and puts in in the local buffer of chars to read back before accessing
- * the target input stream.
- *
- * @param oneChar
- * the char to push back into the stream.
- *
- * @throws IOException
- * If the pushback buffer is already full.
- */
- public void unread(int oneChar) throws IOException {
- synchronized (lock) {
- if (buf != null) {
- if (pos != 0)
- buf[--pos] = (char) oneChar;
- else
- throw new IOException(org.apache.harmony.luni.util.Msg
- .getString("K007e")); //$NON-NLS-1$
- } else
- throw new IOException();
- }
- }
-
- /**
- * Skips <code>count</code> number of characters in this Reader.
- * Subsequent <code>read()</code>'s will not return these characters
- * unless <code>reset()</code> is used.
- *
- * @param count
- * the maximum number of characters to skip.
- * @return the number of characters actually skipped.
- *
- * @throws IOException
- * If the Reader is already closed or some other IO error
- * occurs.
- * @throws IllegalArgumentException
- * If count is negative.
- */
- public long skip(long count) throws IOException {
- if (count < 0) {
- throw new IllegalArgumentException();
- }
- synchronized (lock) {
- if (buf != null) {
- if (count == 0) {
- return 0;
- }
- long inSkipped;
- int availableFromBuffer = buf.length - pos;
- if (availableFromBuffer > 0) {
- long requiredFromIn = count - availableFromBuffer;
- if (requiredFromIn <= 0) {
- pos += count;
- return count;
- }
- pos += availableFromBuffer;
- inSkipped = in.skip(requiredFromIn);
- } else {
- inSkipped = in.skip(count);
- }
- return inSkipped + availableFromBuffer;
- }
- throw new IOException();
- }
- }
+ /**
+ * The <code>char</code> array containing the chars to read.
+ */
+ char[] buf;
+
+ /**
+ * The current position within the char array <code>buf</code>. A value
+ * equal to buf.length indicates no chars available. A value of 0 indicates
+ * the buffer is full.
+ */
+ int pos;
+
+ /**
+ * Constructs a new PushbackReader on the Reader <code>in</code>. The
+ * size of the pushback buffer is set to the default, or 1 character.
+ *
+ * @param in
+ * the Reader to allow pushback operations on.
+ *
+ */
+ public PushbackReader(Reader in) {
+ super(in);
+ buf = new char[1];
+ pos = 1;
+ }
+
+ /**
+ * Constructs a new PushbackReader on the Reader <code>in</code>. The
+ * size of the pushback buffer is set to <code>size</code> characters.
+ *
+ * @param in
+ * the Reader to allow pushback operations on.
+ * @param size
+ * the size of the pushback buffer (<code>size>=0</code>) in
+ * characters.
+ */
+ public PushbackReader(Reader in, int size) {
+ super(in);
+ if (size > 0) {
+ buf = new char[size];
+ pos = size;
+ } else {
+ throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Close this PushbackReader. This implementation closes this reader,
+ * releases the buffer used to pushback characters, and closes the target
+ * reader.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this Reader.
+ */
+ @Override
+ public void close() throws IOException {
+ synchronized (lock) {
+ buf = null;
+ in.close();
+ }
+ }
+
+ /**
+ * Mark this PushbackReader. Since mark is not supported, this method will
+ * always throw IOException.
+ *
+ * @param readAheadLimit
+ * ignored, this method always throws IOException.
+ *
+ * @throws IOException
+ * Since mark is not supported byt PushbackReader.
+ */
+ @Override
+ public void mark(int readAheadLimit) throws IOException {
+ throw new IOException(Msg.getString("K007f")); //$NON-NLS-1$
+ }
+
+ /**
+ * Answers a boolean indicating whether or not this PushbackReader supports
+ * mark() and reset(). This implementation always answers false since
+ * PushbackReaders 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 character from this PushbackReader and returns the result
+ * as an int. The 2 lowest-order bytes are returned or -1 of the end of
+ * stream was encountered. If the pushback buffer does not contain any
+ * available chars then a char from the target input reader is returned.
+ *
+ * @return int The char read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If an IOException occurs.
+ */
+ @Override
+ public int read() throws IOException {
+ synchronized (lock) {
+ if (buf != null) {
+ /* Is there a pushback character available? */
+ if (pos < buf.length) {
+ return buf[pos++];
+ }
+ /**
+ * Assume read() in the InputStream will return 2 lowest-order
+ * bytes or -1 if end of stream.
+ */
+ return in.read();
+ }
+ throw new IOException();
+ }
+ }
+
+ /**
+ * Reads at most <code>count</code> chars from this PushbackReader and
+ * stores them in char array <code>buffer</code> starting at
+ * <code>offset</code>. Answer the number of chars actually read or -1 if
+ * no chars were read and end of stream was encountered. This implementation
+ * reads chars from the pushback buffer first, then the target stream if
+ * more chars are required to satisfy <code>count</code>.
+ *
+ * @param buffer
+ * the char array in which to store the read chars.
+ * @param offset
+ * the offset in <code>buffer</code> to store the read chars.
+ * @param count
+ * the maximum number of chars to store in <code>buffer</code>.
+ * @return the number of chars actually read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If an IOException occurs.
+ */
+ @Override
+ public int read(char[] buffer, int offset, int count) throws IOException {
+ // avoid int overflow
+ if (0 <= offset && offset <= buffer.length && 0 <= count
+ && count <= buffer.length - offset) {
+ synchronized (lock) {
+ if (buf != null) {
+ int copiedChars = 0, copyLength = 0, newOffset = offset;
+ /* Are there pushback chars available? */
+ if (pos < buf.length) {
+ copyLength = (buf.length - pos >= count) ? count
+ : buf.length - pos;
+ System.arraycopy(buf, pos, buffer, newOffset,
+ copyLength);
+ newOffset += copyLength;
+ copiedChars += copyLength;
+ /* Use up the chars in the local buffer */
+ pos += copyLength;
+ }
+ /* Have we copied enough? */
+ if (copyLength == count) {
+ return count;
+ }
+ int inCopied = in.read(buffer, newOffset, count
+ - copiedChars);
+ if (inCopied > 0) {
+ return inCopied + copiedChars;
+ }
+ if (copiedChars == 0) {
+ return inCopied;
+ }
+ return copiedChars;
+ }
+ throw new IOException();
+ }
+ }
+ throw new ArrayIndexOutOfBoundsException();
+ }
+
+ /**
+ * Answers a <code>boolean</code> indicating whether or not this
+ * PushbackReader is ready to be read without blocking. If the result is
+ * <code>true</code>, the next <code>read()</code> will not block. If
+ * the result is <code>false</code> this Reader may or may not block when
+ * <code>read()</code> is sent.
+ *
+ * @return boolean <code>true</code> if the receiver will not block when
+ * <code>read()</code> is called, <code>false</code> if unknown
+ * or blocking will occur.
+ *
+ * @throws IOException
+ * If the Reader is already closed or some other IO error
+ * occurs.
+ */
+ @Override
+ public boolean ready() throws IOException {
+ synchronized (lock) {
+ if (buf != null) {
+ return (buf.length - pos > 0 || in.ready());
+ }
+ throw new IOException(Msg.getString("K0080")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Resets this PushbackReader. Since mark is not supported, always throw
+ * IOException.
+ *
+ * @throws IOException
+ * Since mark is not supported.
+ */
+ @Override
+ public void reset() throws IOException {
+ throw new IOException(Msg.getString("K007f")); //$NON-NLS-1$
+ }
+
+ /**
+ * Push back all the chars in <code>buffer</code>. The chars 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 char array containing chars to push back into the reader.
+ *
+ * @throws IOException
+ * If the pushback buffer becomes, or is, full.
+ */
+ public void unread(char[] buffer) throws IOException {
+ unread(buffer, 0, buffer.length);
+ }
+
+ /**
+ * Push back <code>count</code> number of chars in <code>buffer</code>
+ * starting at <code>offset</code>. The chars are pushed so that they
+ * would be read back buffer[offset], buffer[offset+1], etc. If the push
+ * back buffer cannot handle the chars copied from <code>buffer</code>,
+ * an IOException will be thrown. Some of the chars may already be in the
+ * buffer after the exception is thrown.
+ *
+ * @param buffer
+ * the char array containing chars to push back into the reader.
+ * @param offset
+ * the location to start taking chars to push back.
+ * @param count
+ * the number of chars to push back.
+ *
+ * @throws IOException
+ * If the pushback buffer becomes, or is, full.
+ */
+ public void unread(char[] buffer, int offset, int count) throws IOException {
+ if (count > pos) {
+ // Pushback buffer full
+ throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
+ }
+ // avoid int overflow
+ if (0 <= offset && offset <= buffer.length && 0 <= count
+ && count <= buffer.length - offset) {
+ synchronized (lock) {
+ for (int i = offset + count - 1; i >= offset; i--) {
+ unread(buffer[i]);
+ }
+ }
+ } else {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ }
+
+ /**
+ * Push back one <code>char</code>. Takes the char <code>oneChar</code>
+ * and puts in in the local buffer of chars to read back before accessing
+ * the target input stream.
+ *
+ * @param oneChar
+ * the char to push back into the stream.
+ *
+ * @throws IOException
+ * If the pushback buffer is already full.
+ */
+ public void unread(int oneChar) throws IOException {
+ synchronized (lock) {
+ if (buf != null) {
+ if (pos != 0) {
+ buf[--pos] = (char) oneChar;
+ } else {
+ throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
+ }
+ } else {
+ throw new IOException();
+ }
+ }
+ }
+
+ /**
+ * Skips <code>count</code> number of characters in this Reader.
+ * Subsequent <code>read()</code>'s will not return these characters
+ * unless <code>reset()</code> is used.
+ *
+ * @param count
+ * the maximum number of characters to skip.
+ * @return the number of characters actually skipped.
+ *
+ * @throws IOException
+ * If the Reader is already closed or some other IO error
+ * occurs.
+ * @throws IllegalArgumentException
+ * If count is negative.
+ */
+ @Override
+ public long skip(long count) throws IOException {
+ if (count < 0) {
+ throw new IllegalArgumentException();
+ }
+ synchronized (lock) {
+ if (buf != null) {
+ if (count == 0) {
+ return 0;
+ }
+ long inSkipped;
+ int availableFromBuffer = buf.length - pos;
+ if (availableFromBuffer > 0) {
+ long requiredFromIn = count - availableFromBuffer;
+ if (requiredFromIn <= 0) {
+ pos += count;
+ return count;
+ }
+ pos += availableFromBuffer;
+ inSkipped = in.skip(requiredFromIn);
+ } else {
+ inSkipped = in.skip(count);
+ }
+ return inSkipped + availableFromBuffer;
+ }
+ throw new IOException();
+ }
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Writer.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Writer.java?rev=429848&r1=429847&r2=429848&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Writer.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/Writer.java Tue Aug 8 15:06:02 2006
@@ -15,7 +15,6 @@
package java.io;
-
/**
* Writer is an Abstract class for writing Character Streams. Subclasses of
* writer must implement the methods <code>write(char[], int, int)</code>,
@@ -24,217 +23,219 @@
* @see Reader
*/
public abstract class Writer implements Appendable, Closeable, Flushable {
-
- static final String TOKEN_NULL = "null";
-
- /** The object used to synchronize access to the writer. */
- protected Object lock;
-
- /**
- * Constructs a new character stream Writer using <code>this</code> as the
- * Object to synchronize critical regions around.
- *
- */
- protected Writer() {
- super();
- lock = this;
- }
-
- /**
- * Constructs a new character stream Writer using <code>lock</code> as the
- * Object to synchronize critical regions around.
- *
- * @param lock
- * the Object to synchronize critical regions around.
- */
- protected Writer(Object lock) {
- if (lock != null)
- this.lock = lock;
- else
- throw new NullPointerException();
- }
-
- /**
- * Close this Writer. This must be implemented by any concrete subclasses.
- * The implementation should free any resources associated with the Writer.
- *
- * @throws IOException
- * If an error occurs attempting to close this Writer.
- */
- public abstract void close() throws IOException;
-
- /**
- * Flush this Writer. This must be implemented by any concrete subclasses.
- * The implementation should ensure all buffered characters are written out.
- *
- * @throws IOException
- * If an error occurs attempting to flush this Writer.
- */
- public abstract void flush() throws IOException;
-
- /**
- * Writes the entire character buffer <code>buf</code> to this Writer.
- *
- * @param buf
- * the non-null array containing characters to write.
- *
- * @throws IOException
- * If this Writer has already been closed or some other
- * IOException occurs.
- */
- public void write(char buf[]) throws IOException {
- write(buf, 0, buf.length);
- }
-
- /**
- * Writes <code>count</code> characters starting at <code>offset<code> in
- * <code>buf</code> to this Writer. This abstract method must be implemented
- * by concrete subclasses.
- *
- * @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 IOException If this Writer has already been closed or some other IOException occurs.
- * @throws ArrayIndexOutOfBoundsException If offset or count are outside of bounds.
- */
- public abstract void write(char buf[], int offset, int count)
- throws IOException;
-
- /**
- * Writes the specified character <code>oneChar</code> to this Writer.
- * This implementation writes the low order two bytes of
- * <code>oneChar</code> to the Stream.
- *
- * @param oneChar
- * The character to write
- *
- * @throws IOException
- * If this Writer has already been closed or some other
- * IOException occurs.
- */
- public void write(int oneChar) throws IOException {
- synchronized (lock) {
- char oneCharArray[] = new char[1];
- oneCharArray[0] = (char) oneChar;
- write(oneCharArray);
- }
- }
-
- /**
- * Writes the characters from the String <code>str</code> to this Writer.
- *
- * @param str
- * the non-null String containing the characters to write.
- *
- * @throws IOException
- * If this Writer has already been closed or some other
- * IOException occurs.
- */
- public void write(String str) throws IOException {
- char buf[] = new char[str.length()];
- str.getChars(0, buf.length, buf, 0);
- synchronized (lock) {
- write(buf);
- }
- }
-
- /**
- * Writes <code>count</code> number of characters starting at
- * <code>offset</code> from the String <code>str</code> to this Writer.
- *
- * @param str
- * the non-null String containing the characters to write.
- * @param offset
- * the starting point to retrieve characters.
- * @param count
- * the number of characters to retrieve and write.
- *
- * @throws IOException
- * If this Writer has already been closed or some other
- * IOException occurs.
- * @throws ArrayIndexOutOfBoundsException
- * If offset or count are outside of bounds.
- */
- public void write(String str, int offset, int count) throws IOException {
- if (count >= 0) { // other cases tested by getChars()
- char buf[] = new char[count];
- str.getChars(offset, offset + count, buf, 0);
-
- synchronized (lock) {
- write(buf);
- }
- } else
- throw new StringIndexOutOfBoundsException();
- }
-
- /**
- * Append a char <code>c</code>to the Writer. The Writer.append(<code>c</code>)
- * works the same as Writer.write(<code>c</code>).
- *
- * @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 <code>csq</code> to the Writer. The
- * Writer.append(<code>csq</code>) works the same way as Writer.write(<code>csq</code>.toString()).
- * If <code>csq</code> is null, then "null" will be substituted for
- * <code>csq</code>.
- *
- * @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 <code>csq</code> to the Writer.
- * The first char and the last char of the subsequnce is specified by the
- * parameter <code>start</code> and <code>end</code>. The
- * Writer.append(<code>csq</code>) works the same way as Writer.write (<code>csq</code>csq.subSequence(<code>start</code>,<code>end</code>).toString).
- * If <code>csq</code> is null, then "null" will be substituted for
- * <code>csq</code>.
- *
- * @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;
- }
+
+ static final String TOKEN_NULL = "null"; //$NON-NLS-1$
+
+ /** The object used to synchronize access to the writer. */
+ protected Object lock;
+
+ /**
+ * Constructs a new character stream Writer using <code>this</code> as the
+ * Object to synchronize critical regions around.
+ *
+ */
+ protected Writer() {
+ super();
+ lock = this;
+ }
+
+ /**
+ * Constructs a new character stream Writer using <code>lock</code> as the
+ * Object to synchronize critical regions around.
+ *
+ * @param lock
+ * the Object to synchronize critical regions around.
+ */
+ protected Writer(Object lock) {
+ if (lock != null) {
+ this.lock = lock;
+ } else {
+ throw new NullPointerException();
+ }
+ }
+
+ /**
+ * Close this Writer. This must be implemented by any concrete subclasses.
+ * The implementation should free any resources associated with the Writer.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this Writer.
+ */
+ public abstract void close() throws IOException;
+
+ /**
+ * Flush this Writer. This must be implemented by any concrete subclasses.
+ * The implementation should ensure all buffered characters are written out.
+ *
+ * @throws IOException
+ * If an error occurs attempting to flush this Writer.
+ */
+ public abstract void flush() throws IOException;
+
+ /**
+ * Writes the entire character buffer <code>buf</code> to this Writer.
+ *
+ * @param buf
+ * the non-null array containing characters to write.
+ *
+ * @throws IOException
+ * If this Writer has already been closed or some other
+ * IOException occurs.
+ */
+ public void write(char buf[]) throws IOException {
+ write(buf, 0, buf.length);
+ }
+
+ /**
+ * Writes <code>count</code> characters starting at <code>offset<code> in
+ * <code>buf</code> to this Writer. This abstract method must be implemented
+ * by concrete subclasses.
+ *
+ * @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 IOException If this Writer has already been closed or some other IOException occurs.
+ * @throws ArrayIndexOutOfBoundsException If offset or count are outside of bounds.
+ */
+ public abstract void write(char buf[], int offset, int count)
+ throws IOException;
+
+ /**
+ * Writes the specified character <code>oneChar</code> to this Writer.
+ * This implementation writes the low order two bytes of
+ * <code>oneChar</code> to the Stream.
+ *
+ * @param oneChar
+ * The character to write
+ *
+ * @throws IOException
+ * If this Writer has already been closed or some other
+ * IOException occurs.
+ */
+ public void write(int oneChar) throws IOException {
+ synchronized (lock) {
+ char oneCharArray[] = new char[1];
+ oneCharArray[0] = (char) oneChar;
+ write(oneCharArray);
+ }
+ }
+
+ /**
+ * Writes the characters from the String <code>str</code> to this Writer.
+ *
+ * @param str
+ * the non-null String containing the characters to write.
+ *
+ * @throws IOException
+ * If this Writer has already been closed or some other
+ * IOException occurs.
+ */
+ public void write(String str) throws IOException {
+ char buf[] = new char[str.length()];
+ str.getChars(0, buf.length, buf, 0);
+ synchronized (lock) {
+ write(buf);
+ }
+ }
+
+ /**
+ * Writes <code>count</code> number of characters starting at
+ * <code>offset</code> from the String <code>str</code> to this Writer.
+ *
+ * @param str
+ * the non-null String containing the characters to write.
+ * @param offset
+ * the starting point to retrieve characters.
+ * @param count
+ * the number of characters to retrieve and write.
+ *
+ * @throws IOException
+ * If this Writer has already been closed or some other
+ * IOException occurs.
+ * @throws ArrayIndexOutOfBoundsException
+ * If offset or count are outside of bounds.
+ */
+ public void write(String str, int offset, int count) throws IOException {
+ if (count >= 0) { // other cases tested by getChars()
+ char buf[] = new char[count];
+ str.getChars(offset, offset + count, buf, 0);
+
+ synchronized (lock) {
+ write(buf);
+ }
+ } else {
+ throw new StringIndexOutOfBoundsException();
+ }
+ }
+
+ /**
+ * Append a char <code>c</code>to the Writer. The Writer.append(<code>c</code>)
+ * works the same as Writer.write(<code>c</code>).
+ *
+ * @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 <code>csq</code> to the Writer. The
+ * Writer.append(<code>csq</code>) works the same way as Writer.write(<code>csq</code>.toString()).
+ * If <code>csq</code> is null, then "null" will be substituted for
+ * <code>csq</code>.
+ *
+ * @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 <code>csq</code> to the Writer.
+ * The first char and the last char of the subsequnce is specified by the
+ * parameter <code>start</code> and <code>end</code>. The
+ * Writer.append(<code>csq</code>) works the same way as Writer.write (<code>csq</code>csq.subSequence(<code>start</code>,<code>end</code>).toString).
+ * If <code>csq</code> is null, then "null" will be substituted for
+ * <code>csq</code>.
+ *
+ * @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;
+ }
}