1 package org.apache.maven.shared.utils;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.io.UnsupportedEncodingException;
31 import java.net.URL;
32 import org.apache.commons.io.input.XmlStreamReader;
33
34 import javax.annotation.Nonnull;
35
36
37 /**
38 * Utility to create Readers from streams, with explicit encoding choice: platform default,
39 * XML, or specified.
40 *
41 * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
42 * @see java.nio.charset.Charset
43 * @see <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/intl/encoding.doc.html">Supported encodings</a>
44 */
45 public class ReaderFactory
46 {
47 /**
48 * ISO Latin Alphabet #1, also known as ISO-LATIN-1.
49 * Every implementation of the Java platform is required to support this character encoding.
50 *
51 * @deprecated use {@code java.nio.charset.StandardCharset.ISO_8859_1}
52 */
53 @Deprecated
54 public static final String ISO_8859_1 = "ISO-8859-1";
55
56 /**
57 * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
58 * Every implementation of the Java platform is required to support this character encoding.
59 *
60 * @deprecated use {@code java.nio.charset.StandardCharset.US_ASCII}
61 */
62 @Deprecated
63 public static final String US_ASCII = "US-ASCII";
64
65 /**
66 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either
67 * order accepted on input, big-endian used on output).
68 * Every implementation of the Java platform is required to support this character encoding.
69 *
70 * @deprecated use {@code java.nio.charset.StandardCharset.UTF_16}
71 */
72 @Deprecated
73 public static final String UTF_16 = "UTF-16";
74
75 /**
76 * Sixteen-bit Unicode Transformation Format, big-endian byte order.
77 * Every implementation of the Java platform is required to support this character encoding.
78 *
79 * @deprecated use {@code java.nio.charset.StandardCharset.UTF_16BE}
80 */
81 @Deprecated
82 public static final String UTF_16BE = "UTF-16BE";
83
84 /**
85 * Sixteen-bit Unicode Transformation Format, little-endian byte order.
86 * Every implementation of the Java platform is required to support this character encoding.
87 *
88 * @deprecated use {@code java.nio.charset.StandardCharset.UTF_16LE}
89 */
90 @Deprecated
91 public static final String UTF_16LE = "UTF-16LE";
92
93 /**
94 * Eight-bit Unicode Transformation Format.
95 * Every implementation of the Java platform is required to support this character encoding.
96 *
97 * @deprecated use {@code java.nio.charset.StandardCharset.UTF_8}
98 */
99 @Deprecated
100 public static final String UTF_8 = "UTF-8";
101
102 /**
103 * The <code>file.encoding</code> System Property.
104 */
105 public static final String FILE_ENCODING = System.getProperty( "file.encoding" );
106
107 /**
108 * Create a new Reader with XML encoding detection rules.
109 *
110 * @param in not null input stream
111 * @return an XML reader instance for the input stream
112 * @throws IOException if any
113 * @deprecated use org.apache.commons.io.input.XmlStreamReader instead
114 */
115 @Deprecated
116 public static Reader newXmlReader( @Nonnull InputStream in )
117 throws IOException
118 {
119 return new XmlStreamReader( in );
120 }
121
122 /**
123 * Create a new Reader with XML encoding detection rules.
124 *
125 * @param file not null file
126 * @return an XML reader instance for the input file
127 * @throws IOException if any
128 * @deprecated use org.apache.commons.io.input.XmlStreamReader instead
129 */
130 @Deprecated
131 public static Reader newXmlReader( @Nonnull File file )
132 throws IOException
133 {
134 return new XmlStreamReader( file );
135 }
136
137 /**
138 * Create a new Reader with XML encoding detection rules.
139 *
140 * @param url not null URL
141 * @return an XML reader instance for the input URL
142 * @throws IOException if any
143 * @deprecated use {@code org.apache.commons.io.input.XmlStreamReader} instead
144 */
145 @Deprecated
146 public static Reader newXmlReader( @Nonnull URL url )
147 throws IOException
148 {
149 return new XmlStreamReader( url );
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 character set
157 * @throws FileNotFoundException if any
158 * @see java.nio.charset.Charset#defaultCharset()
159 * @deprecated always specify an encoding. Do not depend on the default platform character set.
160 */
161 @Deprecated
162 public static Reader newPlatformReader( @Nonnull File file )
163 throws FileNotFoundException
164 {
165 return new FileReader( file );
166 }
167
168 /**
169 * Create a new Reader with specified encoding.
170 *
171 * @param in not null input stream
172 * @param encoding not null supported encoding
173 * @return a reader instance for the input stream using the given encoding
174 * @throws UnsupportedEncodingException if any
175 * @see <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/intl/encoding.doc.html">Supported
176 * encodings</a>
177 */
178 public static Reader newReader( @Nonnull InputStream in, @Nonnull String encoding )
179 throws UnsupportedEncodingException
180 {
181 return new InputStreamReader( in, encoding );
182 }
183
184 /**
185 * Create a new Reader with specified encoding.
186 *
187 * @param file not null file
188 * @param encoding not null supported encoding
189 * @return a reader instance for the input file using the given encoding
190 * @throws FileNotFoundException if any
191 * @throws UnsupportedEncodingException if any
192 * @see <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/intl/encoding.doc.html">Supported
193 * encodings</a>
194 */
195 public static Reader newReader( @Nonnull File file, @Nonnull String encoding )
196 throws FileNotFoundException, UnsupportedEncodingException
197 {
198 return new InputStreamReader( new FileInputStream( file ), encoding );
199 }
200
201 /**
202 * Create a new Reader with specified encoding.
203 *
204 * @param url not null URL
205 * @param encoding not null supported encoding
206 * @return a reader instance for the input URL using the given encoding
207 * @throws IOException if any
208 * @see <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/intl/encoding.doc.html">Supported
209 * encodings</a>
210 */
211 public static Reader newReader( @Nonnull URL url, @Nonnull String encoding )
212 throws IOException
213 {
214 return new InputStreamReader( url.openStream(), encoding );
215 }
216 }