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.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.io.UnsupportedEncodingException;
28  import java.net.URL;
29  import java.nio.charset.Charset;
30  
31  import org.codehaus.plexus.util.xml.XmlStreamReader;
32  
33  /**
34   * Utility to create Readers from streams, with explicit encoding choice: platform default, XML, or specified.
35   *
36   * @author <a href="mailto:hboutemy@codehaus.org">Herve Boutemy</a>
37   * @see Charset
38   * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
39   * @version $Id$
40   * @since 1.4.3
41   */
42  public class ReaderFactory
43  {
44      /**
45       * ISO Latin Alphabet #1, also known as ISO-LATIN-1. Every implementation of the Java platform is required to
46       * support this character encoding.
47       * 
48       * @see Charset
49       */
50      public static final String ISO_8859_1 = "ISO-8859-1";
51  
52      /**
53       * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. Every
54       * implementation of the Java platform is required to support this character encoding.
55       * 
56       * @see Charset
57       */
58      public static final String US_ASCII = "US-ASCII";
59  
60      /**
61       * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either
62       * order accepted on input, big-endian used on output). Every implementation of the Java platform is required to
63       * support this character encoding.
64       * 
65       * @see Charset
66       */
67      public static final String UTF_16 = "UTF-16";
68  
69      /**
70       * Sixteen-bit Unicode Transformation Format, big-endian byte order. Every implementation of the Java platform is
71       * required to support this character encoding.
72       * 
73       * @see Charset
74       */
75      public static final String UTF_16BE = "UTF-16BE";
76  
77      /**
78       * Sixteen-bit Unicode Transformation Format, little-endian byte order. Every implementation of the Java platform is
79       * required to support this character encoding.
80       * 
81       * @see Charset
82       */
83      public static final String UTF_16LE = "UTF-16LE";
84  
85      /**
86       * Eight-bit Unicode Transformation Format. Every implementation of the Java platform is required to support this
87       * character encoding.
88       * 
89       * @see Charset
90       */
91      public static final String UTF_8 = "UTF-8";
92  
93      /**
94       * The <code>file.encoding</code> System Property.
95       */
96      public static final String FILE_ENCODING = System.getProperty( "file.encoding" );
97  
98      /**
99       * Create a new Reader with XML encoding detection rules.
100      *
101      * @param in not null input stream.
102      * @return an XML reader instance for the input stream.
103      * @throws IOException if any.
104      * @see XmlStreamReader
105      */
106     public static XmlStreamReader newXmlReader( InputStream in )
107         throws IOException
108     {
109         return new XmlStreamReader( in );
110     }
111 
112     /**
113      * Create a new Reader with XML encoding detection rules.
114      *
115      * @param file not null file.
116      * @return an XML reader instance for the input file.
117      * @throws IOException if any.
118      * @see XmlStreamReader
119      */
120     public static XmlStreamReader newXmlReader( File file )
121         throws IOException
122     {
123         return new XmlStreamReader( file );
124     }
125 
126     /**
127      * Create a new Reader with XML encoding detection rules.
128      *
129      * @param url not null url.
130      * @return an XML reader instance for the input url.
131      * @throws IOException if any.
132      * @see XmlStreamReader
133      */
134     public static XmlStreamReader newXmlReader( URL url )
135         throws IOException
136     {
137         return new XmlStreamReader( url );
138     }
139 
140     /**
141      * Create a new Reader with default platform encoding.
142      *
143      * @param in not null input stream.
144      * @return a reader instance for the input stream using the default platform charset.
145      * @see Charset#defaultCharset()
146      */
147     public static Reader newPlatformReader( InputStream in )
148     {
149         return new InputStreamReader( in );
150     }
151 
152     /**
153      * Create a new Reader with default platform encoding.
154      *
155      * @param file not null file.
156      * @return a reader instance for the input file using the default platform charset.
157      * @throws FileNotFoundException if any.
158      * @see Charset#defaultCharset()
159      */
160     public static Reader newPlatformReader( File file )
161         throws FileNotFoundException
162     {
163         return new FileReader( file );
164     }
165 
166     /**
167      * Create a new Reader with default platform encoding.
168      *
169      * @param url not null url.
170      * @return a reader instance for the input url using the default platform charset.
171      * @throws IOException if any.
172      * @see Charset#defaultCharset()
173      */
174     public static Reader newPlatformReader( URL url )
175         throws IOException
176     {
177         return new InputStreamReader( url.openStream() );
178     }
179 
180     /**
181      * Create a new Reader with specified encoding.
182      *
183      * @param in not null input stream.
184      * @param encoding not null supported encoding.
185      * @return a reader instance for the input stream using the given encoding.
186      * @throws UnsupportedEncodingException if any.
187      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
188      */
189     public static Reader newReader( InputStream in, String encoding )
190         throws UnsupportedEncodingException
191     {
192         return new InputStreamReader( in, encoding );
193     }
194 
195     /**
196      * Create a new Reader with specified encoding. Note that there is no buffering on this reader, which favours
197      * clients that read into large buffers (8K+).
198      *
199      * @param file not null file.
200      * @param encoding not null supported encoding.
201      * @return a reader instance for the input file using the given encoding.
202      * @throws FileNotFoundException if any.
203      * @throws UnsupportedEncodingException if any.
204      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
205      */
206     public static Reader newReader( File file, String encoding )
207         throws FileNotFoundException, UnsupportedEncodingException
208     {
209         return new InputStreamReader( new FileInputStream( file ), encoding );
210     }
211 
212     /**
213      * Create a new Reader with specified encoding.
214      *
215      * @param url not null url.
216      * @param encoding not null supported encoding.
217      * @return a reader instance for the input url using the given encoding.
218      * @throws IOException if any.
219      * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
220      */
221     public static Reader newReader( URL url, String encoding )
222         throws IOException
223     {
224         return new InputStreamReader( url.openStream(), encoding );
225     }
226 }