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.OutputStream; 22 import java.io.OutputStreamWriter; 23 import java.io.UnsupportedEncodingException; 24 import java.io.Writer; 25 import java.nio.charset.Charset; 26 import java.nio.file.Files; 27 28 import org.codehaus.plexus.util.xml.XmlStreamWriter; 29 30 /** 31 * Utility to create Writers, with explicit encoding choice: platform default, XML, or specified. 32 * 33 * @deprecated This class has been deprecated. When writing XML, users can create the {@link XmlStreamWriter} instance 34 * directly. For other usages, using {@link Files} helper methods is recommended. 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 * 40 * @since 1.4.4 41 */ 42 @Deprecated 43 public class WriterFactory { 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 Writer with XML encoding detection rules. 100 * 101 * @param out not null output stream. 102 * @return an XML writer instance for the output stream. 103 * @throws IOException if any. 104 * @see XmlStreamWriter 105 */ 106 public static XmlStreamWriter newXmlWriter(OutputStream out) throws IOException { 107 return new XmlStreamWriter(out); 108 } 109 110 /** 111 * Create a new Writer with XML encoding detection rules. 112 * 113 * @param file not null file. 114 * @return an XML writer instance for the output file. 115 * @throws IOException if any. 116 * @see XmlStreamWriter 117 */ 118 public static XmlStreamWriter newXmlWriter(File file) throws IOException { 119 return new XmlStreamWriter(file); 120 } 121 122 /** 123 * Create a new Writer with default platform encoding. 124 * 125 * @param out not null output stream. 126 * @return a writer instance for the output stream using the default platform charset. 127 * @see Charset#defaultCharset() 128 */ 129 public static Writer newPlatformWriter(OutputStream out) { 130 return new OutputStreamWriter(out); 131 } 132 133 /** 134 * Create a new Writer with default platform encoding. 135 * 136 * @param file not null file. 137 * @return a writer instance for the output file using the default platform charset. 138 * @throws IOException if any. 139 * @see Charset#defaultCharset() 140 */ 141 public static Writer newPlatformWriter(File file) throws IOException { 142 return Files.newBufferedWriter(file.toPath()); 143 } 144 145 /** 146 * Create a new Writer with specified encoding. 147 * 148 * @param out not null output stream. 149 * @param encoding not null supported encoding. 150 * @return a writer instance for the output stream using the given encoding. 151 * @throws UnsupportedEncodingException if any. 152 * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a> 153 */ 154 public static Writer newWriter(OutputStream out, String encoding) throws UnsupportedEncodingException { 155 return new OutputStreamWriter(out, encoding); 156 } 157 158 /** 159 * Create a new Writer with specified encoding. 160 * 161 * @param file not null file. 162 * @param encoding not null supported encoding. 163 * @return a writer instance for the output file using the given encoding. 164 * @throws IOException if any. 165 * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a> 166 */ 167 public static Writer newWriter(File file, String encoding) throws IOException { 168 return newWriter(Files.newOutputStream(file.toPath()), encoding); 169 } 170 }