001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2005 Grzegorz Lukasik
005     * Copyright (C) 2006 John Lewis
006     * Copyright (C) 2007 Ignat Zapolsky
007     *
008     * Note: This file is dual licensed under the GPL and the Apache
009     * Source License (so that it can be used from both the main
010     * Cobertura classes and the ant tasks).
011     *
012     * Cobertura is free software; you can redistribute it and/or modify
013     * it under the terms of the GNU General Public License as published
014     * by the Free Software Foundation; either version 2 of the License,
015     * or (at your option) any later version.
016     *
017     * Cobertura is distributed in the hope that it will be useful, but
018     * WITHOUT ANY WARRANTY; without even the implied warranty of
019     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020     * General Public License for more details.
021     *
022     * You should have received a copy of the GNU General Public License
023     * along with Cobertura; if not, write to the Free Software
024     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
025     * USA
026     */
027    
028    package net.sourceforge.cobertura.util;
029    
030    import java.io.BufferedWriter;
031    import java.io.ByteArrayOutputStream;
032    import java.io.File;
033    import java.io.FileInputStream;
034    import java.io.FileNotFoundException;
035    import java.io.FileOutputStream;
036    import java.io.IOException;
037    import java.io.InputStream;
038    import java.io.OutputStream;
039    import java.io.OutputStreamWriter;
040    import java.io.PrintWriter;
041    import java.io.UnsupportedEncodingException;
042    import java.io.Writer;
043    
044    /**
045     * Helper class with useful I/O operations.
046     * 
047     * @author Grzegorz Lukasik
048     */
049    public abstract class IOUtil
050    {
051    
052            /**
053             * Copies bytes from input stream into the output stream.  Stops
054             * when the input stream read method returns -1.  Does not close
055             * the streams.
056             * 
057             * @throws IOException If either passed stream will throw IOException.
058             * @throws NullPointerException If either passed stream is null.
059             */
060            public static void copyStream(InputStream in, OutputStream out)
061                            throws IOException
062            {
063                    // NullPointerException is explicity thrown to guarantee expected behaviour
064                    if (in == null || out == null)
065                            throw new NullPointerException();
066    
067                    int el;
068                    byte[] buffer = new byte[1 << 15];
069                    while ((el = in.read(buffer)) != -1)
070                    {
071                            out.write(buffer, 0, el);
072                    }
073            }
074    
075            /**
076             * Returns an array that contains values read from the
077             * given input stream.
078             * 
079             * @throws NullPointerException If null stream is passed.
080             */
081            public static byte[] createByteArrayFromInputStream(InputStream in)
082                            throws IOException
083            {
084                    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
085                    copyStream(in, byteArray);
086                    return byteArray.toByteArray();
087            }
088    
089            /**
090             * Moves a file from one location to other.
091             *
092             * @throws IOException If IO exception occur during moving.
093             * @throws NullPointerException If either passed file is null.
094             */
095            public static void moveFile(File sourceFile, File destinationFile)
096                            throws IOException
097            {
098                    if (destinationFile.exists())
099                    {
100                            destinationFile.delete();
101                    }
102    
103                    // Move file using File method if possible
104                    boolean succesfulMove = sourceFile.renameTo(destinationFile);
105                    if (succesfulMove)
106                            return;
107    
108                    // Copy file from source to destination
109                    InputStream in = null;
110                    OutputStream out = null;
111                    try
112                    {
113                            in = new FileInputStream(sourceFile);
114                            out = new FileOutputStream(destinationFile);
115                            copyStream(in, out);
116                    }
117                    finally
118                    {
119                            in = closeInputStream(in);
120                            out = closeOutputStream(out);
121                    }
122    
123                    // Remove source file
124                    sourceFile.delete();
125            }
126    
127            /**
128             * Closes an input stream.
129             * 
130             * @param in The stream to close.
131             * @return null unless an exception was thrown while closing, else
132             *         returns the stream
133             */
134            public static InputStream closeInputStream(InputStream in)
135            {
136                    if (in != null)
137                    {
138                            try
139                            {
140                                    in.close();
141                                    in = null;
142                            }
143                            catch (IOException e)
144                            {
145                                    System.err.println("Cobertura: Error closing input stream.");
146                                    e.printStackTrace();
147                            }
148                    }
149                    return in;
150            }
151    
152            /**
153             * Closes an output stream.
154             * 
155             * @param out The stream to close.
156             * @return null unless an exception was thrown while closing, else
157             *         returns the stream.
158             */
159            public static OutputStream closeOutputStream(OutputStream out)
160            {
161                    if (out != null)
162                    {
163                            try
164                            {
165                                    out.close();
166                                    out = null;
167                            }
168                            catch (IOException e)
169                            {
170                                    System.err.println("Cobertura: Error closing output stream.");
171                                    e.printStackTrace();
172                            }
173                    }
174                    return out;
175            }
176    
177            public static PrintWriter getPrintWriter(File file) throws UnsupportedEncodingException, FileNotFoundException
178            {
179                    Writer osWriter = new BufferedWriter (new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), 16384);
180                    PrintWriter pw = new PrintWriter(osWriter, false);
181                    return pw;
182            }
183            
184    }