Coverage Report - org.apache.commons.io.output.FileWriterWithEncoding
 
Classes in this File Line Coverage Branch Coverage Complexity
FileWriterWithEncoding
44%
30/68
58%
7/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 1415850 2012-11-30 20:51:39Z ggregory $
 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  0
     }
 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) 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  0
         this(new File(filename), encoding, false);
 92  0
     }
 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) throws IOException {
 104  0
         this(new File(filename), encoding, append);
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Constructs a FileWriterWithEncoding with a file encoding.
 109  
      *
 110  
      * @param filename  the name of the file to write to, not null
 111  
      * @param encoding  the encoding to use, not null
 112  
      * @throws NullPointerException if the file name or encoding is null
 113  
      * @throws IOException in case of an I/O error
 114  
      */
 115  
     public FileWriterWithEncoding(final String filename, final CharsetEncoder encoding) throws IOException {
 116  0
         this(new File(filename), encoding, false);
 117  0
     }
 118  
 
 119  
     /**
 120  
      * Constructs a FileWriterWithEncoding with a file encoding.
 121  
      *
 122  
      * @param filename  the name of the file to write to, not null
 123  
      * @param encoding  the encoding to use, not null
 124  
      * @param append  true if content should be appended, false to overwrite
 125  
      * @throws NullPointerException if the file name or encoding is null
 126  
      * @throws IOException in case of an I/O error
 127  
      */
 128  
     public FileWriterWithEncoding(final String filename, final CharsetEncoder encoding, final boolean append) throws IOException {
 129  0
         this(new File(filename), encoding, append);
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Constructs a FileWriterWithEncoding with a file encoding.
 134  
      *
 135  
      * @param file  the file to write to, not null
 136  
      * @param encoding  the encoding to use, not null
 137  
      * @throws NullPointerException if the file or encoding is null
 138  
      * @throws IOException in case of an I/O error
 139  
      */
 140  
     public FileWriterWithEncoding(final File file, final String encoding) throws IOException {
 141  12
         this(file, encoding, false);
 142  6
     }
 143  
 
 144  
     /**
 145  
      * Constructs a FileWriterWithEncoding with a file encoding.
 146  
      *
 147  
      * @param file  the file to write to, not null
 148  
      * @param encoding  the encoding to use, not null
 149  
      * @param append  true if content should be appended, false to overwrite
 150  
      * @throws NullPointerException if the file or encoding is null
 151  
      * @throws IOException in case of an I/O error
 152  
      */
 153  
     public FileWriterWithEncoding(final File file, final String encoding, final boolean append) throws IOException {
 154  12
         super();
 155  12
         this.out = initWriter(file, encoding, append);
 156  6
     }
 157  
 
 158  
     /**
 159  
      * Constructs a FileWriterWithEncoding with a file encoding.
 160  
      *
 161  
      * @param file  the file to write to, not null
 162  
      * @param encoding  the encoding to use, not null
 163  
      * @throws NullPointerException if the file or encoding is null
 164  
      * @throws IOException in case of an I/O error
 165  
      */
 166  
     public FileWriterWithEncoding(final File file, final Charset encoding) throws IOException {
 167  0
         this(file, encoding, false);
 168  0
     }
 169  
 
 170  
     /**
 171  
      * Constructs a FileWriterWithEncoding with a file encoding.
 172  
      *
 173  
      * @param file  the file to write to, not null
 174  
      * @param encoding  the encoding to use, not null
 175  
      * @param append  true if content should be appended, false to overwrite
 176  
      * @throws NullPointerException if the file or encoding is null
 177  
      * @throws IOException in case of an I/O error
 178  
      */
 179  
     public FileWriterWithEncoding(final File file, final Charset encoding, final boolean append) throws IOException {
 180  0
         super();
 181  0
         this.out = initWriter(file, encoding, append);
 182  0
     }
 183  
 
 184  
     /**
 185  
      * Constructs a FileWriterWithEncoding with a file encoding.
 186  
      *
 187  
      * @param file  the file to write to, not null
 188  
      * @param encoding  the encoding to use, not null
 189  
      * @throws NullPointerException if the file or encoding is null
 190  
      * @throws IOException in case of an I/O error
 191  
      */
 192  
     public FileWriterWithEncoding(final File file, final CharsetEncoder encoding) throws IOException {
 193  0
         this(file, encoding, false);
 194  0
     }
 195  
 
 196  
     /**
 197  
      * Constructs a FileWriterWithEncoding with a file encoding.
 198  
      *
 199  
      * @param file  the file to write to, not null
 200  
      * @param encoding  the encoding to use, not null
 201  
      * @param append  true if content should be appended, false to overwrite
 202  
      * @throws NullPointerException if the file or encoding is null
 203  
      * @throws IOException in case of an I/O error
 204  
      */
 205  
     public FileWriterWithEncoding(final File file, final CharsetEncoder encoding, final boolean append) throws IOException {
 206  0
         super();
 207  0
         this.out = initWriter(file, encoding, append);
 208  0
     }
 209  
 
 210  
     //-----------------------------------------------------------------------
 211  
     /**
 212  
      * Initialise the wrapped file writer.
 213  
      * Ensure that a cleanup occurs if the writer creation fails.
 214  
      *
 215  
      * @param file  the file to be accessed
 216  
      * @param encoding  the encoding to use - may be Charset, CharsetEncoder or String
 217  
      * @param append  true to append
 218  
      * @return the initialised writer
 219  
      * @throws NullPointerException if the file or encoding is null
 220  
      * @throws IOException if an error occurs
 221  
      */
 222  
      private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
 223  12
         if (file == null) {
 224  2
             throw new NullPointerException("File is missing");
 225  
         }
 226  10
         if (encoding == null) {
 227  0
             throw new NullPointerException("Encoding is missing");
 228  
         }
 229  10
         final boolean fileExistedAlready = file.exists();
 230  10
         OutputStream stream = null;
 231  10
         Writer writer = null;
 232  
         try {
 233  10
             stream = new FileOutputStream(file, append);
 234  8
             if (encoding instanceof Charset) {
 235  0
                 writer = new OutputStreamWriter(stream, (Charset)encoding);
 236  8
             } else if (encoding instanceof CharsetEncoder) {
 237  0
                 writer = new OutputStreamWriter(stream, (CharsetEncoder)encoding);
 238  
             } else {
 239  8
                 writer = new OutputStreamWriter(stream, (String)encoding);
 240  
             }
 241  4
         } catch (final IOException ex) {
 242  4
             IOUtils.closeQuietly(writer);
 243  4
             IOUtils.closeQuietly(stream);
 244  4
             if (fileExistedAlready == false) {
 245  2
                 FileUtils.deleteQuietly(file);
 246  
             }
 247  4
             throw ex;
 248  0
         } catch (final RuntimeException ex) {
 249  0
             IOUtils.closeQuietly(writer);
 250  0
             IOUtils.closeQuietly(stream);
 251  0
             if (fileExistedAlready == false) {
 252  0
                 FileUtils.deleteQuietly(file);
 253  
             }
 254  0
             throw ex;
 255  6
         }
 256  6
         return writer;
 257  
     }
 258  
 
 259  
     //-----------------------------------------------------------------------
 260  
     /**
 261  
      * Write a character.
 262  
      * @param idx the character to write
 263  
      * @throws IOException if an I/O error occurs
 264  
      */
 265  
      @Override
 266  
     public void write(final int idx) throws IOException {
 267  0
         out.write(idx);
 268  0
     }
 269  
 
 270  
     /**
 271  
      * Write the characters from an array.
 272  
      * @param chr the characters to write
 273  
      * @throws IOException if an I/O error occurs
 274  
      */
 275  
      @Override
 276  
     public void write(final char[] chr) throws IOException {
 277  0
         out.write(chr);
 278  0
     }
 279  
 
 280  
     /**
 281  
      * Write the specified characters from an array.
 282  
      * @param chr the characters to write
 283  
      * @param st The start offset
 284  
      * @param end The number of characters to write
 285  
      * @throws IOException if an I/O error occurs
 286  
      */
 287  
      @Override
 288  
     public void write(final char[] chr, final int st, final int end) throws IOException {
 289  0
         out.write(chr, st, end);
 290  0
     }
 291  
 
 292  
     /**
 293  
      * Write the characters from a string.
 294  
      * @param str the string to write
 295  
      * @throws IOException if an I/O error occurs
 296  
      */
 297  
      @Override
 298  
     public void write(final String str) throws IOException {
 299  6
         out.write(str);
 300  6
     }
 301  
 
 302  
     /**
 303  
      * Write the specified characters from a string.
 304  
      * @param str the string to write
 305  
      * @param st The start offset
 306  
      * @param end The number of characters to write
 307  
      * @throws IOException if an I/O error occurs
 308  
      */
 309  
      @Override
 310  
     public void write(final String str, final int st, final int end) throws IOException {
 311  0
         out.write(str, st, end);
 312  0
     }
 313  
 
 314  
     /**
 315  
      * Flush the stream.
 316  
      * @throws IOException if an I/O error occurs
 317  
      */
 318  
      @Override
 319  
     public void flush() throws IOException {
 320  6
         out.flush();
 321  6
     }
 322  
 
 323  
     /**
 324  
      * Close the stream.
 325  
      * @throws IOException if an I/O error occurs
 326  
      */
 327  
      @Override
 328  
     public void close() throws IOException {
 329  6
         out.close();
 330  6
     }
 331  
 }