[kaffe] CVS kaffe (guilhem): Imported java.io from GNU Classpath (except encoding) + fixes

Kaffe CVS cvs-commits at kaffe.org
Sun Nov 2 05:32:02 PST 2003


PatchSet 4148 
Date: 2003/11/02 13:29:34
Author: guilhem
Branch: HEAD
Tag: (none) 
Log:
Imported java.io from GNU Classpath (except encoding) + fixes

This checkin has also a small fix concerning java.util.zip against the
new implementation of FilterOutputStream.

Members: 
	ChangeLog:1.1740->1.1741 
	libraries/clib/zip/Deflater.c:1.11->1.12 
	libraries/javalib/java/io/BufferedInputStream.java:1.11->1.12 
	libraries/javalib/java/io/BufferedOutputStream.java:1.7->1.8 
	libraries/javalib/java/io/BufferedReader.java:1.11->1.12 
	libraries/javalib/java/io/BufferedWriter.java:1.6->1.7 
	libraries/javalib/java/io/CharArrayReader.java:1.5->1.6 
	libraries/javalib/java/io/CharArrayWriter.java:1.7->1.8 
	libraries/javalib/java/io/DataInput.java:1.5->1.6 
	libraries/javalib/java/io/DataInputStream.java:1.18->1.19 
	libraries/javalib/java/io/DataOutput.java:1.5->1.6 
	libraries/javalib/java/io/DataOutputStream.java:1.8->1.9 
	libraries/javalib/java/io/FileFilter.java:1.2->1.3 
	libraries/javalib/java/io/FilenameFilter.java:1.4->1.5 
	libraries/javalib/java/io/FilterInputStream.java:1.9->1.10 
	libraries/javalib/java/io/FilterOutputStream.java:1.1->1.2 
	libraries/javalib/java/io/FilterReader.java:1.2->1.3 
	libraries/javalib/java/io/FilterWriter.java:1.4->1.5 
	libraries/javalib/java/io/InterruptedIOException.java:1.5->1.6 
	libraries/javalib/java/io/InvalidClassException.java:1.8->1.9 
	libraries/javalib/java/io/LineNumberInputStream.java:1.4->1.5 
	libraries/javalib/java/io/LineNumberReader.java:1.4->1.5 
	libraries/javalib/java/io/OptionalDataException.java:1.5->1.6 
	libraries/javalib/java/io/PrintStream.java:1.14->1.15 
	libraries/javalib/java/io/PrintWriter.java:1.6->1.7 
	libraries/javalib/java/io/PushbackInputStream.java:1.5->1.6 
	libraries/javalib/java/io/PushbackReader.java:1.2->1.3 
	libraries/javalib/java/io/Reader.java:1.7->1.8 
	libraries/javalib/java/io/SequenceInputStream.java:1.4->1.5 
	libraries/javalib/java/io/StreamTokenizer.java:1.21->1.22 
	libraries/javalib/java/io/WriteAbortedException.java:1.4->1.5 
	libraries/javalib/java/io/Writer.java:1.6->1.7 
	libraries/javalib/java/net/DatagramSocket.java:1.17->1.18 
	libraries/javalib/java/util/zip/DeflaterOutputStream.java:1.6->1.7 
	libraries/javalib/java/util/zip/ZipOutputStream.java:1.14->1.15 
	test/regression/BufferedReaderTest.java:1.2->1.3 
	test/regression/CharArrayReaderTest.java:1.2->1.3 
	test/regression/InputStreamTest.java:1.1->1.2 
	test/regression/LineNumberReaderTest.java:1.2->1.3 
	test/regression/ReaderTest.java:1.1->1.2 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.1740 kaffe/ChangeLog:1.1741
