01: /*
02: * Copyright 2006-2007 The Scriptella Project Team.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package scriptella.driver.text;
17:
18: import java.io.IOException;
19: import java.io.InputStreamReader;
20: import java.io.OutputStreamWriter;
21: import java.io.Reader;
22: import java.io.UnsupportedEncodingException;
23: import java.io.Writer;
24:
25: /**
26: * Pre Java SE 6 adapters for {@link System#in} and {@link System#out}
27: * <p>TODO: Move this class to a spi.support.text package.
28: *
29: * @author Fyodor Kupolov
30: * @version 1.0
31: */
32: public class ConsoleAdapters {
33:
34: /**
35: * Returns the reader for System.in.
36: *
37: * @param charsetName charset name, can be null.
38: * @return System.in reader.
39: * @throws UnsupportedEncodingException if specified charset is unsupported
40: */
41: public static Reader getConsoleReader(String charsetName)
42: throws UnsupportedEncodingException {
43: return charsetName == null ? new SystemInReader()
44: : new SystemInReader(charsetName);
45: }
46:
47: /**
48: * Returns the writer for System.out.
49: *
50: * @param charsetName charset name, can be null.
51: * @return System.out writer.
52: * @throws UnsupportedEncodingException if specified charset is unsupported
53: */
54: public static Writer getConsoleWriter(String charsetName)
55: throws UnsupportedEncodingException {
56: return charsetName == null ? new SystemOutWriter()
57: : new SystemOutWriter(charsetName);
58: }
59:
60: /**
61: * A simple {@link java.io.Reader} adapter for {@link java.io.InputStream}.
62: */
63: static class SystemInReader extends InputStreamReader {
64:
65: public SystemInReader() {
66: super (System.in);
67: }
68:
69: public SystemInReader(String charsetName)
70: throws UnsupportedEncodingException {
71: super (System.in, charsetName);
72: }
73:
74: public void close() throws IOException {
75: }
76: }
77:
78: /**
79: * A simple {@link java.io.Writer} adapter for {@link java.io.PrintStream}.
80: */
81: static class SystemOutWriter extends OutputStreamWriter {
82:
83: public SystemOutWriter() throws UnsupportedEncodingException {
84: super (System.out);
85: }
86:
87: public SystemOutWriter(String charsetName)
88: throws UnsupportedEncodingException {
89: super (System.out, charsetName);
90: }
91:
92: public void close() throws IOException {
93: flush();
94: }
95: }
96: }
|