42 |
import gnu.java.io.encode.Encoder; |
import gnu.java.io.encode.Encoder; |
43 |
|
|
44 |
/** |
/** |
45 |
* This class writes characters to an output stream that is byte oriented |
* This class writes characters to an output stream that is byte oriented |
46 |
* It converts the chars that are written to bytes using an encoding layer, |
* It converts the chars that are written to bytes using an encoding layer, |
47 |
* which is specific to a particular encoding standard. The desired |
* which is specific to a particular encoding standard. The desired |
48 |
* encoding can either be specified by name, or if no encoding is specified, |
* encoding can either be specified by name, or if no encoding is specified, |
49 |
* the system default encoding will be used. The system default encoding |
* the system default encoding will be used. The system default encoding |
50 |
* name is determined from the system property <code>file.encoding</code>. |
* name is determined from the system property <code>file.encoding</code>. |
51 |
* The only encodings that are guaranteed to be available are "8859_1" |
* The only encodings that are guaranteed to be available are "8859_1" |
52 |
* (the Latin-1 character set) and "UTF8". Unfortunately, Java does not |
* (the Latin-1 character set) and "UTF8". Unfortunately, Java does not |
53 |
* provide a mechanism for listing the encodings that are supported in |
* provide a mechanism for listing the encodings that are supported in |
54 |
* a given implementation. |
* a given implementation. |
55 |
* <p> |
* <p> |
56 |
* Here is a list of standard encoding names that may be available: |
* Here is a list of standard encoding names that may be available: |
57 |
* <p> |
* <p> |
58 |
* <ul> |
* <ul> |
59 |
* <li>8859_1 (ISO-8859-1/Latin-1) |
* <li>8859_1 (ISO-8859-1/Latin-1) |
60 |
* <li>8859_2 (ISO-8859-2/Latin-2) |
* <li>8859_2 (ISO-8859-2/Latin-2) |
61 |
* <li>8859_3 (ISO-8859-3/Latin-3) |
* <li>8859_3 (ISO-8859-3/Latin-3) |
62 |
* <li>8859_4 (ISO-8859-4/Latin-4) |
* <li>8859_4 (ISO-8859-4/Latin-4) |
63 |
* <li>8859_5 (ISO-8859-5/Latin-5) |
* <li>8859_5 (ISO-8859-5/Latin-5) |
64 |
* <li>8859_6 (ISO-8859-6/Latin-6) |
* <li>8859_6 (ISO-8859-6/Latin-6) |
65 |
* <li>8859_7 (ISO-8859-7/Latin-7) |
* <li>8859_7 (ISO-8859-7/Latin-7) |
66 |
* <li>8859_8 (ISO-8859-8/Latin-8) |
* <li>8859_8 (ISO-8859-8/Latin-8) |
67 |
* <li>8859_9 (ISO-8859-9/Latin-9) |
* <li>8859_9 (ISO-8859-9/Latin-9) |
68 |
* <li>ASCII (7-bit ASCII) |
* <li>ASCII (7-bit ASCII) |
69 |
* <li>UTF8 (UCS Transformation Format-8) |
* <li>UTF8 (UCS Transformation Format-8) |
70 |
* <li>More Later |
* <li>More Later |
71 |
* </ul> |
* </ul> |
72 |
* |
* |
73 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
74 |
*/ |
*/ |
75 |
public class OutputStreamWriter extends Writer |
public class OutputStreamWriter extends Writer |
76 |
{ |
{ |
|
/** |
|
|
* This is the byte-character encoder class that does the writing and |
|
|
* translation of characters to bytes before writing to the underlying |
|
|
* class. |
|
|
*/ |
|
|
private Encoder out; |
|
|
|
|
|
/*************************************************************************/ |
|
77 |
|
|
78 |
/* |
/** |
79 |
* Constructors |
* This is the byte-character encoder class that does the writing and |
80 |
|
* translation of characters to bytes before writing to the underlying |
81 |
|
* class. |
82 |
*/ |
*/ |
83 |
|
private Encoder out; |
84 |
|
|
85 |
/** |
/** |
86 |
* This method initializes a new instance of <code>OutputStreamWriter</code> |
* This method initializes a new instance of <code>OutputStreamWriter</code> |
87 |
* to write to the specified stream using the default encoding. |
* to write to the specified stream using the default encoding. |
88 |
* |
* |
89 |
* @param out The <code>OutputStream</code> to write to |
* @param out The <code>OutputStream</code> to write to |
90 |
*/ |
*/ |
91 |
public OutputStreamWriter(OutputStream out) |
public OutputStreamWriter(OutputStream out) |
92 |
{ |
{ |
93 |
this.out = EncodingManager.getEncoder(out); |
this.out = EncodingManager.getEncoder(out); |
94 |
} |
} |
95 |
|
|
|
/*************************************************************************/ |
|
|
|
|
96 |
/** |
/** |
97 |
* This method initializes a new instance of <code>OutputStreamWriter</code> |
* This method initializes a new instance of <code>OutputStreamWriter</code> |
98 |
* to write to the specified stream using a caller supplied character |
* to write to the specified stream using a caller supplied character |
99 |
* encoding scheme. Note that due to a deficiency in the Java language |
* encoding scheme. Note that due to a deficiency in the Java language |
100 |
* design, there is no way to determine which encodings are supported. |
* design, there is no way to determine which encodings are supported. |
101 |
* |
* |
102 |
* @param out The <code>OutputStream</code> to write to |
* @param out The <code>OutputStream</code> to write to |
103 |
* @param encoding_scheme The name of the encoding scheme to use for |
* @param encoding_scheme The name of the encoding scheme to use for |
104 |
* character to byte translation |
* character to byte translation |
105 |
* |
* |
106 |
* @exception UnsupportedEncodingException If the named encoding is |
* @exception UnsupportedEncodingException If the named encoding is |
107 |
* not available. |
* not available. |
108 |
*/ |
*/ |
109 |
public OutputStreamWriter(OutputStream out, String encoding_scheme) |
public OutputStreamWriter(OutputStream out, String encoding_scheme) |
110 |
throws UnsupportedEncodingException |
throws UnsupportedEncodingException |
111 |
{ |
{ |
112 |
this.out = EncodingManager.getEncoder(out, encoding_scheme); |
this.out = EncodingManager.getEncoder(out, encoding_scheme); |
113 |
} |
} |
114 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
115 |
/** |
/** |
116 |
* This method closes this stream, and the underlying |
* This method closes this stream, and the underlying |
117 |
* <code>OutputStream</code> |
* <code>OutputStream</code> |
118 |
* |
* |
119 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
120 |
*/ |
*/ |
121 |
public void close() throws IOException |
public void close() throws IOException |
122 |
{ |
{ |
123 |
out.close(); |
out.close(); |
124 |
} |
} |
125 |
|
|
|
/*************************************************************************/ |
|
|
|
|
126 |
/** |
/** |
127 |
* This method flushes any buffered bytes to the underlying output sink. |
* This method flushes any buffered bytes to the underlying output sink. |
128 |
* |
* |
129 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
130 |
*/ |
*/ |
131 |
public void flush() throws IOException |
public void flush() throws IOException |
132 |
{ |
{ |
133 |
out.flush(); |
out.flush(); |
134 |
} |
} |
135 |
|
|
|
/*************************************************************************/ |
|
|
|
|
136 |
/** |
/** |
137 |
* This method returns the name of the character encoding scheme currently |
* This method returns the name of the character encoding scheme currently |
138 |
* in use by this stream. If the stream has been closed, then this method |
* in use by this stream. If the stream has been closed, then this method |
139 |
* may return <code>null</code>. |
* may return <code>null</code>. |
140 |
* |
* |
141 |
* @return The encoding scheme name |
* @return The encoding scheme name |
142 |
*/ |
*/ |
143 |
public String getEncoding() |
public String getEncoding() |
144 |
{ |
{ |
145 |
return(out.getSchemeName()); |
return(out.getSchemeName()); |
146 |
} |
} |
147 |
|
|
|
/*************************************************************************/ |
|
|
|
|
148 |
/** |
/** |
149 |
* This method writes a single character to the output stream. |
* This method writes a single character to the output stream. |
150 |
* |
* |
151 |
* @param c The char to write, passed as an int. |
* @param c The char to write, passed as an int. |
152 |
* |
* |
153 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
154 |
*/ |
*/ |
155 |
public void write(int c) throws IOException |
public void write(int c) throws IOException |
156 |
{ |
{ |
157 |
out.write(c); |
out.write(c); |
158 |
} |
} |
159 |
|
|
|
/*************************************************************************/ |
|
|
|
|
160 |
/** |
/** |
161 |
* This method writes <code>len</code> characters from the specified |
* This method writes <code>len</code> characters from the specified |
162 |
* array to the output stream starting at position <code>offset</code> |
* array to the output stream starting at position <code>offset</code> |
163 |
* into the array. |
* into the array. |
164 |
* |
* |
165 |
* @param buf The array of character to write from |
* @param buf The array of character to write from |
166 |
* @param offset The offset into the array to start writing chars from |
* @param offset The offset into the array to start writing chars from |
167 |
* @param len The number of chars to write. |
* @param len The number of chars to write. |
168 |
* |
* |
169 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
170 |
*/ |
*/ |
171 |
public void write(char[] buf, int offset, int len) throws IOException |
public void write(char[] buf, int offset, int len) throws IOException |
172 |
{ |
{ |
173 |
out.write(buf, offset, len); |
out.write(buf, offset, len); |
174 |
} |
} |
175 |
|
|
|
/*************************************************************************/ |
|
|
|
|
176 |
/** |
/** |
177 |
* This method writes <code>len</code> bytes from the specified |
* This method writes <code>len</code> bytes from the specified |
178 |
* <code>String</code> starting at position <code>offset</code> into the |
* <code>String</code> starting at position <code>offset</code> into the |
179 |
* <code>String</code>. |
* <code>String</code>. |
180 |
* |
* |
181 |
* @param str The <code>String</code> to write chars from |
* @param str The <code>String</code> to write chars from |
182 |
* @param offset The position in the <code>String</code> to start |
* @param offset The position in the <code>String</code> to start |
183 |
* writing chars from |
* writing chars from |
184 |
* @param len The number of chars to write |
* @param len The number of chars to write |
185 |
* |
* |
186 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
187 |
*/ |
*/ |
188 |
public void write(String str, int offset, int len) throws IOException |
public void write(String str, int offset, int len) throws IOException |
189 |
{ |
{ |
190 |
out.write(str, offset, len); |
out.write(str, offset, len); |