source: branches/GNU/src/gcc/libjava/java/io/OutputStreamWriter.java@ 1391

Last change on this file since 1391 was 1391, checked in by bird, 22 years ago

GCC v3.3.3 sources.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.0 KB
Line 
1/* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation
2
3 This file is part of libgcj.
4
5This software is copyrighted work licensed under the terms of the
6Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7details. */
8
9package java.io;
10import gnu.gcj.convert.UnicodeToBytes;
11
12/**
13 * @author Per Bothner <[email protected]>
14 * @date April 17, 1998.
15 */
16/* Written using "Java Class Libraries", 2nd edition, plus online
17 * API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct, but only supports 8859_1.
19 */
20
21public class OutputStreamWriter extends Writer
22{
23 BufferedOutputStream out;
24
25 UnicodeToBytes converter;
26
27 /* Temporary buffer. */
28 private char[] work;
29 private int wcount;
30
31 public String getEncoding()
32 {
33 return out != null ? converter.getName() : null;
34 }
35
36 private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
37 {
38 this.out = out instanceof BufferedOutputStream
39 ? (BufferedOutputStream) out
40 : new BufferedOutputStream(out, 250);
41 /* Don't need to call super(out) here as long as the lock gets set. */
42 this.lock = out;
43 this.converter = encoder;
44 }
45
46 public OutputStreamWriter(OutputStream out, String enc)
47 throws UnsupportedEncodingException
48 {
49 this(out, UnicodeToBytes.getEncoder(enc));
50 }
51
52 public OutputStreamWriter(OutputStream out)
53 {
54 this(out, UnicodeToBytes.getDefaultEncoder());
55 }
56
57 public void close() throws IOException
58 {
59 synchronized (lock)
60 {
61 if (out != null)
62 {
63 flush();
64 out.close();
65 out = null;
66 }
67 work = null;
68 }
69 }
70
71 public void flush() throws IOException
72 {
73 synchronized (lock)
74 {
75 if (out == null)
76 throw new IOException("Stream closed");
77
78 if (wcount > 0)
79 {
80 writeChars(work, 0, wcount);
81 wcount = 0;
82 }
83 out.flush();
84 }
85 }
86
87 public void write(char[] buf, int offset, int count)
88 throws IOException
89 {
90 synchronized (lock)
91 {
92 if (out == null)
93 throw new IOException("Stream closed");
94
95 if (wcount > 0)
96 {
97 writeChars(work, 0, wcount);
98 wcount = 0;
99 }
100 writeChars(buf, offset, count);
101 }
102 }
103
104 /** Writes characters through to the inferior BufferedOutputStream.
105 * Ignores wcount and the work buffer. */
106 private void writeChars(char[] buf, int offset, int count)
107 throws IOException
108 {
109 while (count > 0 || converter.havePendingBytes())
110 {
111 // We must flush if out.count == out.buf.length.
112 // It is probably a good idea to flush if out.buf is almost full.
113 // This test is an approximation for "almost full".
114 if (out.count + count >= out.buf.length)
115 {
116 out.flush();
117 if (out.count != 0)
118 throw new IOException("unable to flush output byte buffer");
119 }
120 converter.setOutput(out.buf, out.count);
121 int converted = converter.write(buf, offset, count);
122 // Flush if we cannot make progress.
123 if (converted == 0 && out.count == converter.count)
124 {
125 out.flush();
126 if (out.count != 0)
127 throw new IOException("unable to flush output byte buffer");
128 }
129 offset += converted;
130 count -= converted;
131 out.count = converter.count;
132 }
133 }
134
135 public void write(String str, int offset, int count)
136 throws IOException
137 {
138 synchronized (lock)
139 {
140 if (out == null)
141 throw new IOException("Stream closed");
142
143 if (work == null)
144 work = new char[100];
145 int wlength = work.length;
146 while (count > 0)
147 {
148 int size = count;
149 if (wcount + size > wlength)
150 {
151 if (2*wcount > wlength)
152 {
153 writeChars(work, 0, wcount);
154 wcount = 0;
155 }
156 if (wcount + size > wlength)
157 size = wlength - wcount;
158 }
159 str.getChars(offset, offset+size, work, wcount);
160 offset += size;
161 count -= size;
162 wcount += size;
163 }
164 }
165 }
166
167 public void write(int ch) throws IOException
168 {
169 synchronized (lock)
170 {
171 if (out == null)
172 throw new IOException("Stream closed");
173
174 if (work == null)
175 work = new char[100];
176 if (wcount >= work.length)
177 {
178 writeChars(work, 0, wcount);
179 wcount = 0;
180 }
181 work[wcount++] = (char) ch;
182 }
183 }
184}
Note: See TracBrowser for help on using the repository browser.