--- kaffe/ChangeLog:1.1740	Fri Oct 31 20:46:56 2003
+++ kaffe/ChangeLog	Sun Nov  2 13:29:34 2003
@@ -1,3 +1,56 @@
+2003-11-02  Guilhem Lavaux <guilhem at kaffe.org>
+
+	* libraries/javalib/java/io/BufferedInputStream.java,
+	libraries/javalib/java/io/BufferedOutputStream.java,
+	libraries/javalib/java/io/BufferedReader.java, 
+	libraries/javalib/java/io/BufferedWriter.java,
+	libraries/javalib/java/io/CharArrayReader.java,
+	libraries/javalib/java/io/CharArrayWriter.java,
+	libraries/javalib/java/io/DataInput.java,
+	libraries/javalib/java/io/DataInputStream.java,
+	libraries/javalib/java/io/DataOutput.java,
+	libraries/javalib/java/io/DataOutputStream.java,
+	libraries/javalib/java/io/FileFilter.java,
+	libraries/javalib/java/io/FilenameFilter.java,
+	libraries/javalib/java/io/FilterInputStream.java,
+	libraries/javalib/java/io/FilterOutputStream.java,
+	libraries/javalib/java/io/FilterReader.java,
+	libraries/javalib/java/io/FilterWriter.java,
+	libraries/javalib/java/io/InterruptedIOException.java,
+	libraries/javalib/java/io/InvalidClassException.java,
+	libraries/javalib/java/io/LineNumberInputStream.java,
+	libraries/javalib/java/io/LineNumberReader.java,
+	libraries/javalib/java/io/OptionalDataException.java,
+	libraries/javalib/java/io/PrintStream.java,
+	libraries/javalib/java/io/PrintWriter.java,
+	libraries/javalib/java/io/PushbackInputStream.java,
+	libraries/javalib/java/io/PushbackReader.java,
+	libraries/javalib/java/io/Reader.java,
+	libraries/javalib/java/io/SequenceInputStream.java,
+	libraries/javalib/java/io/StreamTokenizer.java,
+	libraries/javalib/java/io/WriteAbortedException.java,
+	libraries/javalib/java/io/Writer.java:
+
+	Imported classes from GNU Classpath. These classes have
+	been fixed to pass Kaffe's regression tests.
+	
+	* libraries/javalib/java/net/DatagramSocket.java:
+	(bind) When address is null, it should bind to an available
+	port.
+	
+	* libraries/javalib/java/util/zip/ZipOutputStream.java:
+	(deflate) Don't call super.write because it may cause
+	an infinite loop with the new classpath's implementation.
+	
+	* test/regression/BufferedReaderTest.java,
+	test/regression/CharArrayReaderTest.java,
+	test/regression/InputStreamTest.java,
+	test/regression/LineNumberReaderTest.java,
+	test/regression/ReaderTest.java:
+
+	Adapted the output of the classes (Exceptions precise
+	output) to match the current implementation.
+	
 2003-10-31  Helmer Kraemer <hkraemer at freenet.de>
 	    Dalibor Topic <robilad at kaffe.org>
 
Index: kaffe/libraries/clib/zip/Deflater.c
diff -u kaffe/libraries/clib/zip/Deflater.c:1.11 kaffe/libraries/clib/zip/Deflater.c:1.12
--- kaffe/libraries/clib/zip/Deflater.c:1.11	Mon Jun 19 11:46:46 2000
+++ kaffe/libraries/clib/zip/Deflater.c	Sun Nov  2 13:29:36 2003
@@ -8,8 +8,6 @@
  * of this file. 
  */
 
-#define	DBG(s)
-
 #include "config.h"
 #include "config-std.h"
 #include "config-mem.h"
@@ -58,7 +56,7 @@
 
 	r = deflate(dstream, unhand(this)->finish ? Z_FINISH : Z_NO_FLUSH);
 
