001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.align.util;
022
023
024import java.io.*;
025import java.util.zip.GZIPOutputStream;
026
027public class SynchronizedOutFile {
028
029        File file;
030
031        String[] tmp;
032
033        int ARR_SIZE=100;
034        Integer counter;
035
036        boolean useGzipCompression = false;
037
038
039        /** Create a thread safe wrapper for writing to this file, the file will be gzip compressed.
040         *
041         * @param f file to write to
042         * @param gzipCompress flag if file should be gzip compressed
043         * @throws FileNotFoundException
044         * @throws IOException
045         */
046        public SynchronizedOutFile(File f, boolean gzipCompress) throws FileNotFoundException, IOException{
047                if ( f.isDirectory())
048                        throw new FileNotFoundException("please provide a file and not a directory");
049
050                if ( ! f.exists()){
051                        System.out.println("creating output file: " + f.getAbsolutePath());
052                        f.createNewFile();
053                }
054                file = f;
055                tmp = new String[ARR_SIZE];
056                counter = -1;
057                useGzipCompression = gzipCompress;
058
059        }
060
061        /** create a thread safe wrapper for working with this file
062         *
063         * @param f
064         */
065        public SynchronizedOutFile(File f) throws FileNotFoundException, IOException{
066
067                this(f,false);
068
069        }
070
071        public synchronized void write(String message) throws IOException{
072
073                synchronized (counter){
074                        counter++;
075                        tmp[counter] = message;
076                        if (counter >= ARR_SIZE - 1 ) {
077                                writeArr();
078                                counter = -1;
079                        }
080                }
081
082
083        }
084
085        public synchronized void flush() throws IOException {
086                synchronized (counter){
087                        writeArr();
088                        counter = -1;
089                }
090        }
091
092        public void close() throws IOException{
093                writeArr();
094                tmp = new String[ARR_SIZE];
095        }
096
097        private void writeArr() throws IOException{
098
099
100                OutputStream out = null;
101                FileOutputStream fileOutputStream=null;
102                try {
103                        //This is less code-redundant
104                        fileOutputStream = new FileOutputStream(file, true);
105                        OutputStream outputstream = useGzipCompression? new GZIPOutputStream(fileOutputStream) : fileOutputStream;
106                        out = new BufferedOutputStream(outputstream);
107
108                        for ( int i = 0 ; i <= counter ; i++){
109                                if ( tmp[i] == null )
110                                        continue;
111                                byte[] data = tmp[i].getBytes();
112                                out.write(data, 0, data.length);
113                        }
114
115                } catch (Exception x) {
116                        System.err.println(x);
117                } finally {
118                        if (out != null) {
119                                out.flush();
120                                out.close();
121                        }
122                }
123        }
124
125
126}