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

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

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • 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)