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