01: /*
02: * :tabSize=8:indentSize=8:noTabs=false:
03: * :folding=explicit:collapseFolds=1:
04: *
05: * Copyright (C) 2007 Kazutoshi Satoda
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or any later version.
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: */
20:
21: package org.gjt.sp.jedit.io;
22:
23: //{{{ Imports
24: import java.io.InputStream;
25: import java.io.OutputStream;
26: import java.io.Reader;
27: import java.io.Writer;
28: import java.io.IOException;
29: import java.io.InputStreamReader;
30: import java.io.OutputStreamWriter;
31: import java.nio.charset.Charset;
32:
33: //}}}
34:
35: /**
36: * Encodings which are provided by java.nio.charset.Charset.
37: *
38: * @since 4.3pre10
39: * @author Kazutoshi Satoda
40: */
41: public class CharsetEncoding implements Encoding {
42: //{{{ Constructor
43: public CharsetEncoding(String name) {
44: body = Charset.forName(name);
45: } //}}}
46:
47: //{{{ implements Encoding
48: public Reader getTextReader(InputStream in) throws IOException {
49: // Pass the decoder explicitly to report a decode error
50: // as an exception instead of replacing with \xFFFD.
51: // The form "InputStreamReader(in, encoding)" seemed to use
52: // CodingErrorAction.REPLACE internally.
53: return new InputStreamReader(in, body.newDecoder());
54: }
55:
56: public Writer getTextWriter(OutputStream out) throws IOException {
57: // Pass the encoder explicitly because of same reason
58: // in getTextReader();
59: return new OutputStreamWriter(out, body.newEncoder());
60: }
61:
62: //}}}
63:
64: //{{{ Private members
65: private final Charset body;
66: //}}}
67: }
|