1 /*
2 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
70 * @see java.nio.charset.Charset
71 *
72 * @author Mark Reinhold
73 * @since 1.1
74 */
75
76 public class OutputStreamWriter extends Writer {
77
78 private final StreamEncoder se;
79
80 /**
81 * Creates an OutputStreamWriter that uses the named charset.
82 *
83 * @param out
84 * An OutputStream
85 *
86 * @param charsetName
87 * The name of a supported
88 * {@link java.nio.charset.Charset charset}
89 *
90 * @exception UnsupportedEncodingException
91 * If the named encoding is not supported
92 */
93 public OutputStreamWriter(OutputStream out, String charsetName)
94 throws UnsupportedEncodingException
95 {
96 super(out);
97 if (charsetName == null)
98 throw new NullPointerException("charsetName");
99 se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
100 }
101
102 /**
103 * Creates an OutputStreamWriter that uses the default character encoding.
104 *
105 * @param out An OutputStream
106 */
107 public OutputStreamWriter(OutputStream out) {
108 super(out);
109 try {
110 se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
170 *
171 * @revised 1.4
172 * @spec JSR-51
173 */
174 public String getEncoding() {
175 return se.getEncoding();
176 }
177
178 /**
179 * Flushes the output buffer to the underlying byte stream, without flushing
180 * the byte stream itself. This method is non-private only so that it may
181 * be invoked by PrintStream.
182 */
183 void flushBuffer() throws IOException {
184 se.flushBuffer();
185 }
186
187 /**
188 * Writes a single character.
189 *
190 * @exception IOException If an I/O error occurs
191 */
192 public void write(int c) throws IOException {
193 se.write(c);
194 }
195
196 /**
197 * Writes a portion of an array of characters.
198 *
199 * @param cbuf Buffer of characters
200 * @param off Offset from which to start writing characters
201 * @param len Number of characters to write
202 *
203 * @throws IndexOutOfBoundsException
204 * If {@code off} is negative, or {@code len} is negative,
205 * or {@code off + len} is negative or greater than the length
206 * of the given array
207 *
208 * @throws IOException If an I/O error occurs
209 */
210 public void write(char cbuf[], int off, int len) throws IOException {
231
232 @Override
233 public Writer append(CharSequence csq, int start, int end) throws IOException {
234 if (csq == null) csq = "null";
235 return append(csq.subSequence(start, end));
236 }
237
238 @Override
239 public Writer append(CharSequence csq) throws IOException {
240 if (csq instanceof CharBuffer) {
241 se.write((CharBuffer) csq);
242 } else {
243 se.write(String.valueOf(csq));
244 }
245 return this;
246 }
247
248 /**
249 * Flushes the stream.
250 *
251 * @exception IOException If an I/O error occurs
252 */
253 public void flush() throws IOException {
254 se.flush();
255 }
256
257 public void close() throws IOException {
258 se.close();
259 }
260 }
|
1 /*
2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
70 * @see java.nio.charset.Charset
71 *
72 * @author Mark Reinhold
73 * @since 1.1
74 */
75
76 public class OutputStreamWriter extends Writer {
77
78 private final StreamEncoder se;
79
80 /**
81 * Creates an OutputStreamWriter that uses the named charset.
82 *
83 * @param out
84 * An OutputStream
85 *
86 * @param charsetName
87 * The name of a supported
88 * {@link java.nio.charset.Charset charset}
89 *
90 * @throws UnsupportedEncodingException
91 * If the named encoding is not supported
92 */
93 public OutputStreamWriter(OutputStream out, String charsetName)
94 throws UnsupportedEncodingException
95 {
96 super(out);
97 if (charsetName == null)
98 throw new NullPointerException("charsetName");
99 se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
100 }
101
102 /**
103 * Creates an OutputStreamWriter that uses the default character encoding.
104 *
105 * @param out An OutputStream
106 */
107 public OutputStreamWriter(OutputStream out) {
108 super(out);
109 try {
110 se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
170 *
171 * @revised 1.4
172 * @spec JSR-51
173 */
174 public String getEncoding() {
175 return se.getEncoding();
176 }
177
178 /**
179 * Flushes the output buffer to the underlying byte stream, without flushing
180 * the byte stream itself. This method is non-private only so that it may
181 * be invoked by PrintStream.
182 */
183 void flushBuffer() throws IOException {
184 se.flushBuffer();
185 }
186
187 /**
188 * Writes a single character.
189 *
190 * @throws IOException If an I/O error occurs
191 */
192 public void write(int c) throws IOException {
193 se.write(c);
194 }
195
196 /**
197 * Writes a portion of an array of characters.
198 *
199 * @param cbuf Buffer of characters
200 * @param off Offset from which to start writing characters
201 * @param len Number of characters to write
202 *
203 * @throws IndexOutOfBoundsException
204 * If {@code off} is negative, or {@code len} is negative,
205 * or {@code off + len} is negative or greater than the length
206 * of the given array
207 *
208 * @throws IOException If an I/O error occurs
209 */
210 public void write(char cbuf[], int off, int len) throws IOException {
231
232 @Override
233 public Writer append(CharSequence csq, int start, int end) throws IOException {
234 if (csq == null) csq = "null";
235 return append(csq.subSequence(start, end));
236 }
237
238 @Override
239 public Writer append(CharSequence csq) throws IOException {
240 if (csq instanceof CharBuffer) {
241 se.write((CharBuffer) csq);
242 } else {
243 se.write(String.valueOf(csq));
244 }
245 return this;
246 }
247
248 /**
249 * Flushes the stream.
250 *
251 * @throws IOException If an I/O error occurs
252 */
253 public void flush() throws IOException {
254 se.flush();
255 }
256
257 public void close() throws IOException {
258 se.close();
259 }
260 }
|