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