1bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook/* 2bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Licensed to the Apache Software Foundation (ASF) under one or more 3bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * contributor license agreements. See the NOTICE file distributed with 4bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * this work for additional information regarding copyright ownership. 5bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * The ASF licenses this file to You under the Apache License, Version 2.0 6bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * (the "License"); you may not use this file except in compliance with 7bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * the License. You may obtain a copy of the License at 8bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 9bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * http://www.apache.org/licenses/LICENSE-2.0 10bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 11bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Unless required by applicable law or agreed to in writing, software 12bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * distributed under the License is distributed on an "AS IS" BASIS, 13bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * See the License for the specific language governing permissions and 15bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * limitations under the License. 16bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 17bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookpackage org.apache.commons.io.output; 18bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 19bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.File; 20bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.FileOutputStream; 21bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.IOException; 22bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.OutputStream; 23bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.OutputStreamWriter; 24bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.io.Writer; 25bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.nio.charset.Charset; 26bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport java.nio.charset.CharsetEncoder; 27bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 28bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport org.apache.commons.io.FileUtils; 29bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport org.apache.commons.io.IOUtils; 30bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 31bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook/** 32bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Writer of files that allows the encoding to be set. 33bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <p> 34bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * This class provides a simple alternative to <code>FileWriter</code> 35bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * that allows an encoding to be set. Unfortunately, it cannot subclass 36bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <code>FileWriter</code>. 37bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <p> 38bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * By default, the file will be overwritten, but this may be changed to append. 39bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <p> 40bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * The encoding must be specified using either the name of the {@link Charset}, 41bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * the {@link Charset}, or a {@link CharsetEncoder}. If the default encoding 42bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * is required then use the {@link java.io.FileWriter} directly, rather than 43bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * this implementation. 44bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * <p> 45bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 46bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 47bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @since Commons IO 1.4 48bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @version $Id: FileWriterWithEncoding.java 611634 2008-01-13 20:35:00Z niallp $ 49bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 50bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookpublic class FileWriterWithEncoding extends Writer { 51bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook // Cannot extend ProxyWriter, as requires writer to be 52bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook // known when super() is called 53bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 54bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** The writer to decorate. */ 55bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook private final Writer out; 56bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 57bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 58bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 59bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 60bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param filename the name of the file to write to, not null 61bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 62bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file name or encoding is null 63bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 64bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 65bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(String filename, String encoding) throws IOException { 66bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(new File(filename), encoding, false); 67bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 68bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 69bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 70bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 71bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 72bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param filename the name of the file to write to, not null 73bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 74bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true if content should be appended, false to overwrite 75bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file name or encoding is null 76bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 77bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 78bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(String filename, String encoding, boolean append) throws IOException { 79bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(new File(filename), encoding, append); 80bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 81bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 82bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 83bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 84bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 85bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param filename the name of the file to write to, not null 86bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 87bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file name or encoding is null 88bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 89bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 90bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(String filename, Charset encoding) throws IOException { 91bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(new File(filename), encoding, false); 92bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 93bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 94bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 95bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 96bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 97bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param filename the name of the file to write to, not null 98bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 99bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true if content should be appended, false to overwrite 100bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file name or encoding is null 101bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 102bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 103bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(String filename, Charset encoding, boolean append) throws IOException { 104bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(new File(filename), encoding, append); 105bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 106bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 107bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 108bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 109bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 110bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param filename the name of the file to write to, not null 111bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 112bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file name or encoding is null 113bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 114bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 115bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(String filename, CharsetEncoder encoding) throws IOException { 116bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(new File(filename), encoding, false); 117bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 118bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 119bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 120bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 121bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 122bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param filename the name of the file to write to, not null 123bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 124bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true if content should be appended, false to overwrite 125bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file name or encoding is null 126bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 127bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 128bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(String filename, CharsetEncoder encoding, boolean append) throws IOException { 129bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(new File(filename), encoding, append); 130bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 131bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 132bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 133bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 134bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 135bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to write to, not null 136bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 137bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 138bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 139bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 140bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(File file, String encoding) throws IOException { 141bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(file, encoding, false); 142bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 143bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 144bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 145bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 146bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 147bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to write to, not null 148bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 149bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true if content should be appended, false to overwrite 150bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 151bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 152bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 153bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(File file, String encoding, boolean append) throws IOException { 154bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook super(); 155bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this.out = initWriter(file, encoding, append); 156bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 157bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 158bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 159bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 160bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 161bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to write to, not null 162bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 163bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 164bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 165bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 166bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(File file, Charset encoding) throws IOException { 167bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(file, encoding, false); 168bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 169bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 170bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 171bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 172bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 173bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to write to, not null 174bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 175bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true if content should be appended, false to overwrite 176bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 177bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 178bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 179bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(File file, Charset encoding, boolean append) throws IOException { 180bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook super(); 181bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this.out = initWriter(file, encoding, append); 182bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 183bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 184bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 185bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 186bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 187bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to write to, not null 188bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 189bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 190bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 191bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 192bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(File file, CharsetEncoder encoding) throws IOException { 193bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this(file, encoding, false); 194bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 195bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 196bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 197bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Constructs a FileWriterWithEncoding with a file encoding. 198bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 199bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to write to, not null 200bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use, not null 201bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true if content should be appended, false to overwrite 202bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 203bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException in case of an I/O error 204bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 205bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public FileWriterWithEncoding(File file, CharsetEncoder encoding, boolean append) throws IOException { 206bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook super(); 207bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook this.out = initWriter(file, encoding, append); 208bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 209bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 210bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook //----------------------------------------------------------------------- 211bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 212bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Initialise the wrapped file writer. 213bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Ensure that a cleanup occurs if the writer creation fails. 214bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * 215bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param file the file to be accessed 216bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param encoding the encoding to use - may be Charset, CharsetEncoder or String 217bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param append true to append 218bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @return the initialised writer 219bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws NullPointerException if the file or encoding is null 220bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an error occurs 221bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 222bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook private static Writer initWriter(File file, Object encoding, boolean append) throws IOException { 223bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook if (file == null) { 224bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook throw new NullPointerException("File is missing"); 225bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 226bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook if (encoding == null) { 227bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook throw new NullPointerException("Encoding is missing"); 228bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 229bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook boolean fileExistedAlready = file.exists(); 230bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook OutputStream stream = null; 231bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook Writer writer = null; 232bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook try { 233bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook stream = new FileOutputStream(file, append); 234bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook if (encoding instanceof Charset) { 235bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook writer = new OutputStreamWriter(stream, (Charset)encoding); 236bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } else if (encoding instanceof CharsetEncoder) { 237bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook writer = new OutputStreamWriter(stream, (CharsetEncoder)encoding); 238bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } else { 239bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook writer = new OutputStreamWriter(stream, (String)encoding); 240bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 241bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } catch (IOException ex) { 242bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook IOUtils.closeQuietly(writer); 243bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook IOUtils.closeQuietly(stream); 244bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook if (fileExistedAlready == false) { 245bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook FileUtils.deleteQuietly(file); 246bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 247bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook throw ex; 248bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } catch (RuntimeException ex) { 249bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook IOUtils.closeQuietly(writer); 250bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook IOUtils.closeQuietly(stream); 251bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook if (fileExistedAlready == false) { 252bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook FileUtils.deleteQuietly(file); 253bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 254bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook throw ex; 255bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 256bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook return writer; 257bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 258bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 259bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook //----------------------------------------------------------------------- 260bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 261bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Write a character. 262bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param idx the character to write 263bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 264bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 265bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void write(int idx) throws IOException { 266bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.write(idx); 267bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 268bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 269bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 270bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Write the characters from an array. 271bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param chr the characters to write 272bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 273bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 274bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void write(char[] chr) throws IOException { 275bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.write(chr); 276bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 277bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 278bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 279bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Write the specified characters from an array. 280bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param chr the characters to write 281bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param st The start offset 282bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param end The number of characters to write 283bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 284bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 285bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void write(char[] chr, int st, int end) throws IOException { 286bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.write(chr, st, end); 287bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 288bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 289bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 290bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Write the characters from a string. 291bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param str the string to write 292bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 293bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 294bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void write(String str) throws IOException { 295bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.write(str); 296bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 297bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 298bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 299bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Write the specified characters from a string. 300bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param str the string to write 301bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param st The start offset 302bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @param end The number of characters to write 303bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 304bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 305bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void write(String str, int st, int end) throws IOException { 306bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.write(str, st, end); 307bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 308bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 309bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 310bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Flush the stream. 311bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 312bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 313bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void flush() throws IOException { 314bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.flush(); 315bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 316bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook 317bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook /** 318bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * Close the stream. 319bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook * @throws IOException if an I/O error occurs 320bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook */ 321bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void close() throws IOException { 322bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook out.close(); 323bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook } 324bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook} 325