14ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira/* 24ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Licensed to the Apache Software Foundation (ASF) under one or more 34ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * contributor license agreements. See the NOTICE file distributed with 44ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * this work for additional information regarding copyright ownership. 54ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * The ASF licenses this file to You under the Apache License, Version 2.0 64ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * (the "License"); you may not use this file except in compliance with 74ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * the License. You may obtain a copy of the License at 84ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 94ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * http://www.apache.org/licenses/LICENSE-2.0 104ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 114ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Unless required by applicable law or agreed to in writing, software 124ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * distributed under the License is distributed on an "AS IS" BASIS, 134ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 144ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * See the License for the specific language governing permissions and 154ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * limitations under the License. 164ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 174ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereirapackage org.apache.commons.io.output; 184ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 194ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.io.File; 204ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.io.FileOutputStream; 214ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.io.IOException; 224ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.io.OutputStream; 234ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.io.OutputStreamWriter; 244ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.io.Writer; 254ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.nio.charset.Charset; 264ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport java.nio.charset.CharsetEncoder; 274ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 284ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport org.apache.commons.io.FileUtils; 294ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereiraimport org.apache.commons.io.IOUtils; 304ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 314ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira/** 324ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Writer of files that allows the encoding to be set. 334ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * <p> 344ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * This class provides a simple alternative to <code>FileWriter</code> 354ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * that allows an encoding to be set. Unfortunately, it cannot subclass 364ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * <code>FileWriter</code>. 374ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * <p> 384ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * By default, the file will be overwritten, but this may be changed to append. 394ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * <p> 404ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * The encoding must be specified using either the name of the {@link Charset}, 414ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * the {@link Charset}, or a {@link CharsetEncoder}. If the default encoding 424ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * is required then use the {@link java.io.FileWriter} directly, rather than 434ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * this implementation. 444ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * <p> 454ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 464ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 474ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @since Commons IO 1.4 484ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @version $Id: FileWriterWithEncoding.java 611634 2008-01-13 20:35:00Z niallp $ 494ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 504ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereirapublic class FileWriterWithEncoding extends Writer { 514ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira // Cannot extend ProxyWriter, as requires writer to be 524ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira // known when super() is called 534ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 544ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** The writer to decorate. */ 554ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira private final Writer out; 564ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 574ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 584ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 594ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 604ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param filename the name of the file to write to, not null 614ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 624ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file name or encoding is null 634ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 644ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 654ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(String filename, String encoding) throws IOException { 664ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(new File(filename), encoding, false); 674ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 684ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 694ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 704ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 714ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 724ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param filename the name of the file to write to, not null 734ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 744ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true if content should be appended, false to overwrite 754ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file name or encoding is null 764ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 774ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 784ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(String filename, String encoding, boolean append) throws IOException { 794ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(new File(filename), encoding, append); 804ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 814ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 824ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 834ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 844ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 854ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param filename the name of the file to write to, not null 864ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 874ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file name or encoding is null 884ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 894ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 904ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(String filename, Charset encoding) throws IOException { 914ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(new File(filename), encoding, false); 924ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 934ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 944ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 954ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 964ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 974ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param filename the name of the file to write to, not null 984ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 994ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true if content should be appended, false to overwrite 1004ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file name or encoding is null 1014ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1024ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1034ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(String filename, Charset encoding, boolean append) throws IOException { 1044ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(new File(filename), encoding, append); 1054ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1064ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1074ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1084ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1094ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1104ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param filename the name of the file to write to, not null 1114ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1124ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file name or encoding is null 1134ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1144ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1154ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(String filename, CharsetEncoder encoding) throws IOException { 1164ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(new File(filename), encoding, false); 1174ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1184ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1194ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1204ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1214ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1224ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param filename the name of the file to write to, not null 1234ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1244ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true if content should be appended, false to overwrite 1254ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file name or encoding is null 1264ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1274ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1284ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(String filename, CharsetEncoder encoding, boolean append) throws IOException { 1294ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(new File(filename), encoding, append); 1304ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1314ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1324ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1334ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1344ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1354ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to write to, not null 1364ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1374ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 1384ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1394ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1404ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(File file, String encoding) throws IOException { 1414ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(file, encoding, false); 1424ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1434ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1444ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1454ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1464ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1474ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to write to, not null 1484ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1494ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true if content should be appended, false to overwrite 1504ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 1514ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1524ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1534ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(File file, String encoding, boolean append) throws IOException { 1544ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira super(); 1554ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this.out = initWriter(file, encoding, append); 1564ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1574ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1584ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1594ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1604ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1614ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to write to, not null 1624ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1634ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 1644ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1654ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1664ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(File file, Charset encoding) throws IOException { 1674ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(file, encoding, false); 1684ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1694ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1704ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1714ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1724ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1734ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to write to, not null 1744ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1754ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true if content should be appended, false to overwrite 1764ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 1774ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1784ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1794ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(File file, Charset encoding, boolean append) throws IOException { 1804ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira super(); 1814ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this.out = initWriter(file, encoding, append); 1824ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1834ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1844ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1854ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1864ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1874ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to write to, not null 1884ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 1894ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 1904ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 1914ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 1924ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(File file, CharsetEncoder encoding) throws IOException { 1934ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this(file, encoding, false); 1944ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 1954ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 1964ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 1974ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Constructs a FileWriterWithEncoding with a file encoding. 1984ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 1994ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to write to, not null 2004ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use, not null 2014ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true if content should be appended, false to overwrite 2024ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 2034ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException in case of an I/O error 2044ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 2054ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public FileWriterWithEncoding(File file, CharsetEncoder encoding, boolean append) throws IOException { 2064ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira super(); 2074ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira this.out = initWriter(file, encoding, append); 2084ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2094ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 2104ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira //----------------------------------------------------------------------- 2114ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 2124ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Initialise the wrapped file writer. 2134ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Ensure that a cleanup occurs if the writer creation fails. 2144ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * 2154ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param file the file to be accessed 2164ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param encoding the encoding to use - may be Charset, CharsetEncoder or String 2174ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param append true to append 2184ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @return the initialised writer 2194ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws NullPointerException if the file or encoding is null 2204ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an error occurs 2214ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 2224ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira private static Writer initWriter(File file, Object encoding, boolean append) throws IOException { 2234ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira if (file == null) { 2244ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira throw new NullPointerException("File is missing"); 2254ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2264ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira if (encoding == null) { 2274ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira throw new NullPointerException("Encoding is missing"); 2284ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2294ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira boolean fileExistedAlready = file.exists(); 2304ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira OutputStream stream = null; 2314ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira Writer writer = null; 2324ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira try { 2334ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira stream = new FileOutputStream(file, append); 2344ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira if (encoding instanceof Charset) { 2354ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira writer = new OutputStreamWriter(stream, (Charset)encoding); 2364ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } else if (encoding instanceof CharsetEncoder) { 2374ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira writer = new OutputStreamWriter(stream, (CharsetEncoder)encoding); 2384ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } else { 2394ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira writer = new OutputStreamWriter(stream, (String)encoding); 2404ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2414ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } catch (IOException ex) { 2424ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira IOUtils.closeQuietly(writer); 2434ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira IOUtils.closeQuietly(stream); 2444ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira if (fileExistedAlready == false) { 2454ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira FileUtils.deleteQuietly(file); 2464ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2474ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira throw ex; 2484ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } catch (RuntimeException ex) { 2494ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira IOUtils.closeQuietly(writer); 2504ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira IOUtils.closeQuietly(stream); 2514ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira if (fileExistedAlready == false) { 2524ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira FileUtils.deleteQuietly(file); 2534ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2544ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira throw ex; 2554ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2564ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira return writer; 2574ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2584ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 2594ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira //----------------------------------------------------------------------- 2604ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 2614ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Write a character. 2624ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param idx the character to write 2634ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 2644ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 2654ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void write(int idx) throws IOException { 2664ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.write(idx); 2674ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2684ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 2694ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 2704ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Write the characters from an array. 2714ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param chr the characters to write 2724ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 2734ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 2744ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void write(char[] chr) throws IOException { 2754ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.write(chr); 2764ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2774ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 2784ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 2794ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Write the specified characters from an array. 2804ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param chr the characters to write 2814ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param st The start offset 2824ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param end The number of characters to write 2834ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 2844ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 2854ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void write(char[] chr, int st, int end) throws IOException { 2864ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.write(chr, st, end); 2874ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2884ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 2894ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 2904ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Write the characters from a string. 2914ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param str the string to write 2924ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 2934ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 2944ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void write(String str) throws IOException { 2954ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.write(str); 2964ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 2974ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 2984ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 2994ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Write the specified characters from a string. 3004ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param str the string to write 3014ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param st The start offset 3024ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @param end The number of characters to write 3034ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 3044ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 3054ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void write(String str, int st, int end) throws IOException { 3064ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.write(str, st, end); 3074ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 3084ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 3094ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 3104ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Flush the stream. 3114ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 3124ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 3134ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void flush() throws IOException { 3144ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.flush(); 3154ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 3164ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira 3174ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira /** 3184ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * Close the stream. 3194ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira * @throws IOException if an I/O error occurs 3204ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira */ 3214ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira public void close() throws IOException { 3224ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira out.close(); 3234ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira } 3244ebb916ddca5f59d4f854f104fca0de6e0dda706Mindy Pereira} 325