harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndbe...@apache.org
Subject svn commit: r431830 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io: BufferedOutputStream.java BufferedReader.java BufferedWriter.java
Date Wed, 16 Aug 2006 05:38:48 GMT
Author: ndbeyer
Date: Tue Aug 15 22:38:48 2006
New Revision: 431830

URL: http://svn.apache.org/viewvc?rev=431830&view=rev
Log:
Cleanup source, remove compiler warnings and set eol-style property.

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java   (contents, props changed)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java   (contents, props changed)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java   (contents, props changed)

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java?rev=431830&r1=431829&r2=431830&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java Tue Aug 15 22:38:48 2006
@@ -1,174 +1,180 @@
-/* Copyright 1998, 2004 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;
-
-
-/**
- * BufferedOutputStream is a class which takes an output stream and
- * <em>buffers</em> the writes to that stream. In this way, costly interaction
- * with the original output stream can be minimized by writing buffered amounts
- * of data infrequently. The drawback is that extra space is required to hold
- * the buffer and copying takes place when writing that buffer.
- * 
- * @see BufferedInputStream
- */
-public class BufferedOutputStream extends FilterOutputStream {
-	/**
-	 * The buffer containing the bytes to be written to the target OutputStream.
-	 */
-	protected byte[] buf;
-
-	/**
-	 * The total number of bytes inside the byte array <code>buf</code>.
-	 */
-	protected int count;
-
-	/**
-	 * Constructs a new BufferedOutputStream on the OutputStream
-	 * <code>out</code>. The default buffer size (512 bytes) is allocated and
-	 * all writes are now filtered through this stream.
-	 * 
-	 * @param out
-	 *            the OutputStream to buffer writes on.
-	 * 
-	 */
-	public BufferedOutputStream(OutputStream out) {
-		super(out);
-		buf = new byte[512];
-	}
-
-	/**
-	 * Constructs a new BufferedOutputStream on the OutputStream
-	 * <code>out</code>. The buffer size is set to <code>size</code> and
-	 * all writes are now filtered through this stream.
-	 * 
-     * @param out
-	 *            the OutputStream to buffer writes on.
-	 * @param size
-	 *            the size of the buffer in bytes.
-	 * 
-	 */
-	public BufferedOutputStream(OutputStream out, int size) {
-		super(out);
-		if (size > 0)
-			buf = new byte[size];
-		else
-			throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
-					.getString("K0058")); //$NON-NLS-1$
-	}
-
-	/**
-	 * Flush this BufferedOutputStream to ensure all pending data is written out
-	 * to the target OutputStream. In addition, the target stream is also
-	 * flushed.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to flush this
-	 *             BufferedOutputStream.
-	 */
-	public synchronized void flush() throws IOException {
-		if (count > 0)
-			out.write(buf, 0, count);
-		count = 0;
-		out.flush();
-	}
-
-	/**
-	 * Writes <code>count</code> <code>bytes</code> from the byte array
-	 * <code>buffer</code> starting at <code>offset</code> to this
-	 * BufferedOutputStream. If there is room in the buffer to hold the bytes,
-	 * they are copied in. If not, the buffered bytes plus the bytes in
-	 * <code>buffer</code> are written to the target stream, the target is
-	 * flushed, and the buffer is cleared.
-	 * 
-	 * @param buffer
-	 *            the buffer to be written
-	 * @param offset
-	 *            offset in buffer to get bytes
-	 * @param length
-	 *            number of bytes in buffer to write
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to write to this
-	 *             BufferedOutputStream.
-	 * @throws NullPointerException
-	 *             If buffer is null.
-	 * @throws IndexOutOfBoundsException
-	 *             If offset or count are outside of bounds.
-	 */
-	public synchronized void write(byte[] buffer, int offset, int length)
-			throws IOException {
-		if (buffer != null) {
-			// avoid int overflow
-			if (0 <= offset && offset <= buffer.length && 0 <= length
-					&& length <= buffer.length - offset) {
-				if (count == 0 && length >= buf.length) {
-					out.write(buffer, offset, length);
-					return;
-				}
-				int available = buf.length - count;
-				if (length < available)
-					available = length;
-				if (available > 0) {
-					System.arraycopy(buffer, offset, buf, count, available);
-					count += available;
-				}
-				if (count == buf.length) {
-					out.write(buf, 0, buf.length);
-					count = 0;
-					if (length > available) {
-						offset += available;
-						available = length - available;
-						if (available >= buf.length) {
-							out.write(buffer, offset, available);
-						} else {
-							System.arraycopy(buffer, offset, buf, count,
-									available);
-							count += available;
-						}
-					}
-				}
-			} else
-				throw new ArrayIndexOutOfBoundsException(org.apache.harmony.luni.util.Msg
-						.getString("K002f")); //$NON-NLS-1$
-		} else
-			throw new NullPointerException(org.apache.harmony.luni.util.Msg
-					.getString("K0047")); //$NON-NLS-1$
-	}
-
-	/**
-	 * Writes the specified byte <code>oneByte</code> to this
-	 * BufferedOutputStream. Only the low order byte of <code>oneByte</code>
-	 * is written. If there is room in the buffer, the byte is copied in and the
-	 * count incremented. Otherwise, the buffer plus <code>oneByte</code> are
-	 * written to the target stream, the target is flushed, and the buffer is
-	 * reset.
-	 * 
-	 * @param oneByte
-	 *            the byte to be written
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to write to this
-	 *             BufferedOutputStream.
-	 */
-	public synchronized void write(int oneByte) throws IOException {
-		if (count == buf.length) {
-			out.write(buf, 0, count);
-			count = 0;
-		}
-		buf[count++] = (byte) oneByte;
-	}
-}
+/* Copyright 1998, 2004 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;
+
+/**
+ * BufferedOutputStream is a class which takes an output stream and
+ * <em>buffers</em> the writes to that stream. In this way, costly interaction
+ * with the original output stream can be minimized by writing buffered amounts
+ * of data infrequently. The drawback is that extra space is required to hold
+ * the buffer and copying takes place when writing that buffer.
+ * 
+ * @see BufferedInputStream
+ */
+public class BufferedOutputStream extends FilterOutputStream {
+	/**
+	 * The buffer containing the bytes to be written to the target OutputStream.
+	 */
+	protected byte[] buf;
+
+	/**
+	 * The total number of bytes inside the byte array <code>buf</code>.
+	 */
+	protected int count;
+
+	/**
+	 * Constructs a new BufferedOutputStream on the OutputStream
+	 * <code>out</code>. The default buffer size (512 bytes) is allocated and
+	 * all writes are now filtered through this stream.
+	 * 
+	 * @param out
+	 *            the OutputStream to buffer writes on.
+	 * 
+	 */
+	public BufferedOutputStream(OutputStream out) {
+		super(out);
+		buf = new byte[512];
+	}
+
+	/**
+	 * Constructs a new BufferedOutputStream on the OutputStream
+	 * <code>out</code>. The buffer size is set to <code>size</code> and
+	 * all writes are now filtered through this stream.
+	 * 
+     * @param out
+	 *            the OutputStream to buffer writes on.
+	 * @param size
+	 *            the size of the buffer in bytes.
+	 * 
+	 */
+	public BufferedOutputStream(OutputStream out, int size) {
+		super(out);
+		if (size > 0) {
+            buf = new byte[size];
+        } else {
+            throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
+        }
+	}
+
+	/**
+	 * Flush this BufferedOutputStream to ensure all pending data is written out
+	 * to the target OutputStream. In addition, the target stream is also
+	 * flushed.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to flush this
+	 *             BufferedOutputStream.
+	 */
+	@Override
+    public synchronized void flush() throws IOException {
+		if (count > 0) {
+            out.write(buf, 0, count);
+        }
+		count = 0;
+		out.flush();
+	}
+
+	/**
+	 * Writes <code>count</code> <code>bytes</code> from the byte array
+	 * <code>buffer</code> starting at <code>offset</code> to this
+	 * BufferedOutputStream. If there is room in the buffer to hold the bytes,
+	 * they are copied in. If not, the buffered bytes plus the bytes in
+	 * <code>buffer</code> are written to the target stream, the target is
+	 * flushed, and the buffer is cleared.
+	 * 
+	 * @param buffer
+	 *            the buffer to be written
+	 * @param offset
+	 *            offset in buffer to get bytes
+	 * @param length
+	 *            number of bytes in buffer to write
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to write to this
+	 *             BufferedOutputStream.
+	 * @throws NullPointerException
+	 *             If buffer is null.
+	 * @throws IndexOutOfBoundsException
+	 *             If offset or count are outside of bounds.
+	 */
+	@Override
+    public synchronized void write(byte[] buffer, int offset, int length)
+			throws IOException {
+		if (buffer != null) {
+			// avoid int overflow
+			if (0 <= offset && offset <= buffer.length && 0 <= length
+					&& length <= buffer.length - offset) {
+				if (count == 0 && length >= buf.length) {
+					out.write(buffer, offset, length);
+					return;
+				}
+				int available = buf.length - count;
+				if (length < available) {
+                    available = length;
+                }
+				if (available > 0) {
+					System.arraycopy(buffer, offset, buf, count, available);
+					count += available;
+				}
+				if (count == buf.length) {
+					out.write(buf, 0, buf.length);
+					count = 0;
+					if (length > available) {
+						offset += available;
+						available = length - available;
+						if (available >= buf.length) {
+							out.write(buffer, offset, available);
+						} else {
+							System.arraycopy(buffer, offset, buf, count,
+									available);
+							count += available;
+						}
+					}
+				}
+			} else {
+                throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
+            }
+		} else {
+            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
+        }
+	}
+
+	/**
+	 * Writes the specified byte <code>oneByte</code> to this
+	 * BufferedOutputStream. Only the low order byte of <code>oneByte</code>
+	 * is written. If there is room in the buffer, the byte is copied in and the
+	 * count incremented. Otherwise, the buffer plus <code>oneByte</code> are
+	 * written to the target stream, the target is flushed, and the buffer is
+	 * reset.
+	 * 
+	 * @param oneByte
+	 *            the byte to be written
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to write to this
+	 *             BufferedOutputStream.
+	 */
+	@Override
+    public synchronized void write(int oneByte) throws IOException {
+		if (count == buf.length) {
+			out.write(buf, 0, count);
+			count = 0;
+		}
+		buf[count++] = (byte) oneByte;
+	}
+}

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java?rev=431830&r1=431829&r2=431830&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java Tue Aug 15 22:38:48 2006
@@ -1,443 +1,473 @@
-/* Copyright 1998, 2005 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;
-
-
-/**
- * BufferedReader is a buffered character input reader. Buffering allows reading
- * from character streams more efficiently. If the default size of the buffer is
- * not practical, another size may be specified. Reading a character from a
- * Reader class usually involves reading a character from its Stream or
- * subsequent Reader. It is advisable to wrap a BufferedReader around those
- * Readers whose read operations may have high latency. For example, the
- * following code
- * 
- * <pre>
- * BufferedReader inReader = new BufferedReader(new FileReader(&quot;file.java&quot;));
- * </pre>
- * 
- * will buffer input for the file <code>file.java</code>.
- * 
- * @see BufferedWriter
- * @since 1.1
- */
-
-public class BufferedReader extends Reader {
-	private Reader in;
-
-	private char[] buf;
-
-	private int marklimit = -1;
-
-	private int count;
-
-	private int markpos = -1;
-
-	private int pos;
-
-	/**
-	 * Constructs a new BufferedReader on the Reader <code>in</code>. The
-	 * default buffer size (8K) is allocated and all reads can now be filtered
-	 * through this BufferedReader.
-	 * 
-	 * @param in
-	 *            the Reader to buffer reads on.
-	 */
-
-	public BufferedReader(Reader in) {
-		super(in);
-		this.in = in;
-		buf = new char[8192];
-	}
-
-	/**
-	 * Constructs a new BufferedReader on the Reader <code>in</code>. The
-	 * buffer size is specified by the parameter <code>size</code> and all
-	 * reads can now be filtered through this BufferedReader.
-	 * 
-	 * @param in
-	 *            the Reader to buffer reads on.
-	 * @param size
-	 *            the size of buffer to allocate.
-	 */
-
-	public BufferedReader(Reader in, int size) {
-		super(in);
-		if (size > 0) {
-			this.in = in;
-			buf = new char[size];
-		} else
-			throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
-					.getString("K0058")); //$NON-NLS-1$
-	}
-
-	/**
-	 * Close the Reader. This implementation closes the Reader being filtered
-	 * and releases the buffer used by this reader. If this BufferedReader has
-	 * already been closed, nothing is done.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to close this BufferedReader.
-	 */
-
-	public void close() throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				in.close();
-				buf = null;
-			}
-		}
-	}
-
-	private int fillbuf() throws IOException {
-		if (markpos == -1 || (pos - markpos >= marklimit)) {
-			/* Mark position not set or exceeded readlimit */
-			int result = in.read(buf, 0, buf.length);
-			if (result > 0) {
-				markpos = -1;
-				pos = 0;
-				count = result == -1 ? 0 : result;
-			}
-			return result;
-		}
-		if (markpos == 0 && marklimit > buf.length) {
-			/* Increase buffer size to accommodate the readlimit */
-			int newLength = buf.length * 2;
-			if (newLength > marklimit)
-				newLength = marklimit;
-			char[] newbuf = new char[newLength];
-			System.arraycopy(buf, 0, newbuf, 0, buf.length);
-			buf = newbuf;
-		} else if (markpos > 0) {
-			System.arraycopy(buf, markpos, buf, 0, buf.length - markpos);
-		}
-
-		/* Set the new position and mark position */
-		pos -= markpos;
-		count = markpos = 0;
-		int charsread = in.read(buf, pos, buf.length - pos);
-		count = charsread == -1 ? pos : pos + charsread;
-		return charsread;
-	}
-
-	/**
-	 * Answer a boolean indicating whether or not this BufferedReader is open.
-	 * 
-	 * @return <code>true</code> if this reader is open, <code>false</code>
-	 *         otherwise
-	 */
-	private boolean isOpen() {
-		return buf != null;
-	}
-
-	/**
-	 * Set a Mark position in this BufferedReader. The parameter
-	 * <code>readLimit</code> indicates how many characters can be read before
-	 * a mark is invalidated. Sending reset() will reposition the reader back to
-	 * the marked position provided <code>readLimit</code> has not been
-	 * surpassed.
-	 * 
-	 * @param readlimit
-	 *            an int representing how many characters must be read
-	 *            before invalidating the mark.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting mark this BufferedReader.
-	 */
-
-	public void mark(int readlimit) throws IOException {
-		if (readlimit >= 0) {
-			synchronized (lock) {
-				if (isOpen()) {
-					marklimit = readlimit;
-					markpos = pos;
-				} else
-					throw new IOException(org.apache.harmony.luni.util.Msg
-							.getString("K005b")); //$NON-NLS-1$
-			}
-		} else
-			throw new IllegalArgumentException();
-	}
-
-	/**
-	 * Answers a boolean indicating whether or not this Reader supports mark()
-	 * and reset(). This implementation answers <code>true</code>.
-	 * 
-	 * @return <code>true</code> if mark() and reset() are supported,
-	 *         <code>false</code> otherwise
-	 */
-
-	public boolean markSupported() {
-		return true;
-	}
-
-	/**
-	 * Reads a single character from this reader and returns the result as an
-	 * int. The 2 higher-order characters are set to 0. If the end of reader was
-	 * encountered then return -1. This implementation either returns a
-	 * character from the buffer or if there are no characters available, fill
-	 * the buffer then return a character or -1.
-	 * 
-	 * @return the character read or -1 if end of reader.
-	 * 
-	 * @throws IOException
-	 *             If the BufferedReader is already closed or some other IO
-	 *             error occurs.
-	 */
-
-	public int read() throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				/* Are there buffered characters available? */
-				if (pos < count || fillbuf() != -1)
-					return buf[pos++];
-				return -1;
-			}
-			throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005b")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Reads at most <code>length</code> characters from this BufferedReader
-	 * and stores them at <code>offset</code> in the character array
-	 * <code>buffer</code>. Returns the number of characters actually read or
-	 * -1 if the end of reader was encountered. If all the buffered characters
-	 * have been used, a mark has not been set, and the requested number of
-	 * characters is larger than this Readers buffer size, this implementation
-	 * bypasses the buffer and simply places the results directly into
-	 * <code>buffer</code>.
-	 * 
-	 * @param buffer
-	 *            character array to store the read characters
-	 * @param offset
-	 *            offset in buf to store the read characters
-	 * @param length
-	 *            maximum number of characters to read
-	 * @return number of characters read or -1 if end of reader.
-	 * 
-	 * @throws IOException
-	 *             If the BufferedReader is already closed or some other IO
-	 *             error occurs.
-	 */
-
-	public int read(char[] buffer, int offset, int length) throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				// check for null
-				int bufLen = buffer.length;
-                if(offset < 0 || length < 0 || (long)offset + (long)length > bufLen){
-                    throw new ArrayIndexOutOfBoundsException();
-                }
-				if (length == 0)
-                    return 0;
-                int required;
-                if (pos < count) {
-                    /* There are bytes available in the buffer. */
-                    int copylength = count - pos >= length ? length : count
-                            - pos;
-                    System.arraycopy(buf, pos, buffer, offset, copylength);
-                    pos += copylength;
-                    if (copylength == length || !in.ready())
-                        return copylength;
-                    offset += copylength;
-                    required = length - copylength;
-                } else
-                    required = length;
-
-                while (true) {
-                    int read;
-                    /*
-                     * If we're not marked and the required size is greater than
-                     * the buffer, simply read the bytes directly bypassing the
-                     * buffer.
-                     */
-                    if (markpos == -1 && required >= buf.length) {
-                        read = in.read(buffer, offset, required);
-                        if (read == -1)
-                            return required == length ? -1 : length - required;
-                    } else {
-                        if (fillbuf() == -1)
-                            return required == length ? -1 : length - required;
-                        read = count - pos >= required ? required : count - pos;
-                        System.arraycopy(buf, pos, buffer, offset, read);
-                        pos += read;
-                    }
-                    required -= read;
-                    if (required == 0)
-                        return length;
-                    if (!in.ready())
-                        return length - required;
-                    offset += read;
-                }
-			}
-			throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005b")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Answers a <code>String</code> representing the next line of text
-	 * available in this BufferedReader. A line is represented by 0 or more
-	 * characters followed by <code>'\n'</code>, <code>'\r'</code>,
-	 * <code>'\r\n'</code> or end of stream. The <code>String</code> does
-	 * not include the newline sequence.
-	 * 
-	 * @return the contents of the line or null if no characters were read
-	 *         before end of stream.
-	 * 
-	 * @throws IOException
-	 *             If the BufferedReader is already closed or some other IO
-	 *             error occurs.
-	 */
-
-	public String readLine() throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				char eol = '\0';
-				StringBuilder result = new StringBuilder(80);
-				/* Typical Line Length */
-
-				while (true) {
-					/* Are there buffered characters available? */
-					if (pos >= count) {
-						if (eol == '\n')
-							return result.toString();
-						// attempt to fill buffer
-						if (fillbuf() == -1) // EOF case returns already read
-												// characters or null.
-							return result.length() > 0 || eol != '\0' ? result
-									.toString() : null;
-					}
-					for (int charPos = pos; charPos < count; charPos++) {
-						if (eol == '\0'
-								&& (buf[charPos] == '\n' || buf[charPos] == '\r')) {
-							eol = buf[charPos];
-						} else if (eol == '\r' && (buf[charPos] == '\n')) {
-							if (charPos > pos)
-								result.append(buf, pos, charPos - pos - 1);
-							pos = charPos + 1;
-							return result.toString();
-						} else if (eol != '\0') {
-							if (charPos > pos)
-								result.append(buf, pos, charPos - pos - 1);
-							pos = charPos;
-							return result.toString();
-						}
-					}
-					if (eol != '\0') // eol could only be last character.
-						result.append(buf, pos, count - pos - 1);
-					else
-						result.append(buf, pos, count - pos);
-					pos = count;
-				}
-			}
-			throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005b")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Answers a <code>boolean</code> indicating whether or not this Reader 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 <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 BufferedReader is already closed or some other IO
-	 *             error occurs.
-	 */
-
-	public boolean ready() throws IOException {
-		synchronized (lock) {
-			if (isOpen())
-				return ((count - pos) > 0) || in.ready();
-			throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005b")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Reset this BufferedReader's position to the last <code>mark()</code>
-	 * location. Invocations of <code>read()/skip()</code> will occur from
-	 * this new location. If this Reader was not marked, throw IOException.
-	 * 
-	 * @throws IOException
-	 *             If a problem occurred, the receiver does not support
-	 *             <code>mark()/reset()</code>, or no mark has been set.
-	 */
-
-	public void reset() throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				if (markpos != -1)
-					pos = markpos;
-				else
-					throw new IOException(org.apache.harmony.luni.util.Msg
-							.getString("K005c")); //$NON-NLS-1$
-			} else
-				throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005b")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Skips <code>amount</code> number of characters in this Reader.
-	 * Subsequent <code>read()</code>'s will not return these characters
-	 * unless <code>reset()</code> is used. Skipping characters may invalidate
-	 * a mark if marklimit is surpassed.
-	 * 
-	 * @param amount
-	 *            the maximum number of characters to skip.
-	 * @return the number of characters actually skipped.
-	 * 
-	 * @throws IOException
-	 *             If the BufferedReader is already closed or some other IO
-	 *             error occurs.
-	 */
-
-	public long skip(long amount) throws IOException {
-		if (amount >= 0) {
-			synchronized (lock) {
-				if (isOpen()) {
-					if (amount < 1)
-						return 0;
-					if (count - pos >= amount) {
-						pos += amount;
-						return amount;
-					}
-
-					long read = count - pos;
-					pos = count;
-					while (read < amount) {
-						if (fillbuf() == -1)
-							return read;
-						if (count - pos >= amount - read) {
-							pos += amount - read;
-							return amount;
-						}
-						// Couldn't get all the characters, skip what we read
-						read += (count - pos);
-						pos = count;
-					}
-					return amount;
-				}
-				throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005b")); //$NON-NLS-1$
-			}
-		}
-		throw new IllegalArgumentException();
-	}
-}
+/* Copyright 1998, 2005 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;
+
+
+/**
+ * BufferedReader is a buffered character input reader. Buffering allows reading
+ * from character streams more efficiently. If the default size of the buffer is
+ * not practical, another size may be specified. Reading a character from a
+ * Reader class usually involves reading a character from its Stream or
+ * subsequent Reader. It is advisable to wrap a BufferedReader around those
+ * Readers whose read operations may have high latency. For example, the
+ * following code
+ * 
+ * <pre>
+ * BufferedReader inReader = new BufferedReader(new FileReader(&quot;file.java&quot;));
+ * </pre>
+ * 
+ * will buffer input for the file <code>file.java</code>.
+ * 
+ * @see BufferedWriter
+ * @since 1.1
+ */
+
+public class BufferedReader extends Reader {
+	private Reader in;
+
+	private char[] buf;
+
+	private int marklimit = -1;
+
+	private int count;
+
+	private int markpos = -1;
+
+	private int pos;
+
+	/**
+	 * Constructs a new BufferedReader on the Reader <code>in</code>. The
+	 * default buffer size (8K) is allocated and all reads can now be filtered
+	 * through this BufferedReader.
+	 * 
+	 * @param in
+	 *            the Reader to buffer reads on.
+	 */
+
+	public BufferedReader(Reader in) {
+		super(in);
+		this.in = in;
+		buf = new char[8192];
+	}
+
+	/**
+	 * Constructs a new BufferedReader on the Reader <code>in</code>. The
+	 * buffer size is specified by the parameter <code>size</code> and all
+	 * reads can now be filtered through this BufferedReader.
+	 * 
+	 * @param in
+	 *            the Reader to buffer reads on.
+	 * @param size
+	 *            the size of buffer to allocate.
+	 */
+
+	public BufferedReader(Reader in, int size) {
+		super(in);
+		if (size > 0) {
+			this.in = in;
+			buf = new char[size];
+		} else {
+            throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
+        }
+	}
+
+	/**
+	 * Close the Reader. This implementation closes the Reader being filtered
+	 * and releases the buffer used by this reader. If this BufferedReader has
+	 * already been closed, nothing is done.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to close this BufferedReader.
+	 */
+
+	@Override
+    public void close() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				in.close();
+				buf = null;
+			}
+		}
+	}
+
+	private int fillbuf() throws IOException {
+		if (markpos == -1 || (pos - markpos >= marklimit)) {
+			/* Mark position not set or exceeded readlimit */
+			int result = in.read(buf, 0, buf.length);
+			if (result > 0) {
+				markpos = -1;
+				pos = 0;
+				count = result == -1 ? 0 : result;
+			}
+			return result;
+		}
+		if (markpos == 0 && marklimit > buf.length) {
+			/* Increase buffer size to accommodate the readlimit */
+			int newLength = buf.length * 2;
+			if (newLength > marklimit) {
+                newLength = marklimit;
+            }
+			char[] newbuf = new char[newLength];
+			System.arraycopy(buf, 0, newbuf, 0, buf.length);
+			buf = newbuf;
+		} else if (markpos > 0) {
+			System.arraycopy(buf, markpos, buf, 0, buf.length - markpos);
+		}
+
+		/* Set the new position and mark position */
+		pos -= markpos;
+		count = markpos = 0;
+		int charsread = in.read(buf, pos, buf.length - pos);
+		count = charsread == -1 ? pos : pos + charsread;
+		return charsread;
+	}
+
+	/**
+	 * Answer a boolean indicating whether or not this BufferedReader is open.
+	 * 
+	 * @return <code>true</code> if this reader is open, <code>false</code>
+	 *         otherwise
+	 */
+	private boolean isOpen() {
+		return buf != null;
+	}
+
+	/**
+	 * Set a Mark position in this BufferedReader. The parameter
+	 * <code>readLimit</code> indicates how many characters can be read before
+	 * a mark is invalidated. Sending reset() will reposition the reader back to
+	 * the marked position provided <code>readLimit</code> has not been
+	 * surpassed.
+	 * 
+	 * @param readlimit
+	 *            an int representing how many characters must be read
+	 *            before invalidating the mark.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting mark this BufferedReader.
+	 */
+
+	@Override
+    public void mark(int readlimit) throws IOException {
+		if (readlimit >= 0) {
+			synchronized (lock) {
+				if (isOpen()) {
+					marklimit = readlimit;
+					markpos = pos;
+				} else {
+                    throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+                }
+			}
+		} else {
+            throw new IllegalArgumentException();
+        }
+	}
+
+	/**
+	 * Answers a boolean indicating whether or not this Reader supports mark()
+	 * and reset(). This implementation answers <code>true</code>.
+	 * 
+	 * @return <code>true</code> if mark() and reset() are supported,
+	 *         <code>false</code> otherwise
+	 */
+
+	@Override
+    public boolean markSupported() {
+		return true;
+	}
+
+	/**
+	 * Reads a single character from this reader and returns the result as an
+	 * int. The 2 higher-order characters are set to 0. If the end of reader was
+	 * encountered then return -1. This implementation either returns a
+	 * character from the buffer or if there are no characters available, fill
+	 * the buffer then return a character or -1.
+	 * 
+	 * @return the character read or -1 if end of reader.
+	 * 
+	 * @throws IOException
+	 *             If the BufferedReader is already closed or some other IO
+	 *             error occurs.
+	 */
+
+	@Override
+    public int read() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				/* Are there buffered characters available? */
+				if (pos < count || fillbuf() != -1) {
+                    return buf[pos++];
+                }
+				return -1;
+			}
+			throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+		}
+	}
+
+	/**
+	 * Reads at most <code>length</code> characters from this BufferedReader
+	 * and stores them at <code>offset</code> in the character array
+	 * <code>buffer</code>. Returns the number of characters actually read or
+	 * -1 if the end of reader was encountered. If all the buffered characters
+	 * have been used, a mark has not been set, and the requested number of
+	 * characters is larger than this Readers buffer size, this implementation
+	 * bypasses the buffer and simply places the results directly into
+	 * <code>buffer</code>.
+	 * 
+	 * @param buffer
+	 *            character array to store the read characters
+	 * @param offset
+	 *            offset in buf to store the read characters
+	 * @param length
+	 *            maximum number of characters to read
+	 * @return number of characters read or -1 if end of reader.
+	 * 
+	 * @throws IOException
+	 *             If the BufferedReader is already closed or some other IO
+	 *             error occurs.
+	 */
+
+	@Override
+    public int read(char[] buffer, int offset, int length) throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				// check for null
+				int bufLen = buffer.length;
+                if(offset < 0 || length < 0 || (long)offset + (long)length > bufLen){
+                    throw new ArrayIndexOutOfBoundsException();
+                }
+				if (length == 0) {
+                    return 0;
+                }
+                int required;
+                if (pos < count) {
+                    /* There are bytes available in the buffer. */
+                    int copylength = count - pos >= length ? length : count
+                            - pos;
+                    System.arraycopy(buf, pos, buffer, offset, copylength);
+                    pos += copylength;
+                    if (copylength == length || !in.ready()) {
+                        return copylength;
+                    }
+                    offset += copylength;
+                    required = length - copylength;
+                } else {
+                    required = length;
+                }
+
+                while (true) {
+                    int read;
+                    /*
+                     * If we're not marked and the required size is greater than
+                     * the buffer, simply read the bytes directly bypassing the
+                     * buffer.
+                     */
+                    if (markpos == -1 && required >= buf.length) {
+                        read = in.read(buffer, offset, required);
+                        if (read == -1) {
+                            return required == length ? -1 : length - required;
+                        }
+                    } else {
+                        if (fillbuf() == -1) {
+                            return required == length ? -1 : length - required;
+                        }
+                        read = count - pos >= required ? required : count - pos;
+                        System.arraycopy(buf, pos, buffer, offset, read);
+                        pos += read;
+                    }
+                    required -= read;
+                    if (required == 0) {
+                        return length;
+                    }
+                    if (!in.ready()) {
+                        return length - required;
+                    }
+                    offset += read;
+                }
+			}
+			throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+		}
+	}
+
+	/**
+	 * Answers a <code>String</code> representing the next line of text
+	 * available in this BufferedReader. A line is represented by 0 or more
+	 * characters followed by <code>'\n'</code>, <code>'\r'</code>,
+	 * <code>'\r\n'</code> or end of stream. The <code>String</code> does
+	 * not include the newline sequence.
+	 * 
+	 * @return the contents of the line or null if no characters were read
+	 *         before end of stream.
+	 * 
+	 * @throws IOException
+	 *             If the BufferedReader is already closed or some other IO
+	 *             error occurs.
+	 */
+
+	public String readLine() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				char eol = '\0';
+				StringBuilder result = new StringBuilder(80);
+				/* Typical Line Length */
+
+				while (true) {
+					/* Are there buffered characters available? */
+					if (pos >= count) {
+						if (eol == '\n') {
+                            return result.toString();
+                        }
+						// attempt to fill buffer
+						if (fillbuf() == -1) {
+                            // characters or null.
+							return result.length() > 0 || eol != '\0' ? result
+									.toString() : null;
+                        }
+					}
+					for (int charPos = pos; charPos < count; charPos++) {
+						if (eol == '\0'
+								&& (buf[charPos] == '\n' || buf[charPos] == '\r')) {
+							eol = buf[charPos];
+						} else if (eol == '\r' && (buf[charPos] == '\n')) {
+							if (charPos > pos) {
+                                result.append(buf, pos, charPos - pos - 1);
+                            }
+							pos = charPos + 1;
+							return result.toString();
+						} else if (eol != '\0') {
+							if (charPos > pos) {
+                                result.append(buf, pos, charPos - pos - 1);
+                            }
+							pos = charPos;
+							return result.toString();
+						}
+					}
+					if (eol != '\0') {
+                        result.append(buf, pos, count - pos - 1);
+                    } else {
+                        result.append(buf, pos, count - pos);
+                    }
+					pos = count;
+				}
+			}
+			throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+		}
+	}
+
+	/**
+	 * Answers a <code>boolean</code> indicating whether or not this Reader 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 <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 BufferedReader is already closed or some other IO
+	 *             error occurs.
+	 */
+
+	@Override
+    public boolean ready() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+                return ((count - pos) > 0) || in.ready();
+            }
+			throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+		}
+	}
+
+	/**
+	 * Reset this BufferedReader's position to the last <code>mark()</code>
+	 * location. Invocations of <code>read()/skip()</code> will occur from
+	 * this new location. If this Reader was not marked, throw IOException.
+	 * 
+	 * @throws IOException
+	 *             If a problem occurred, the receiver does not support
+	 *             <code>mark()/reset()</code>, or no mark has been set.
+	 */
+
+	@Override
+    public void reset() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				if (markpos != -1) {
+                    pos = markpos;
+                } else {
+                    throw new IOException(Msg
+							.getString("K005c")); //$NON-NLS-1$
+                }
+			} else {
+                throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+            }
+		}
+	}
+
+	/**
+	 * Skips <code>amount</code> number of characters in this Reader.
+	 * Subsequent <code>read()</code>'s will not return these characters
+	 * unless <code>reset()</code> is used. Skipping characters may invalidate
+	 * a mark if marklimit is surpassed.
+	 * 
+	 * @param amount
+	 *            the maximum number of characters to skip.
+	 * @return the number of characters actually skipped.
+	 * 
+	 * @throws IOException
+	 *             If the BufferedReader is already closed or some other IO
+	 *             error occurs.
+	 */
+
+	@Override
+    public long skip(long amount) throws IOException {
+		if (amount >= 0) {
+			synchronized (lock) {
+				if (isOpen()) {
+					if (amount < 1) {
+                        return 0;
+                    }
+					if (count - pos >= amount) {
+						pos += amount;
+						return amount;
+					}
+
+					long read = count - pos;
+					pos = count;
+					while (read < amount) {
+						if (fillbuf() == -1) {
+                            return read;
+                        }
+						if (count - pos >= amount - read) {
+							pos += amount - read;
+							return amount;
+						}
+						// Couldn't get all the characters, skip what we read
+						read += (count - pos);
+						pos = count;
+					}
+					return amount;
+				}
+				throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+			}
+		}
+		throw new IllegalArgumentException();
+	}
+}

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java?rev=431830&r1=431829&r2=431830&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java Tue Aug 15 22:38:48 2006
@@ -1,286 +1,295 @@
-/* Copyright 1998, 2004 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 org.apache.harmony.luni.util.PriviAction;
-
-/**
- * BufferedWriter is for writing buffered character output. Characters written
- * to this Writer are buffered internally before being committed to the target
- * Writer.
- * 
- * @see BufferedReader
- */
-public class BufferedWriter extends Writer {
-	private Writer out;
-
-	private char buf[];
-
-	private int pos;
-
-	private final String lineSeparator = AccessController
-			.doPrivileged(new PriviAction<String>("line.separator")); //$NON-NLS-1$
-
-	/**
-	 * Constructs a new BufferedReader with <code>out</code> as the Writer on
-	 * which to buffer write operations. The buffer size is set to the default,
-	 * which is 8K.
-	 * 
-	 * @param out
-	 *            The Writer to buffer character writing on
-	 */
-
-	public BufferedWriter(Writer out) {
-		super(out);
-		this.out = out;
-		buf = new char[8192];
-	}
-
-	/**
-	 * Constructs a new BufferedReader with <code>out</code> as the Writer on
-	 * which buffer write operations. The buffer size is set to
-	 * <code>size</code>.
-	 * 
-	 * @param out
-	 *            The Writer to buffer character writing on.
-	 * @param size
-	 *            The size of the buffer to use.
-	 */
-
-	public BufferedWriter(Writer out, int size) {
-		super(out);
-		if (size > 0) {
-			this.out = out;
-			this.buf = new char[size];
-		} else
-			throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
-					.getString("K0058")); //$NON-NLS-1$
-	}
-
-	/**
-	 * Close this BufferedWriter. The contents of the buffer are flushed, the
-	 * target writer is closed, and the buffer is released. Only the first
-	 * invocation of close has any effect.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to close this Writer.
-	 */
-
-	public void close() throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				flush();
-				out.close();
-				buf = null;
-				out = null;
-			}
-		}
-	}
-
-	/**
-	 * Flush this BufferedWriter. The contents of the buffer are committed to
-	 * the target writer and it is then flushed.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to flush this Writer.
-	 */
-
-	public void flush() throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				if (pos > 0)
-					out.write(buf, 0, pos);
-				pos = 0;
-				out.flush();
-			} else
-				throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005d")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Answer a boolean indicating whether or not this BufferedWriter is open.
-	 * 
-	 * @return <code>true</code> if this reader is open, <code>false</code>
-	 *         otherwise
-	 */
-	private boolean isOpen() {
-		return out != null;
-	}
-
-	/**
-	 * Write a newline to thie Writer. A newline is determined by the System
-	 * property "line.separator". The target writer may or may not be flushed
-	 * when a newline is written.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to write to this Writer.
-	 */
-
-	public void newLine() throws IOException {
-		write(lineSeparator, 0, lineSeparator.length());
-	}
-
-	/**
-	 * Writes out <code>count</code> characters starting at
-	 * <code>offset</code> in <code>buf</code> to this BufferedWriter. If
-	 * <code>count</code> is greater than this Writers buffer then flush the
-	 * contents and also write the characters directly to the target Writer.
-	 * 
-	 * @param cbuf
-	 *            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 void write(char[] cbuf, int offset, int count) throws IOException {
-		// avoid int overflow
-		if (0 <= offset && offset <= cbuf.length && 0 <= count
-				&& count <= cbuf.length - offset) {
-			synchronized (lock) {
-				if (isOpen()) {
-					if (pos == 0 && count >= this.buf.length) {
-						out.write(cbuf, offset, count);
-						return;
-					}
-					int available = this.buf.length - pos;
-					if (count < available)
-						available = count;
-					if (available > 0) {
-						System
-								.arraycopy(cbuf, offset, this.buf, pos,
-										available);
-						pos += available;
-					}
-					if (pos == this.buf.length) {
-						out.write(this.buf, 0, this.buf.length);
-						pos = 0;
-						if (count > available) {
-							offset += available;
-							available = count - available;
-							if (available >= this.buf.length) {
-								out.write(cbuf, offset, available);
-								return;
-							}
-							System.arraycopy(cbuf, offset, this.buf, pos,
-									available);
-							pos += available;
-						}
-					}
-				} else
-					throw new IOException(org.apache.harmony.luni.util.Msg
-							.getString("K005d")); //$NON-NLS-1$
-			}
-		} else
-			throw new ArrayIndexOutOfBoundsException();
-	}
-
-	/**
-	 * Writes the character
-	 * <code>oneChar</code> BufferedWriter.  If the buffer is filled by
-	 * writing this character, flush this Writer.  Only the lower 2 bytes are written.
-	 *
-	 * @param 		oneChar	The Character to write out.
-	 *
-	 * @throws 		IOException 	If this Writer has already been closed or some other IOException occurs.
-	 */
-	public void write(int oneChar) throws IOException {
-		synchronized (lock) {
-			if (isOpen()) {
-				if (pos >= buf.length) {
-					out.write(buf, 0, buf.length);
-					pos = 0;
-				}
-				buf[pos++] = (char) oneChar;
-			} else
-				throw new IOException(org.apache.harmony.luni.util.Msg.getString("K005d")); //$NON-NLS-1$
-		}
-	}
-
-	/**
-	 * Writes out <code>count</code> characters starting at
-	 * <code>offset</code> in <code>str</code> to this BufferedWriter. If
-	 * <code>count</code> is greater than this Writers buffer then flush the
-	 * contents and also write the characters directly to the target Writer.
-	 * 
-	 * @param str
-	 *            the non-null String containing characters to write
-	 * @param offset
-	 *            offset in str 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 void write(String str, int offset, int count) throws IOException {
-		// avoid int overflow
-		if (0 <= offset && offset <= str.length() && 0 <= count
-				&& count <= str.length() - offset) {
-			synchronized (lock) {
-				if (isOpen()) {
-					if (pos == 0 && count >= buf.length) {
-						char[] chars = new char[count];
-						str.getChars(offset, offset + count, chars, 0);
-						out.write(chars, 0, count);
-						return;
-					}
-					int available = buf.length - pos;
-					if (count < available)
-						available = count;
-					if (available > 0) {
-						str.getChars(offset, offset + available, buf, pos);
-						pos += available;
-					}
-					if (pos == buf.length) {
-						out.write(this.buf, 0, this.buf.length);
-						pos = 0;
-						if (count > available) {
-							offset += available;
-							available = count - available;
-							if (available >= buf.length) {
-								char[] chars = new char[count];
-								str.getChars(offset, offset + available, chars,
-										0);
-								out.write(chars, 0, available);
-								return;
-							}
-							str.getChars(offset, offset + available, buf, pos);
-							pos += available;
-						}
-					}
-				} else
-					throw new IOException(org.apache.harmony.luni.util.Msg
-							.getString("K005d")); //$NON-NLS-1$
-			}
-		} else
-			throw new StringIndexOutOfBoundsException();
-	}
-}
+/* Copyright 1998, 2004 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 org.apache.harmony.luni.util.Msg;
+import org.apache.harmony.luni.util.PriviAction;
+
+/**
+ * BufferedWriter is for writing buffered character output. Characters written
+ * to this Writer are buffered internally before being committed to the target
+ * Writer.
+ * 
+ * @see BufferedReader
+ */
+public class BufferedWriter extends Writer {
+	private Writer out;
+
+	private char buf[];
+
+	private int pos;
+
+	private final String lineSeparator = AccessController
+			.doPrivileged(new PriviAction<String>("line.separator")); //$NON-NLS-1$
+
+	/**
+	 * Constructs a new BufferedReader with <code>out</code> as the Writer on
+	 * which to buffer write operations. The buffer size is set to the default,
+	 * which is 8K.
+	 * 
+	 * @param out
+	 *            The Writer to buffer character writing on
+	 */
+
+	public BufferedWriter(Writer out) {
+		super(out);
+		this.out = out;
+		buf = new char[8192];
+	}
+
+	/**
+	 * Constructs a new BufferedReader with <code>out</code> as the Writer on
+	 * which buffer write operations. The buffer size is set to
+	 * <code>size</code>.
+	 * 
+	 * @param out
+	 *            The Writer to buffer character writing on.
+	 * @param size
+	 *            The size of the buffer to use.
+	 */
+
+	public BufferedWriter(Writer out, int size) {
+		super(out);
+		if (size > 0) {
+			this.out = out;
+			this.buf = new char[size];
+		} else {
+            throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
+        }
+	}
+
+	/**
+	 * Close this BufferedWriter. The contents of the buffer are flushed, the
+	 * target writer is closed, and the buffer is released. Only the first
+	 * invocation of close has any effect.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to close this Writer.
+	 */
+
+	@Override
+    public void close() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				flush();
+				out.close();
+				buf = null;
+				out = null;
+			}
+		}
+	}
+
+	/**
+	 * Flush this BufferedWriter. The contents of the buffer are committed to
+	 * the target writer and it is then flushed.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to flush this Writer.
+	 */
+
+	@Override
+    public void flush() throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				if (pos > 0) {
+                    out.write(buf, 0, pos);
+                }
+				pos = 0;
+				out.flush();
+			} else {
+                throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
+            }
+		}
+	}
+
+	/**
+	 * Answer a boolean indicating whether or not this BufferedWriter is open.
+	 * 
+	 * @return <code>true</code> if this reader is open, <code>false</code>
+	 *         otherwise
+	 */
+	private boolean isOpen() {
+		return out != null;
+	}
+
+	/**
+	 * Write a newline to thie Writer. A newline is determined by the System
+	 * property "line.separator". The target writer may or may not be flushed
+	 * when a newline is written.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to write to this Writer.
+	 */
+
+	public void newLine() throws IOException {
+		write(lineSeparator, 0, lineSeparator.length());
+	}
+
+	/**
+	 * Writes out <code>count</code> characters starting at
+	 * <code>offset</code> in <code>buf</code> to this BufferedWriter. If
+	 * <code>count</code> is greater than this Writers buffer then flush the
+	 * contents and also write the characters directly to the target Writer.
+	 * 
+	 * @param cbuf
+	 *            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.
+	 */
+
+	@Override
+    public void write(char[] cbuf, int offset, int count) throws IOException {
+		// avoid int overflow
+		if (0 <= offset && offset <= cbuf.length && 0 <= count
+				&& count <= cbuf.length - offset) {
+			synchronized (lock) {
+				if (isOpen()) {
+					if (pos == 0 && count >= this.buf.length) {
+						out.write(cbuf, offset, count);
+						return;
+					}
+					int available = this.buf.length - pos;
+					if (count < available) {
+                        available = count;
+                    }
+					if (available > 0) {
+						System.arraycopy(cbuf, offset, this.buf, pos, available);
+						pos += available;
+					}
+					if (pos == this.buf.length) {
+						out.write(this.buf, 0, this.buf.length);
+						pos = 0;
+						if (count > available) {
+							offset += available;
+							available = count - available;
+							if (available >= this.buf.length) {
+								out.write(cbuf, offset, available);
+								return;
+							}
+							System.arraycopy(cbuf, offset, this.buf, pos, available);
+                            pos += available;
+						}
+					}
+				} else {
+                    throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
+                }
+			}
+		} else {
+            throw new ArrayIndexOutOfBoundsException();
+        }
+	}
+
+	/**
+	 * Writes the character
+	 * <code>oneChar</code> BufferedWriter.  If the buffer is filled by
+	 * writing this character, flush this Writer.  Only the lower 2 bytes are written.
+	 *
+	 * @param 		oneChar	The Character to write out.
+	 *
+	 * @throws 		IOException 	If this Writer has already been closed or some other IOException occurs.
+	 */
+	@Override
+    public void write(int oneChar) throws IOException {
+		synchronized (lock) {
+			if (isOpen()) {
+				if (pos >= buf.length) {
+					out.write(buf, 0, buf.length);
+					pos = 0;
+				}
+				buf[pos++] = (char) oneChar;
+			} else {
+                throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
+            }
+		}
+	}
+
+	/**
+	 * Writes out <code>count</code> characters starting at
+	 * <code>offset</code> in <code>str</code> to this BufferedWriter. If
+	 * <code>count</code> is greater than this Writers buffer then flush the
+	 * contents and also write the characters directly to the target Writer.
+	 * 
+	 * @param str
+	 *            the non-null String containing characters to write
+	 * @param offset
+	 *            offset in str 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.
+	 */
+
+	@Override
+    public void write(String str, int offset, int count) throws IOException {
+		// avoid int overflow
+		if (0 <= offset && offset <= str.length() && 0 <= count
+				&& count <= str.length() - offset) {
+			synchronized (lock) {
+				if (isOpen()) {
+					if (pos == 0 && count >= buf.length) {
+						char[] chars = new char[count];
+						str.getChars(offset, offset + count, chars, 0);
+						out.write(chars, 0, count);
+						return;
+					}
+					int available = buf.length - pos;
+					if (count < available) {
+                        available = count;
+                    }
+					if (available > 0) {
+						str.getChars(offset, offset + available, buf, pos);
+						pos += available;
+					}
+					if (pos == buf.length) {
+						out.write(this.buf, 0, this.buf.length);
+						pos = 0;
+						if (count > available) {
+							offset += available;
+							available = count - available;
+							if (available >= buf.length) {
+								char[] chars = new char[count];
+								str.getChars(offset, offset + available, chars,
+										0);
+								out.write(chars, 0, available);
+								return;
+							}
+							str.getChars(offset, offset + available, buf, pos);
+							pos += available;
+						}
+					}
+				} else {
+                    throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
+                }
+			}
+		} else {
+            throw new StringIndexOutOfBoundsException();
+        }
+	}
+}

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



Mime
View raw message