OSDN Git Service

85aceed68bdc0b6dabbb5a16841f81841ff5d51e
[pf3gnuchains/gcc-fork.git] / libjava / java / io / BufferedWriter.java
1 // BufferedWriter.java - Filtered character output stream.
2
3 /* Copyright (C) 1998, 1999  Cygnus Solutions
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.io;
12
13 /**
14  * @author Tom Tromey <tromey@cygnus.com>
15  * @date September 25, 1998 
16  */
17
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19  * "The Java Language Specification", ISBN 0-201-63451-1
20  * Status:  Complete to version 1.1.
21  */
22
23 // Why not extend FilterWriter?
24 public class BufferedWriter extends Writer
25 {
26   public BufferedWriter (Writer out)
27   {
28     this (out, 8192);
29   }
30
31   public BufferedWriter (Writer ox, int size)
32   {
33     super (ox);
34     out = ox;
35     buffer = new char[size];
36     count = 0;
37   }
38
39   public void close () throws IOException
40   {
41     localFlush ();
42     out.close();
43   }
44
45   public void flush () throws IOException
46   {
47     localFlush ();
48     out.flush();
49   }
50
51   public void newLine () throws IOException
52   {
53     write (System.getProperty("line.separator"));
54   }
55
56   public void write (int oneChar) throws IOException
57   {
58     synchronized (lock)
59       {
60         buffer[count++] = (char) oneChar;
61         if (count == buffer.length)
62           localFlush ();
63       }
64   }
65
66   public void write (char[] buf, int offset, int len) throws IOException
67   {
68     if (offset < 0 || len < 0 || offset + len > buf.length)
69       throw new ArrayIndexOutOfBoundsException ();
70
71     synchronized (lock)
72       {
73         // Bypass buffering if there is too much incoming data.
74         if (count + len > buffer.length)
75           {
76             localFlush ();
77             out.write(buf, offset, len);
78           }
79         else
80           {
81             System.arraycopy(buf, offset, buffer, count, len);
82             count += len;
83             if (count == buffer.length)
84               localFlush ();
85           }
86       }
87   }
88
89   public void write (String str, int offset, int len) throws IOException
90   {
91     if (offset < 0 || len < 0 || offset + len < str.length())
92       throw new ArrayIndexOutOfBoundsException ();
93
94     synchronized (lock)
95       {
96         if (count + len > buffer.length)
97           {
98             localFlush ();
99             out.write(str, offset, len);
100           }
101         else
102           {
103             str.getChars(offset, offset + len, buffer, count);
104             count += len;
105             if (count == buffer.length)
106               localFlush ();
107           }
108       }
109   }
110
111   private final void localFlush () throws IOException
112   {
113     if (count > 0)
114       {
115         synchronized (lock)
116           {
117             out.write(buffer, 0, count);
118             count = 0;
119           }
120       }
121   }
122
123   // The downstream writer.
124   private Writer out;
125   // The character buffer.
126   char[] buffer;
127   // Number of valid chars in buffer.
128   int count;
129 }