From: Tom Tromey Date: Tue, 20 Feb 2001 19:01:55 +0000 (+0000) Subject: PipedWriter.java (flush): Throw exception if stream closed. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=39f90b7ce07ffa09df609ec724a22fe18606a668;p=gcc.git PipedWriter.java (flush): Throw exception if stream closed. * java/io/PipedWriter.java (flush): Throw exception if stream closed. * java/io/OutputStreamWriter.java (write): Throw exception if stream closed. (writeChars): Don't throw exception if stream closed. * java/io/CharArrayWriter.java (closed): New field. (close): Set it. (flush): Throw exception if stream closed. (reset): Synchronize on correct lock. Allow stream to be reopened. (toCharArray, toString, writeTo): Synchronize. (write): Throwe exception if stream closed. * java/io/BufferedWriter.java (close): Clear `buffer'. (flush): Throw IOException if stream is closed. (write): Likewise. From-SVN: r39927 --- diff --git a/libjava/ChangeLog b/libjava/ChangeLog index f179ab0e81c..2693f7404b5 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,21 @@ +2001-02-20 Tom Tromey + + * java/io/PipedWriter.java (flush): Throw exception if stream + closed. + * java/io/OutputStreamWriter.java (write): Throw exception if + stream closed. + (writeChars): Don't throw exception if stream closed. + * java/io/CharArrayWriter.java (closed): New field. + (close): Set it. + (flush): Throw exception if stream closed. + (reset): Synchronize on correct lock. Allow stream to be + reopened. + (toCharArray, toString, writeTo): Synchronize. + (write): Throwe exception if stream closed. + * java/io/BufferedWriter.java (close): Clear `buffer'. + (flush): Throw IOException if stream is closed. + (write): Likewise. + 2001-02-16 Tom Tromey * java/lang/ThreadGroup.java (activeCount): Only include threads diff --git a/libjava/java/io/BufferedWriter.java b/libjava/java/io/BufferedWriter.java index f31dc28a0d2..e99586121db 100644 --- a/libjava/java/io/BufferedWriter.java +++ b/libjava/java/io/BufferedWriter.java @@ -1,12 +1,29 @@ -// BufferedWriter.java - Filtered character output stream. +/* BufferedWriter.java -- Buffer output into large blocks before writing + Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. -/* Copyright (C) 1998, 1999, 2000 Free Software Foundation +This file is part of GNU Classpath. - This file is part of libgcj. +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. + +As a special exception, if you link this library with other files to +produce an executable, this library does not by itself cause the +resulting executable to be covered by the GNU General Public License. +This exception does not however invalidate any other reasons why the +executable file might be covered by the GNU General Public License. */ -This software is copyrighted work licensed under the terms of the -Libgcj License. Please consult the file "LIBGCJ_LICENSE" for -details. */ package java.io; @@ -67,8 +84,14 @@ public class BufferedWriter extends Writer */ public void close () throws IOException { - localFlush (); - out.close(); + synchronized (lock) + { + // It is safe to call localFlush even if the stream is already + // closed. + localFlush (); + out.close(); + buffer = null; + } } /** @@ -79,8 +102,13 @@ public class BufferedWriter extends Writer */ public void flush () throws IOException { - localFlush (); - out.flush(); + synchronized (lock) + { + if (buffer == null) + throw new IOException ("Stream closed"); + localFlush (); + out.flush(); + } } /** @@ -109,6 +137,8 @@ public class BufferedWriter extends Writer { synchronized (lock) { + if (buffer == null) + throw new IOException ("Stream closed"); buffer[count++] = (char) oneChar; if (count == buffer.length) localFlush (); @@ -135,6 +165,9 @@ public class BufferedWriter extends Writer synchronized (lock) { + if (buffer == null) + throw new IOException ("Stream closed"); + // Bypass buffering if there is too much incoming data. if (count + len > buffer.length) { @@ -171,6 +204,9 @@ public class BufferedWriter extends Writer synchronized (lock) { + if (buffer == null) + throw new IOException ("Stream closed"); + if (count + len > buffer.length) { localFlush (); diff --git a/libjava/java/io/CharArrayWriter.java b/libjava/java/io/CharArrayWriter.java index 5e04d6171b1..7bec5558a01 100644 --- a/libjava/java/io/CharArrayWriter.java +++ b/libjava/java/io/CharArrayWriter.java @@ -1,6 +1,6 @@ // CharArrayWriter.java - Character array output stream. -/* Copyright (C) 1998, 1999 Free Software Foundation +/* Copyright (C) 1998, 1999, 2001 Free Software Foundation This file is part of libgcj. @@ -35,18 +35,27 @@ public class CharArrayWriter extends Writer public void close () { - // JCL says this does nothing. This seems to violate the Writer - // contract, in that other methods should still throw and - // IOException after a close. Still, we just follow JCL. + closed = true; } - public void flush () + public void flush () throws IOException { + synchronized (lock) + { + if (closed) + throw new IOException ("Stream closed"); + } } - public synchronized void reset () + public void reset () { - count = 0; + synchronized (lock) + { + count = 0; + // Allow this to reopen the stream. + // FIXME - what does the JDK do? + closed = false; + } } public int size () @@ -56,29 +65,41 @@ public class CharArrayWriter extends Writer public char[] toCharArray () { - char[] nc = new char[count]; - System.arraycopy(buf, 0, nc, 0, count); - return nc; + synchronized (lock) + { + char[] nc = new char[count]; + System.arraycopy(buf, 0, nc, 0, count); + return nc; + } } public String toString () { - return new String (buf, 0, count); + synchronized (lock) + { + return new String (buf, 0, count); + } } - public void write (int oneChar) + public void write (int oneChar) throws IOException { synchronized (lock) { + if (closed) + throw new IOException ("Stream closed"); + resize (1); buf[count++] = (char) oneChar; } } - public void write (char[] buffer, int offset, int len) + public void write (char[] buffer, int offset, int len) throws IOException { synchronized (lock) { + if (closed) + throw new IOException ("Stream closed"); + if (len >= 0) resize (len); System.arraycopy(buffer, offset, buf, count, len); @@ -86,10 +107,13 @@ public class CharArrayWriter extends Writer } } - public void write (String str, int offset, int len) + public void write (String str, int offset, int len) throws IOException { synchronized (lock) { + if (closed) + throw new IOException ("Stream closed"); + if (len >= 0) resize (len); str.getChars(offset, offset + len, buf, count); @@ -99,7 +123,10 @@ public class CharArrayWriter extends Writer public void writeTo (Writer out) throws IOException { - out.write(buf, 0, count); + synchronized (lock) + { + out.write(buf, 0, count); + } } private final void resize (int len) @@ -119,4 +146,6 @@ public class CharArrayWriter extends Writer protected char[] buf; // Number of valid characters in buffer. protected int count; + // True if stream is closed. + private boolean closed; } diff --git a/libjava/java/io/OutputStreamWriter.java b/libjava/java/io/OutputStreamWriter.java index 41275985bea..527ff75c66b 100644 --- a/libjava/java/io/OutputStreamWriter.java +++ b/libjava/java/io/OutputStreamWriter.java @@ -86,6 +86,9 @@ public class OutputStreamWriter extends Writer { synchronized (lock) { + if (out == null) + throw new IOException("Stream closed"); + if (wcount > 0) { writeChars(work, 0, wcount); @@ -100,9 +103,6 @@ public class OutputStreamWriter extends Writer private void writeChars(char[] buf, int offset, int count) throws IOException { - if (out == null) - throw new IOException("Stream closed"); - while (count > 0) { // We must flush if out.count == out.buf.length. @@ -127,6 +127,9 @@ public class OutputStreamWriter extends Writer { synchronized (lock) { + if (out == null) + throw new IOException("Stream closed"); + if (work == null) work = new char[100]; int wlength = work.length; @@ -155,6 +158,9 @@ public class OutputStreamWriter extends Writer { synchronized (lock) { + if (out == null) + throw new IOException("Stream closed"); + if (work == null) work = new char[100]; if (wcount >= work.length) diff --git a/libjava/java/io/PipedWriter.java b/libjava/java/io/PipedWriter.java index a0a51d1d44b..ebcbde783ec 100644 --- a/libjava/java/io/PipedWriter.java +++ b/libjava/java/io/PipedWriter.java @@ -142,8 +142,10 @@ public class PipedWriter extends Writer * had read all available data. Thats not the case - this method * appears to be a no-op? */ - public void flush() + public void flush() throws IOException { + if (closed) + throw new IOException ("Pipe closed"); } /**