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