001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.util.io;
025    
026    import java.io.BufferedWriter;
027    import java.io.Closeable;
028    import java.io.File;
029    import java.io.FileOutputStream;
030    import java.io.FileWriter;
031    import java.io.Flushable;
032    import java.io.IOException;
033    import java.io.OutputStream;
034    import java.io.OutputStreamWriter;
035    import java.io.Writer;
036    import java.lang.reflect.Constructor;
037    import java.util.zip.GZIPOutputStream;
038    import java.util.zip.ZipOutputStream;
039    
040    public class IntelligentFileWriter extends Writer implements Appendable, Closeable, Flushable {
041    
042            private static final String SEVENZIPOUTPUTSTREAM = "org.ujmp.j7zip.SevenZipOutputStream";
043    
044            private FileWriter fw = null;
045    
046            private OutputStream zip = null;
047    
048            private BufferedWriter bw = null;
049    
050            private String encoding = "UTF-8";
051    
052            public IntelligentFileWriter(String filename) throws IOException, ClassNotFoundException {
053                    this(filename, false);
054            }
055    
056            public IntelligentFileWriter(String filename, boolean append) throws IOException,
057                            ClassNotFoundException {
058                    this(new File(filename), append);
059            }
060    
061            public IntelligentFileWriter(OutputStream outputStream) throws IOException {
062                    bw = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
063            }
064    
065            public IntelligentFileWriter(File file) throws IOException {
066                    this(file, false);
067            }
068    
069            public IntelligentFileWriter(File file, boolean append) throws IOException {
070                    if (file.getAbsolutePath().toLowerCase().endsWith(".gz")) {
071                            zip = new GZIPOutputStream(new FileOutputStream(file, append));
072                            bw = new BufferedWriter(new OutputStreamWriter(zip, encoding));
073                    } else if (file.getAbsolutePath().toLowerCase().endsWith(".z")) {
074                            zip = new ZipOutputStream(new FileOutputStream(file, append));
075                            bw = new BufferedWriter(new OutputStreamWriter(zip, encoding));
076                    } else if (file.getAbsolutePath().toLowerCase().endsWith(".7z")) {
077                            try {
078                                    Class<?> c = Class.forName(SEVENZIPOUTPUTSTREAM);
079                                    Constructor<?> con = c.getConstructor(new Class[] { FileOutputStream.class });
080                                    zip = (OutputStream) con.newInstance(new Object[] { new FileOutputStream(file,
081                                                    append) });
082                            } catch (ClassNotFoundException e) {
083                                    throw new IOException("Could not find ujmp-j7zip in classpath");
084                            } catch (Exception e) {
085                                    throw new IOException("Could not create SevenZipOutputStream");
086                            }
087                            bw = new BufferedWriter(new OutputStreamWriter(zip, encoding));
088                    } else {
089                            fw = new FileWriter(file, append);
090                            bw = new BufferedWriter(fw);
091                    }
092            }
093    
094            public static final void save(String filename, String text) throws Exception {
095                    IntelligentFileWriter fw = new IntelligentFileWriter(filename, false);
096                    fw.write(text);
097                    fw.close();
098            }
099    
100            public static final void append(String filename, String text) throws Exception {
101                    IntelligentFileWriter fw = new IntelligentFileWriter(filename, true);
102                    fw.write(text);
103                    fw.close();
104            }
105    
106            
107            public void close() throws IOException {
108                    if (bw != null)
109                            bw.close();
110                    if (fw != null)
111                            fw.close();
112                    if (zip != null)
113                            zip.close();
114            }
115    
116            
117            public void flush() throws IOException {
118                    if (bw != null)
119                            bw.flush();
120                    if (fw != null)
121                            fw.flush();
122                    if (zip != null)
123                            zip.flush();
124            }
125    
126            
127            public void write(char[] cbuf, int off, int len) throws IOException {
128                    bw.write(cbuf, off, len);
129            }
130    
131            public static void write(OutputStream os, String text) {
132                    try {
133                            IntelligentFileWriter fw = new IntelligentFileWriter(os);
134                            fw.write(text);
135                            fw.close();
136                    } catch (Exception e) {
137                            e.printStackTrace();
138                    }
139            }
140    
141    }