source: trunk/src/gcc/libjava/java/io/OutputStreamWriter.java@ 154

Last change on this file since 154 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.8 KB
Line 
1/* Copyright (C) 1998, 1999, 2000, 2001 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() { return converter.getName(); }
32
33 private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
34 {
35 this.out = out instanceof BufferedOutputStream
36 ? (BufferedOutputStream) out
37 : new BufferedOutputStream(out, 250);
38 /* Don't need to call super(out) here as long as the lock gets set. */
39 this.lock = out;
40 this.converter = encoder;
41 }
42
43 public OutputStreamWriter(OutputStream out, String enc)
44 throws UnsupportedEncodingException
45 {
46 this(out, UnicodeToBytes.getEncoder(enc));
47 }
48
49 public OutputStreamWriter(OutputStream out)
50 {
51 this(out, UnicodeToBytes.getDefaultEncoder());
52 }
53
54 public void close() throws IOException
55 {
56 synchronized (lock)
57 {
58 if (out != null)
59 {
60 flush();
61 out.close();
62 out = null;
63 }
64 work = null;
65 }
66 }
67
68 public void flush() throws IOException
69 {
70 synchronized (lock)
71 {
72 if (out == null)
73 throw new IOException("Stream closed");
74
75 if (wcount > 0)
76 {
77 writeChars(work, 0, wcount);
78 wcount = 0;
79 }
80 out.flush();
81 }
82 }
83
84 public void write(char[] buf, int offset, int count)
85 throws IOException
86 {
87 synchronized (lock)
88 {
89 if (out == null)
90 throw new IOException("Stream closed");
91
92 if (wcount > 0)
93 {
94 writeChars(work, 0, wcount);
95 wcount = 0;
96 }
97 writeChars(buf, offset, count);
98 }
99 }
100
101 /** Writes characters through to the inferior BufferedOutputStream.
102 * Ignores wcount and the work buffer. */
103 private void writeChars(char[] buf, int offset, int count)
104 throws IOException
105 {
106 while (count > 0)
107 {
108 // We must flush if out.count == out.buf.length.
109 // It is probably a good idea to flush if out.buf is almost full.
110 // This test is an approximation for "almost full".
111 if (out.count + count >= out.buf.length)
112 {
113 out.flush();
114 if (out.count != 0)
115 throw new IOException("unable to flush output byte buffer");
116 }
117 converter.setOutput(out.buf, out.count);
118 int converted = converter.write(buf, offset, count);
119 offset += converted;
120 count -= converted;
121 out.count = converter.count;
122 }
123 }
124
125 public void write(String str, int offset, int count)
126 throws IOException
127 {
128 synchronized (lock)
129 {
130 if (out == null)
131 throw new IOException("Stream closed");
132
133 if (work == null)
134 work = new char[100];
135 int wlength = work.length;
136 while (count > 0)
137 {
138 int size = count;
139 if (wcount + size > wlength)
140 {
141 if (2*wcount > wlength)
142 {
143 writeChars(work, 0, wcount);
144 wcount = 0;
145 }
146 if (wcount + size > wlength)
147 size = wlength - wcount;
148 }
149 str.getChars(offset, offset+size, work, wcount);
150 offset += size;
151 count -= size;
152 wcount += size;
153 }
154 }
155 }
156
157 public void write(int ch) throws IOException
158 {
159 synchronized (lock)
160 {
161 if (out == null)
162 throw new IOException("Stream closed");
163
164 if (work == null)
165 work = new char[100];
166 if (wcount >= work.length)
167 {
168 writeChars(work, 0, wcount);
169 wcount = 0;
170 }
171 work[wcount++] = (char) ch;
172 }
173 }
174}
Note: See TracBrowser for help on using the repository browser.