122 |
{ |
{ |
123 |
this.out = out; |
this.out = out; |
124 |
try |
try |
|
{ |
|
|
// Don't use NIO if avoidable |
|
|
if(EncodingHelper.isISOLatin1(encoding_scheme)) |
|
125 |
{ |
{ |
126 |
|
// Don't use NIO if avoidable |
127 |
|
if(EncodingHelper.isISOLatin1(encoding_scheme)) |
128 |
|
{ |
129 |
|
encodingName = "ISO8859_1"; |
130 |
|
encoder = null; |
131 |
|
return; |
132 |
|
} |
133 |
|
|
134 |
|
/* |
135 |
|
* Workraround for encodings with a byte-order-mark. |
136 |
|
* We only want to write it once per stream. |
137 |
|
*/ |
138 |
|
try |
139 |
|
{ |
140 |
|
if(encoding_scheme.equalsIgnoreCase("UnicodeBig") || |
141 |
|
encoding_scheme.equalsIgnoreCase("UTF-16") || |
142 |
|
encoding_scheme.equalsIgnoreCase("UTF16")) |
143 |
|
{ |
144 |
|
encoding_scheme = "UTF-16BE"; |
145 |
|
out.write((byte)0xFE); |
146 |
|
out.write((byte)0xFF); |
147 |
|
} |
148 |
|
else if(encoding_scheme.equalsIgnoreCase("UnicodeLittle")){ |
149 |
|
encoding_scheme = "UTF-16LE"; |
150 |
|
out.write((byte)0xFF); |
151 |
|
out.write((byte)0xFE); |
152 |
|
} |
153 |
|
} |
154 |
|
catch(IOException ioe) |
155 |
|
{ |
156 |
|
} |
157 |
|
|
158 |
|
outputBuffer = CharBuffer.allocate(BUFFER_SIZE); |
159 |
|
|
160 |
|
Charset cs = EncodingHelper.getCharset(encoding_scheme); |
161 |
|
if(cs == null) |
162 |
|
throw new UnsupportedEncodingException("Encoding "+encoding_scheme+ |
163 |
|
" unknown"); |
164 |
|
encoder = cs.newEncoder(); |
165 |
|
encodingName = EncodingHelper.getOldCanonical(cs.name()); |
166 |
|
|
167 |
|
encoder.onMalformedInput(CodingErrorAction.REPLACE); |
168 |
|
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); |
169 |
|
} |
170 |
|
catch(RuntimeException e) |
171 |
|
{ |
172 |
|
// Default to ISO Latin-1, will happen if this is called, for instance, |
173 |
|
// before the NIO provider is loadable. |
174 |
|
encoder = null; |
175 |
encodingName = "ISO8859_1"; |
encodingName = "ISO8859_1"; |
|
encoder = null; |
|
|
return; |
|
|
} |
|
|
|
|
|
/* |
|
|
* Workraround for encodings with a byte-order-mark. |
|
|
* We only want to write it once per stream. |
|
|
*/ |
|
|
try { |
|
|
if(encoding_scheme.equalsIgnoreCase("UnicodeBig") || |
|
|
encoding_scheme.equalsIgnoreCase("UTF-16") || |
|
|
encoding_scheme.equalsIgnoreCase("UTF16")) |
|
|
{ |
|
|
encoding_scheme = "UTF-16BE"; |
|
|
out.write((byte)0xFE); |
|
|
out.write((byte)0xFF); |
|
|
} else if(encoding_scheme.equalsIgnoreCase("UnicodeLittle")){ |
|
|
encoding_scheme = "UTF-16LE"; |
|
|
out.write((byte)0xFF); |
|
|
out.write((byte)0xFE); |
|
|
} |
|
|
} catch(IOException ioe){ |
|
176 |
} |
} |
|
|
|
|
outputBuffer = CharBuffer.allocate(BUFFER_SIZE); |
|
|
|
|
|
Charset cs = EncodingHelper.getCharset(encoding_scheme); |
|
|
if(cs == null) |
|
|
throw new UnsupportedEncodingException("Encoding "+encoding_scheme+ |
|
|
" unknown"); |
|
|
encoder = cs.newEncoder(); |
|
|
encodingName = EncodingHelper.getOldCanonical(cs.name()); |
|
|
|
|
|
encoder.onMalformedInput(CodingErrorAction.REPLACE); |
|
|
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); |
|
|
} catch(RuntimeException e) { |
|
|
// Default to ISO Latin-1, will happen if this is called, for instance, |
|
|
// before the NIO provider is loadable. |
|
|
encoder = null; |
|
|
encodingName = "ISO8859_1"; |
|
|
} |
|
177 |
} |
} |
178 |
|
|
179 |
/** |
/** |
187 |
this.out = out; |
this.out = out; |
188 |
outputBuffer = null; |
outputBuffer = null; |
189 |
try |
try |
190 |
{ |
{ |
191 |
String encoding = System.getProperty("file.encoding"); |
String encoding = System.getProperty("file.encoding"); |
192 |
Charset cs = Charset.forName(encoding); |
Charset cs = Charset.forName(encoding); |
193 |
encoder = cs.newEncoder(); |
encoder = cs.newEncoder(); |
194 |
encodingName = EncodingHelper.getOldCanonical(cs.name()); |
encodingName = EncodingHelper.getOldCanonical(cs.name()); |
195 |
} catch(RuntimeException e) { |
} |
196 |
encoder = null; |
catch(RuntimeException e) |
197 |
encodingName = "ISO8859_1"; |
{ |
198 |
} |
encoder = null; |
199 |
|
encodingName = "ISO8859_1"; |
200 |
|
} |
201 |
|
|
202 |
if(encoder != null) |
if(encoder != null) |
203 |
{ |
{ |
204 |
encoder.onMalformedInput(CodingErrorAction.REPLACE); |
encoder.onMalformedInput(CodingErrorAction.REPLACE); |
205 |
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); |
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); |
206 |
outputBuffer = CharBuffer.allocate(BUFFER_SIZE); |
outputBuffer = CharBuffer.allocate(BUFFER_SIZE); |
207 |
} |
} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* This method initializes a new instance of <code>OutputStreamWriter</code> |
212 |
|
* to write to the specified stream using a given <code>Charset</code>. |
213 |
|
* |
214 |
|
* @param out The <code>OutputStream</code> to write to |
215 |
|
* @param cs The <code>Charset</code> of the encoding to use |
216 |
|
*/ |
217 |
|
public OutputStreamWriter(OutputStream out, Charset cs) |
218 |
|
{ |
219 |
|
this.out = out; |
220 |
|
encoder = cs.newEncoder(); |
221 |
|
encoder.onMalformedInput(CodingErrorAction.REPLACE); |
222 |
|
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); |
223 |
|
outputBuffer = CharBuffer.allocate(BUFFER_SIZE); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
|
* This method initializes a new instance of <code>OutputStreamWriter</code> |
228 |
|
* to write to the specified stream using a given |
229 |
|
* <code>CharsetEncoder</code>. |
230 |
|
* |
231 |
|
* @param out The <code>OutputStream</code> to write to |
232 |
|
* @param enc The <code>CharsetEncoder</code> to encode the output with |
233 |
|
*/ |
234 |
|
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) |
235 |
|
{ |
236 |
|
this.out = out; |
237 |
|
encoder = enc; |
238 |
|
outputBuffer = CharBuffer.allocate(BUFFER_SIZE); |
239 |
} |
} |
240 |
|
|
241 |
/** |
/** |