[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
ByteArrayOutputStream.close
From: |
Tom Tromey |
Subject: |
ByteArrayOutputStream.close |
Date: |
16 Nov 2000 17:46:43 -0700 |
Consider this program:
import java.io.*;
public class foo
{
public static void main (String[] args)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
baos.write (23);
baos.close ();
baos.write (27);
System.out.println (baos.size ());
System.out.println (baos.toByteArray ().length);
}
catch (IOException _)
{
System.out.println (_);
}
}
}
What should it print?
With libgcj (and thus the shared-with-Classpath ByteArrayOutputStream)
it prints:
2
2
Sun's JDK 1.2.2 also prints this.
However the online JDK docs imply that the second write() should cause
an exception to be thrown. The docs for close() say:
Closes this output stream and releases any system resources
associated with this stream. A closed stream cannot perform output
operations and cannot be reopened.
Any thoughts on this?
The appended patch changes this to work the way I think it ought to
work. However, I'm reluctant to check this in. Maybe it is just a
bug in the docs. Or maybe I'm reading too much into the close()
documentation.
Tom
Index: java/io/ByteArrayOutputStream.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/io/ByteArrayOutputStream.java,v
retrieving revision 1.5
diff -u -r1.5 ByteArrayOutputStream.java
--- ByteArrayOutputStream.java 2000/08/19 19:54:36 1.5
+++ ByteArrayOutputStream.java 2000/11/17 00:39:11
@@ -57,6 +57,7 @@
public ByteArrayOutputStream ()
{
this (initial_buffer_size);
+ closed = false;
}
/**
@@ -69,6 +70,7 @@
{
buf = new byte[size];
count = 0;
+ closed = false;
}
/**
@@ -83,6 +85,16 @@
}
/**
+ * Close the output stream.
+ * Future attempts to write to this stream will cause exceptions.
+ * A closed stream cannot be reopened.
+ */
+ public void close () throws IOException
+ {
+ closed = true;
+ }
+
+ /**
* This method returns the number of bytes that have been written to
* the buffer so far. This is the same as the value of the protected
* <code>count</code> variable. If the <code>reset</code> method is
@@ -162,7 +174,7 @@
* @return A <code>String</code> containing the data written to this
* stream so far
*
- * @deprecrated
+ * @deprecated
*/
public String toString (int hibyte)
{
@@ -191,6 +203,8 @@
*/
public synchronized void write (int oneByte)
{
+ if (closed)
+ throw new IOException ("stream closed");
resize (1);
buf[count++] = (byte) oneByte;
}
@@ -206,6 +220,8 @@
*/
public synchronized void write (byte[] buffer, int offset, int add)
{
+ if (closed)
+ throw new IOException ("stream closed");
// If ADD < 0 then arraycopy will throw the appropriate error for
// us.
if (add >= 0)
@@ -236,6 +252,11 @@
* The number of bytes that have been written to the buffer
*/
protected int count;
+
+ /**
+ * Whether the stream has been closed.
+ */
+ private boolean closed;
/**
* The default initial buffer size. Specified by the JCL.
- ByteArrayOutputStream.close,
Tom Tromey <=