001package org.unix4j.io;
002
003import java.io.File;
004import java.io.FileDescriptor;
005import java.io.FileOutputStream;
006import java.io.FileWriter;
007import java.io.IOException;
008import java.io.OutputStreamWriter;
009
010/**
011 * Output device writing to a {@link File}.
012 */
013public class FileOutput extends WriterOutput {
014        private final String fileInfo;
015
016        public FileOutput(FileWriter fileWriter) {
017                super(fileWriter, false);
018                this.fileInfo = fileWriter.toString();
019        }
020
021        public FileOutput(File file) {
022                super(createFileWriter(file), true);
023                this.fileInfo = file.toString();
024        }
025
026        public FileOutput(FileOutputStream fileStream) {
027                super(new OutputStreamWriter(fileStream), false);
028                this.fileInfo = fileStream.toString();
029        }
030
031        public FileOutput(FileDescriptor fileDesc) {
032                super(new FileWriter(fileDesc), true);
033                this.fileInfo = fileDesc.toString();
034        }
035
036        public FileOutput(String file) {
037                this(new File(file));
038        }
039
040        private static FileWriter createFileWriter(File file) {
041                try {
042                        return new FileWriter(file);
043                } catch (IOException e) {
044                        throw new RuntimeException(e);
045                }
046        }
047
048        @Override
049        public String toString() {
050                return getClass().getSimpleName() + "(fileInfo=" + fileInfo + ")";
051        }
052}