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