-DBG(	dprintf("Deflate: in %d left %d out %d status %d\n", ilen, dstream->avail_in, len - dstream->avail_out, r);	)
+DBG(NATIVELIB,	dprintf("Deflate: in %d left %d out %d status %d\n", ilen, dstream->avail_in, len - dstream->avail_out, r);	)
 
 	switch (r) {
 	case Z_OK:
Index: kaffe/libraries/javalib/java/io/BufferedInputStream.java
diff -u kaffe/libraries/javalib/java/io/BufferedInputStream.java:1.11 kaffe/libraries/javalib/java/io/BufferedInputStream.java:1.12
--- kaffe/libraries/javalib/java/io/BufferedInputStream.java:1.11	Sat Jun  7 17:05:39 2003
+++ kaffe/libraries/javalib/java/io/BufferedInputStream.java	Sun Nov  2 13:29:36 2003
@@ -1,210 +1,360 @@
-/*
- * Java core library component.
- *
- * Copyright (c) 1997, 1998
- *      Transvirtual Technologies, Inc.  All rights reserved.
- *
- * See the file "license.terms" for information on usage and redistribution
- * of this file.
- */
+/* BufferedInputStream.java -- An input stream that implements buffering
+   Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
 
-package java.io;
+This file is part of GNU Classpath.
 
-public class BufferedInputStream extends FilterInputStream
-{
-	protected byte[] buf;
-	protected int count;
-	protected int pos;
-	protected int markpos;
-	protected int marklimit;
-	private final byte[] single = new byte[1];
-	private static final int DEFAULTBUFFER = 2048;
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
 
-/*
- * Invariant:
- *
- *   markpos <= pos <= count <= buf.length >= marklimit
- */
+ 
+package java.io;
 
-public BufferedInputStream(InputStream in) {
-	this(in, DEFAULTBUFFER);
-}
-
-public BufferedInputStream(InputStream in, int size) {
-	super(in);
-
-	if (size <= 0) {
-		throw new IllegalArgumentException("Buffer size <= 0");
-	}
-
-	buf = new byte[size];
-	marklimit = size;
-	markpos = -1;
-}
-
-public synchronized int available() throws IOException {
-	checkIfStillOpen();
-	return (count - pos) + in.available();
-}
-
-private void checkIfStillOpen() throws IOException {
-	if (in == null) {
-		throw new IOException("stream closed");
-	}
-}
-
-public synchronized void mark(int marklimit) {
-	if (marklimit > buf.length - pos) {		// not enough room
-		byte[] newbuf;
-
-		if (marklimit <= buf.length) {
-			newbuf = buf;			// just shift buffer
-		} else {
-			newbuf = new byte[marklimit];	// need a new buffer
-		}
-		System.arraycopy(buf, pos, newbuf, 0, count - pos);
-		buf = newbuf;
-		count -= pos;
-		pos = markpos = 0;
-	} else {
-		markpos = pos;
-	}
-	this.marklimit = marklimit;
-}
-
-public boolean markSupported() {
-	return true;
-}
-
-public synchronized int read() throws IOException {
-	if (_read(single, 0, 1) == -1) {
-		return (-1);
-	}
-	else {
-		return (single[0] & 0xFF);
-	}
-}
-
-public synchronized int read(byte b[], int off, int len) throws IOException {
-	return _read(b, off, len);
-}
-
-private int _read(byte b[], int off, int len) throws IOException {
-	if (off < 0 || len < 0 || off + len > b.length) {
-	   throw new IndexOutOfBoundsException();
-	}
-
-	checkIfStillOpen();
-
-	/* Common case short-cut */
-	if (len == 1 && pos < count) {
-		b[off] = buf[pos++];
-		return (1);
-	}
-
-	int total = 0;
-
-	do {
-		// If buffer fully consumed, invalidate mark & reset buffer
-		if (pos == buf.length) {
-			pos = count = 0;
-			markpos = -1;
-		}
-
-		// Buffer empty?
-		int nread;
-		if (pos == count) {
-
-			// If the amount requested is more than the size
-			// of the buffer, we might as well optimize with
-			// a direct read to avoid needless copying of data.
-			if (len >= buf.length) {
-				if ((nread = super.read(b, off, len)) == -1) {
-					return (total > 0) ? total : -1;
-				}
-				return total + nread;
-			}
-
-			// Read another buffer's worth of data
-			if (!fillBuffer()) {
-				return (total > 0) ? total : -1;
-			}
-		}
-
-		// Copy the next chunk of bytes from our buffer
-		nread = Math.min(count - pos, len);
-		System.arraycopy(buf, pos, b, off, nread);
-		total += nread;
-		pos += nread;
-		off += nread;
-		len -= nread;
-	} while (len > 0 && available() > 0);
-
-	return total;
-}
-
-public synchronized void reset() throws IOException {
-	checkIfStillOpen();
-
-	if (markpos == -1) {
-		throw new IOException(
-		    "Attempt to reset when no mark is valid"
-			+ " (marklimit=" + marklimit + ")");
-	}
-	pos = markpos;
-}
-
-/*
- * This version of skip() does not invalidate a mark if less
- * than marklimit total bytes are read and/or skipped.
- * Not sure if this is actually a requirement or not.
+/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
+ * "The Java Language Specification", ISBN 0-201-63451-1
+ * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
+ * Status:  Believed complete and correct.
  */
-public synchronized long skip(long n) throws IOException {
-
-	checkIfStillOpen();
-
-	// Sanity check
-	if (n <= 0) {
-		return 0;
-	}
-
-	// Skip buffered data if there is any
-	if (pos < count) {
-		if (count - pos > n) {
-			pos += (int)n;		// narrowing cast OK
-		} else {
-			n = count - pos;
-			pos = count;
-		}
-		return n;
-	}
-
-	// If buffer fully consumed, invalidate mark & reset buffer
-	if (pos == buf.length) {
-		pos = count = 0;
-		markpos = -1;
-		return super.skip(n);
-	}
-
-	// Read data into buffer and try again
-	return fillBuffer() ? skip(n) : 0;
-}
-
-/*
- * Get more buffered data. This should only be called when:
+ 
+/**
+ * This subclass of <code>FilterInputStream</code> buffers input from an 
+ * underlying implementation to provide a possibly more efficient read
+ * mechanism.  It maintains the buffer and buffer state in instance 
+ * variables that are available to subclasses.  The default buffer size
+ * of 2048 bytes can be overridden by the creator of the stream.
+ * <p>
+ * This class also implements mark/reset functionality.  It is capable
+ * of remembering any number of input bytes, to the limits of
+ * system memory or the size of <code>Integer.MAX_VALUE</code>
+ * <p>
+ * Please note that this class does not properly handle character
+ * encodings.  Consider using the <code>BufferedReader</code> class which
+ * does.
  *
- *	1 pos == count
- *	2 count < buf.length
- *
- * Returns true if at least one byte was read.
+ * @author Aaron M. Renn (arenn at urbanophile.com)
+ * @author Warren Levy <warrenl at cygnus.com>
  */
-private boolean fillBuffer() throws IOException {
-	int nread;
+public class BufferedInputStream extends FilterInputStream
+{
 
-	if ((nread = super.read(buf, pos, buf.length - pos)) <= 0) {
-		return false;
-	}
-	count += nread;
-	return true;
-}
+  /**
+   * This is the default buffer size
+   */
+  private static final int DEFAULT_BUFFER_SIZE = 2048;
+
+  /**
+   * The buffer used for storing data from the underlying stream.
+   */
+  protected byte[] buf;
+
+  /**
+   * The number of valid bytes currently in the buffer.  It is also the index
+   * of the buffer position one byte past the end of the valid data.
+   */
+  protected int count = 0;
+
+  /**
+   * The index of the next character that will by read from the buffer.
+   * When <code>pos == count</code>, the buffer is empty.
+   */
+  protected int pos = 0;
+
+  /**
+   * The value of <code>pos</code> when the <code>mark()</code> method was
+   * called.  
+   * This is set to -1 if there is no mark set.
+   */
+  protected int markpos = -1;
+
+  /**
+   * This is the maximum number of bytes than can be read after a 
+   * call to <code>mark()</code> before the mark can be discarded.
+   * After this may bytes are read, the <code>reset()</code> method
+   * may not be called successfully.
+   */
+  protected int marklimit = 0;
+
+  /**
+   * This method initializes a new <code>BufferedInputStream</code> that will
+   * read from the specified subordinate stream with a default buffer size
+   * of 2048 bytes
+   *
+   * @param in The subordinate stream to read from
+   */
+  public BufferedInputStream(InputStream in)
+  {
+    this(in, DEFAULT_BUFFER_SIZE);
+  }
+
+  /**
+   * This method initializes a new <code>BufferedInputStream</code> that will
+   * read from the specified subordinate stream with a buffer size that
+   * is specified by the caller.
+   *
+   * @param in The subordinate stream to read from
+   * @param size The buffer size to use
+   *
+   * @exception IllegalArgumentException when size is smaller then 1
+   */
+  public BufferedInputStream(InputStream in, int size)
+  {
+    super(in);
+    if (size <= 0)
+      throw new IllegalArgumentException();
+    buf = new byte[size];
+  }
+
+  /**
+   * This method returns the number of bytes that can be read from this
+   * stream before a read can block.  A return of 0 indicates that blocking
+   * might (or might not) occur on the very next read attempt.
+   * <p>
+   * The number of available bytes will be the number of read ahead bytes 
+   * stored in the internal buffer plus the number of available bytes in
+   * the underlying stream.
+   *
+   * @return The number of bytes that can be read before blocking could occur
+   *
+   * @exception IOException If an error occurs
+   */
+  public synchronized int available() throws IOException
+  {
+    return count - pos + super.available();
+  }
+
+  /**
+   * This method closes the underlying input stream and frees any
+   * resources associated with it. Sets <code>buf</code> to <code>null</code>.
+   *
+   * @exception IOException If an error occurs.
+   */
+  public void close() throws IOException
+  {
+    // Free up the array memory.
+    buf = null;
+    super.close();
+  }
+
+  /**
+   * This method marks a position in the input to which the stream can be
+   * "reset" by calling the <code>reset()</code> method.  The parameter
+   * <code>readlimit</code> is the number of bytes that can be read from the 
+   * stream after setting the mark before the mark becomes invalid.  For
+   * example, if <code>mark()</code> is called with a read limit of 10, then
+   * when 11 bytes of data are read from the stream before the
+   * <code>reset()</code> method is called, then the mark is invalid and the
+   * stream object instance is not required to remember the mark.
+   * <p>
+   * Note that the number of bytes that can be remembered by this method
+   * can be greater than the size of the internal read buffer.  It is also
+   * not dependent on the subordinate stream supporting mark/reset
+   * functionality.
+   *
+   * @param readlimit The number of bytes that can be read before the mark
+   * becomes invalid
+   */
+  public synchronized void mark(int readlimit)
+  {
+    marklimit = readlimit;
+    markpos = pos;
+  }
+
+  /**
+   * This method returns <code>true</code> to indicate that this class
+   * supports mark/reset functionality.
+   *
+   * @return <code>true</code> to indicate that mark/reset functionality is
+   * supported
+   *
+   */
+  public boolean markSupported()
+  {
+    return true;
+  }
+
+  /**
+   * This method reads an unsigned byte from the input stream and returns it
+   * as an int in the range of 0-255.  This method also will return -1 if
+   * the end of the stream has been reached.
+   * <p>
+   * This method will block until the byte can be read.
+   *
+   * @return The byte read or -1 if end of stream
+   *
+   * @exception IOException If an error occurs
+   */
+  public synchronized int read() throws IOException
+  {
+    if (pos >= count && !refill())
+      return -1;	// EOF
+
+    if (markpos >= 0 && pos - markpos > marklimit)
+      markpos = -1;
+
+    return ((int) buf[pos++]) & 0xFF;
+  }
+
+  /**
+   * This method reads bytes from a stream and stores them into a caller
+   * supplied buffer.  It starts storing the data at index <code>off</code>
+   * into the buffer and attempts to read <code>len</code> bytes.  This method
+   * can return before reading the number of bytes requested.  The actual
+   * number of bytes read is returned as an int.  A -1 is returned to indicate
+   * the end of the stream.
+   * <p>
+   * This method will block until some data can be read.
+   *
+   * @param b The array into which the bytes read should be stored
+   * @param off The offset into the array to start storing bytes
+   * @param len The requested number of bytes to read
+   *
+   * @return The actual number of bytes read, or -1 if end of stream.
+   *
+   * @exception IOException If an error occurs.
+   * @exception IndexOutOfBoundsException when <code>off</code> or
+   *            <code>len</code> are negative, or when <code>off + len</code>
+   *            is larger then the size of <code>b</code>,
+   */
+  public synchronized int read(byte[] b, int off, int len) throws IOException
+  {
+    if (off < 0 || len < 0 || off + len > b.length)
+      throw new IndexOutOfBoundsException();
+
+    if (pos >= count && !refill())
+      return -1;		// No bytes were read before EOF.
+
+    int remain = Math.min(count - pos, len);
+    System.arraycopy(buf, pos, b, off, remain);
+    pos += remain;
+
+    if (markpos >= 0 && pos - markpos > marklimit)
+      markpos = -1;
+
+    return remain;
+  }
+
+  /**
+   * This method resets a stream to the point where the <code>mark()</code>
+   * method was called.  Any bytes that were read after the mark point was
+   * set will be re-read during subsequent reads.
+   * <p>
+   * This method will throw an IOException if the number of bytes read from
+   * the stream since the call to <code>mark()</code> exceeds the mark limit
+   * passed when establishing the mark.
+   *
+   * @exception IOException If <code>mark()</code> was never called or more
+   *            then <code>markLimit</code> bytes were read since the last
+   *            call to <code>mark()</code>
+   */
+  public synchronized void reset() throws IOException
+  {
+    if (markpos < 0)
+      throw new IOException();
+
+    pos = markpos;
+  }
+
+  /**
+   * This method skips the specified number of bytes in the stream.  It
+   * returns the actual number of bytes skipped, which may be less than the
+   * requested amount.
+   *
+   * @param n The requested number of bytes to skip
+   *
+   * @return The actual number of bytes skipped.
+   *
+   * @exception IOException If an error occurs
+   */
+  public synchronized long skip(long n) throws IOException
+  {
+    final long origN = n;
+
+    while (n > 0L)
+      {
+	if (pos >= count && !refill())
+	  if (n < origN)
+	    break;
+	  else
+	    return -1;	// No bytes were read before EOF.
+
+	int numread = (int) Math.min((long) (count - pos), n);
+	pos += numread;
+	n -= numread;
+
+        if (markpos >= 0 && pos - markpos > marklimit)
+          markpos = -1;
+      }
+
+    return origN - n;
+  }
+
+  /**
+   * Called to refill the buffer (when count is equal or greater the pos).
+   * Package local so BufferedReader can call it when needed.
+   *
+   * @return <code>true</code> when <code>buf</code> can be (partly) refilled,
+   *         <code>false</code> otherwise.
+   */
+  boolean refill() throws IOException
+  {
+    if (markpos < 0)
+      count = pos = 0;
+    else if (markpos > 0)
+      {
+        // Shift the marked bytes (if any) to the beginning of the array
+	// but don't grow it.  This saves space in case a reset is done
+	// before we reach the max capacity of this array.
+        System.arraycopy(buf, markpos, buf, 0, count - markpos);
+	count -= markpos;
+	pos -= markpos;
+	markpos = 0;
+      }
+    else if (marklimit >= buf.length)	// BTW, markpos == 0
+      {
+	// Need to grow the buffer now to have room for marklimit bytes.
+	// Note that the new buffer is one greater than marklimit.
+	// This is so that there will be one byte past marklimit to be read
+	// before having to call refill again, thus allowing marklimit to be
+	// invalidated.  That way refill doesn't have to check marklimit.
+	byte[] newbuf = new byte[marklimit + 1];
+	System.arraycopy(buf, 0, newbuf, 0, count);
+	buf = newbuf;
+      }
+
+    int numread = super.read(buf, count, buf.length - count);
+
+    if (numread < 0)	// EOF
+      return false;
+
+    count += numread;
+    return true;
+  }
 }
Index: kaffe/libraries/javalib/java/io/BufferedOutputStream.java
diff -u kaffe/libraries/javalib/java/io/BufferedOutputStream.java:1.7 kaffe/libraries/javalib/java/io/BufferedOutputStream.java:1.8
--- kaffe/libraries/javalib/java/io/BufferedOutputStream.java:1.7	Tue Sep  3 19:53:47 2002
+++ kaffe/libraries/javalib/java/io/BufferedOutputStream.java	Sun Nov  2 13:29:36 2003
@@ -1,97 +1,192 @@
-package java.io;
+/* BufferedOutputStream.java -- Buffer output into large blocks before writing
+   Copyright (C) 1998, 2000, 2003 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+ 
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
 
-/*
- * Java core library component.
- *
- * Copyright (c) 1997, 1998
- *      Transvirtual Technologies, Inc.  All rights reserved.
- *
- * See the file "license.terms" for information on usage and redistribution
- * of this file.
- */
 
+package java.io;
+
+/**
+  * This class accumulates bytes written in a buffer instead of immediately
+  * writing the data to the underlying output sink. The bytes are instead
+  * as one large block when the buffer is filled, or when the stream is
+  * closed or explicitly flushed. This mode operation can provide a more
+  * efficient mechanism for writing versus doing numerous small unbuffered
+  * writes.
+  *
+  * @author Aaron M. Renn (arenn at urbanophile.com)
+  */
 public class BufferedOutputStream extends FilterOutputStream
 {
-	private static final int DEFAULTBUFFER = 2048;
-	protected byte[] buf;
-	protected int count;
-
-public BufferedOutputStream(OutputStream out) {
-	this(out, DEFAULTBUFFER);
-}
-
-public BufferedOutputStream(OutputStream out, int size) {
-	super(out);
-
-	if (size <= 0) {
-	        throw new IllegalArgumentException("Buffer size <= 0");
-	}
-
-	buf = new byte[size];
-	count = 0;
-}
-
-public synchronized void flush() throws IOException {
-	if (count > 0) {
-		try
-		{
-			out.write(buf, 0, count);
-			count = 0;
-		}
-		catch(InterruptedIOException e)
-		{
-			count -= e.bytesTransferred;
-			throw e;
-		}
-	}
-	out.flush();
-}
-
-public synchronized void write(byte b[], int off, int len) throws IOException {
-	if (off < 0 || len < 0 || off + len > b.length) {
-	   throw new IndexOutOfBoundsException();
-	}
-
-	// If no write will be necessary, just copy the new data to the buffer
-	if (count + len <= buf.length) {
-	    System.arraycopy(b, off, buf, count, len);
-	    count += len;
-	    return;
-	}
-
-	// Otherwise, first write out any old buffered data
-	if (count > 0) {
-		try
-		{
-			out.write(buf, 0, count);
-			count = 0;
-		}
-		catch(InterruptedIOException e)
-		{
-			count -= e.bytesTransferred;
-			throw e;
-		}
-	}
-
-	// Then write out the new data directly, without copying
-	out.write(b, off, len);
-}
-
-public synchronized void write(int b) throws IOException {
-	if (count == buf.length) {
-		try
-		{
-			out.write(buf, 0, buf.length);
-			count = 0;
-		}
-		catch(InterruptedIOException e)
-		{
-			count -= e.bytesTransferred;
-			throw e;
-		}
-	}
-	buf[count++] = (byte)b;
-}
+  /**
+   * This is the default buffer size
+   */
+  private static final int DEFAULT_BUFFER_SIZE = 512;
+
+  /**
+   * This is the internal byte array used for buffering output before
+   * writing it.
+   */
+  protected byte[] buf;
+
+  /**
+   * This is the number of bytes that are currently in the buffer and
+   * are waiting to be written to the underlying stream.  It always points to
+   * the index into the buffer where the next byte of data will be stored
+   */
+  protected int count;
+
+  /**
+   * This method initializes a new <code>BufferedOutputStream</code> instance
+   * that will write to the specified subordinate <code>OutputStream</code>
+   * and which will use a default buffer size of 512 bytes.
+   *
+   * @param out The underlying <code>OutputStream</code> to write data to
+   */
+  public BufferedOutputStream(OutputStream out)
+  {
+    this(out, DEFAULT_BUFFER_SIZE);
+  }
+
+  /**
+   * This method initializes a new <code>BufferedOutputStream</code> instance
+   * that will write to the specified subordinate <code>OutputStream</code>
+   * and which will use the specified buffer size
+   *
+   * @param out The underlying <code>OutputStream</code> to write data to
+   * @param size The size of the internal buffer
+   */
+  public BufferedOutputStream(OutputStream out, int size)
+  {
+    super(out);
+
+    buf = new byte[size];
+  }
+
+  /**
+   * This method causes any currently buffered bytes to be immediately
+   * written to the underlying output stream.
+   *
+   * @exception IOException If an error occurs
+   */
+  public synchronized void flush() throws IOException
+  {
+    if (count == 0)
+      return;
+
+    out.write(buf, 0, count);
+    count = 0;
+    out.flush();
+  }
+
+  /**
+   * This method flushes any remaining buffered bytes then closes the 
+   * underlying output stream.  Any further attempts to write to this stream
+   * may throw an exception
+   *
+  public synchronized void close() throws IOException
+  {
+    flush();
+    out.close();
+  }
+  */
+
+  /**
+   * This method runs when the object is garbage collected.  It is 
+   * responsible for ensuring that all buffered bytes are written and
+   * for closing the underlying stream.
+   *
+   * @exception IOException If an error occurs (ignored by the Java runtime)
+   *
+  protected void finalize() throws IOException
+  {
+    close();
+  }
+  */
+
+  /**
+   * This method writes a single byte of data.  This will be written to the
+   * buffer instead of the underlying data source.  However, if the buffer
+   * is filled as a result of this write request, it will be flushed to the
+   * underlying output stream.
+   *
+   * @param b The byte of data to be written, passed as an int
+   *
+   * @exception IOException If an error occurs
+   */
+  public synchronized void write(int b) throws IOException
+  {
+    if (count == buf.length)
+      flush();
+
+    buf[count] = (byte)(b & 0xFF);
+    ++count;
+  }
+
+  /**
+   * This method writes <code>len</code> bytes from the byte array 
+   * <code>buf</code> starting at position <code>offset</code> in the buffer. 
+   * These bytes will be written to the internal buffer.  However, if this
+   * write operation fills the buffer, the buffer will be flushed to the
+   * underlying output stream.
+   *
+   * @param buf The array of bytes to write.
+   * @param offset The index into the byte array to start writing from.
+   * @param len The number of bytes to write.
+   *
+   * @exception IOException If an error occurs
+   */
+  public synchronized void write(byte[] buf, int offset, int len) 
+    throws IOException
+  {
+    // Buffer can hold everything.  Note that the case where LEN < 0
+    // is automatically handled by the downstream write.
+    if (len < (this.buf.length - count))
+      {
+        System.arraycopy(buf, offset, this.buf, count, len);
+        count += len;
+      }
+    else
+      {
+        // The write was too big.  So flush the buffer and write the new
+        // bytes directly to the underlying stream, per the JDK 1.2
+        // docs.
+        flush();
+        out.write (buf, offset, len);
+      }
+  }
 
-}
+} // class BufferedOutputStream 
 
Index: kaffe/libraries/javalib/java/io/BufferedReader.java
diff -u kaffe/libraries/javalib/java/io/BufferedReader.java:1.11 kaffe/libraries/javalib/java/io/BufferedReader.java:1.12
--- kaffe/libraries/javalib/java/io/BufferedReader.java:1.11	Fri Jan  4 05:12:22 2002
+++ kaffe/libraries/javalib/java/io/BufferedReader.java	Sun Nov  2 13:29:36 2003
@@ -1,298 +1,571 @@
+/* BufferedReader.java
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 
+     Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.

*** Patch too long, truncated ***




More information about the kaffe mailing list