001    /*
002     * Copyright (c) 2013 by Oli B.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     *
016     * (c)reated 14.09.2013 by Oli B. (boehm@javatux.de)
017     */
018    
019    package patterntesting.runtime.io;
020    
021    import java.io.*;
022    
023    /**
024     * Whenever you need a {@link FileOutputStream} and want a better toString
025     * implementation you can use the class BetterFileOutputStream. "Better" means
026     * that you see the name if the input file in the log which is much more helpful
027     * than the default implementation of {@link Object#toString()}, especially for
028     * logging and debugging.
029     *
030     * @author oliver (boehm@javatux.de)
031     * @since 1.3.1 (14.09.2013)
032     * @see FileOutputStream
033     */
034    public final class BetterFileOutputStream extends FileOutputStream {
035    
036        private final File file;
037    
038        /**
039         * Instantiates a new better file output stream.
040         *
041         * @param name the file name
042         * @throws FileNotFoundException the file not found exception
043         * @see FileOutputStream#FileOutputStream(String)
044         */
045        public BetterFileOutputStream(final String name) throws FileNotFoundException {
046             super(name);
047             this.file = new File(name);
048        }
049    
050        /**
051         * Instantiates a new better file output stream.
052         *
053         * @param file the file
054         * @throws FileNotFoundException the file not found exception
055         * @see FileOutputStream#FileOutputStream(File)
056         */
057        public BetterFileOutputStream(final File file) throws FileNotFoundException {
058            super(file);
059            this.file = file;
060        }
061    
062        /**
063         * Instantiates a new better file output stream.
064         *
065         * @param name the file name
066         * @param append if it should be appended
067         * @throws FileNotFoundException the file not found exception
068         * @see FileOutputStream#FileOutputStream(String,boolean)
069         */
070        public BetterFileOutputStream(final String name, final boolean append) throws FileNotFoundException {
071            super(name, append);
072            this.file = new File(name);
073        }
074    
075        /**
076         * Instantiates a new better file output stream.
077         *
078         * @param file the file
079         * @param append if it should be appended
080         * @throws FileNotFoundException the file not found exception
081         * @see FileOutputStream#FileOutputStream(File,boolean)
082         */
083        public BetterFileOutputStream(final File file, final boolean append) throws FileNotFoundException {
084            super(file, append);
085            this.file = file;
086        }
087    
088        /**
089         * This toString implementation reports the name of the output file which is
090         * much more helpful than the default implementation of
091         * {@link Object#toString()}, especially for logging and debugging.
092         *
093         * @return the string
094         * @see java.lang.Object#toString()
095         */
096        @Override
097        public String toString() {
098            return "output stream for file \"" + this.file + '"';
099        }
100    
101    }
102