View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.io.Reader;
24  import java.io.UnsupportedEncodingException;
25  import java.net.URL;
26  import java.nio.charset.Charset;
27  import java.nio.file.Files;
28  
29  import org.codehaus.plexus.util.xml.XmlStreamReader;
30  
31  /**
32   * Utility to create Readers from streams, with explicit encoding choice: platform default, XML, or specified.
33   *
34   * @deprecated This class has been deprecated. When reading XML, users are encouraged to pass the {@code InputStream}
35   *             directory to the {@link org.codehaus.plexus.util.xml.pull.MXParser#setInput(InputStream, String)},
36   *             giving a {@code null} encoding to let the parser figure it out. For non xml usages, use the JDK
37   *             {@link Files} utility methods.
38   *
39   * @author <a href="mailto:hboutemy@codehaus.org">Herve Boutemy</a>
40   * @see Charset
41   * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
42   *
43   * @since 1.4.3
44   */
45  @Deprecated
46  public class ReaderFactory {
47      /**
48       * ISO Latin Alphabet #1, also known as ISO-LATIN-1. Every implementation of the Java platform is required to
49       * support this character encoding.
50       *
51       * @see Charset
52       */
53      public static final String ISO_8859_1 = "ISO-8859-1";
54  
55      /**
56       * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. Every
57       * implementation of the Java platform is required to support this character encoding.
58       *
59       * @see Charset
60       */
61      public static final String US_ASCII = "US-ASCII";
62  
63      /**
64       * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either
65       * order accepted on input, big-endian used on output). Every implementation of the Java platform is required to
66       * support this character encoding.
67       *
68       * @see Charset
69       */
70      public static final String UTF_16 = "UTF-16";
71  
72      /**
73       * Sixteen-bit Unicode Transformation Format, big-endian byte order. Every implementation of the Java platform is
74       * required to support this character encoding.
75       *
76       * @see Charset
77       */
78      public static final String UTF_16BE = "UTF-16BE";
79  
80      /**
81       * Sixteen-bit Unicode Transformation Format, little-endian byte order. Every implementation of the Java platform is
82       * required to support this character encoding.
83       *
84       * @see Charset
85       */
86      public static final String UTF_16LE = "UTF-16LE";
87  
88      /**
89       * Eight-bit Unicode Transformation Format. Every implementation of the Java platform is required to support this
90       * character encoding.
91       *
92       * @see Charset
93       */
94      public static final String UTF_8 = "UTF-8";
95  
96      /**
97       * The <code>file.encoding</code> System Property.
98       */
99      public static final String FILE_ENCODING = System.getProperty("file.encoding");
100 
101     /**
102      * Create a new Reader with XML encoding detection rules.
103      *
104      * @param in not null input stream.
105      * @return an XML reader instance for the input stream.
106      * @throws IOException if any.
107      * @see XmlStreamReader
108      */
109     public static XmlStreamReader newXmlReader(InputStream in) throws IOException {
110         return new XmlStreamReader(in);
111     }
112 
113     /**
114      * Create a new Reader with XML encoding detection rules.
115      *
116      * @param file not null file.
117      * @return an XML reader instance for the input file.
118      * @throws IOException if any.
119      * @see XmlStreamReader
120      */
121     public static XmlStreamReader newXmlReader(File file) throws IOException {
122         return new XmlStreamReader(file);
123     }
124 
125     /**
126      * Create a new Reader with XML encoding detection rules.
127      *
128      * @param url not null url.
129      * @return an XML reader instance for the input url.
130      * @throws IOException if any.
131      * @see XmlStreamReader
132      */
133     public static XmlStreamReader newXmlReader(URL url) throws IOException {
134         return new XmlStreamReader(url);
135     }
136 
137     /**
138      * Create a new Reader with default platform encoding.
139      *
140      * @param in not null input stream.
141      * @return a reader instance for the input stream using the default platform charset.
142      * @see Charset#defaultCharset()
143      */
144     public static Reader newPlatformReader(InputStream in) {
145         return new InputStreamReader(in);
146     }
147 
148     /**
149      * Create a new Reader with default platform encoding.
150      *
151      * @param file not null file.
152      * @return a reader instance for the input file using the default platform charset.
153      * @throws IOException if any.
154      * @see Charset#defaultCharset()
155      */
156     public static Reader newPlatformReader(File file) throws IOException {
157         return Files.newBufferedReader(file.toPath());
158     }
159 
160     /**
161      * Create a new Reader with default platform encoding.
162      *
163      * @param url not null url.
164      * @return a reader instance for the input url using the default platform charset.
165      * @throws IOException if any.
166      * @see Charset#defaultCharset()
167      */
168     public static Reader newPlatformReader(URL url) throws IOException {
169         return new InputStreamReader(url.openStream());
170     }
171 
172     /**
173      * Create a new Reader with specified encoding.
174      *
175      * @param in not null input stream.
176      * @param encoding not null supported encoding.
177      * @return a reader instance for the input stream using the given encoding.
178      * @throws UnsupportedEncodingException if any.
179      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
180      */
181     public static Reader newReader(InputStream in, String encoding) throws UnsupportedEncodingException {
182         return new InputStreamReader(in, encoding);
183     }
184 
185     /**
186      * Create a new Reader with specified encoding. Note that there is no buffering on this reader, which favours
187      * clients that read into large buffers (8K+).
188      *
189      * @param file not null file.
190      * @param encoding not null supported encoding.
191      * @return a reader instance for the input file using the given encoding.
192      * @throws IOException if any.
193      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
194      */
195     public static Reader newReader(File file, String encoding) throws IOException {
196         return new InputStreamReader(Files.newInputStream(file.toPath()), encoding);
197     }
198 
199     /**
200      * Create a new Reader with specified encoding.
201      *
202      * @param url not null url.
203      * @param encoding not null supported encoding.
204      * @return a reader instance for the input url using the given encoding.
205      * @throws IOException if any.
206      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
207      */
208     public static Reader newReader(URL url, String encoding) throws IOException {
209         return new InputStreamReader(url.openStream(), encoding);
210     }
211 }