view src/FileUtils.java @ 7:342d366654ce

2012-01-27 Pavel Tisnovsky <ptisnovs@redhat.com> * templates/style.css: New styles added which is used in generated test report. * src/FileUtils.java: * src/PrintClassList.java: * src/PrintPublicMethods.java: * src/PrintTestCoverage.java: Minor changes - JavaDoc * templates/all_classes_template.html: * templates/all_packages_template.html: * templates/class_template.html: * templates/package_template.html: * templates/summary.html: Removed useless character ^M in the XML declarations.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Fri, 27 Jan 2012 16:35:26 +0100
parents 5ef74c026b81
children f010a4361c96
line wrap: on
line source

/*
  Test coverage tool.

   Copyright (C) 2012 Red Hat

This tool is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This tool is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this tool; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * Basic file utilities to avoid boring file handling.
 *
 * @author Pavel Tisnovsky
 */
class FileUtils
{
    /**
     * EOF constant used as special return value in some methods.
     */
    private static final int EOF = -1;

    /**
     * Read content of given text file and return it as list of strings. No
     * exception is thrown during reading.
     * 
     * @param fileName
     *            name of file to be read
     * @return list of string containing content of text file
     */
    static List<String> readTextFile(String fileName)
    {
        BufferedReader reader = null;
        List<String> out = new LinkedList<String>();
        try
        {
            // try to open file for reading
            reader = new BufferedReader(new FileReader(fileName));
            readAllLinesFromTextFile(reader, out);
        }
        catch (FileNotFoundException e)
        {
            // might happen - empty list is returned in this case
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // might happen - empty list is returned in this case
            // make sure the list is empty
            out.clear();
            e.printStackTrace();
        }
        finally
        {
            closeBufferedReader(reader);
        }
        // return list containing content of text file
        return out;
    }

    /**
     * Write list of string to a file with given name. No exception is thrown
     * during reading.
     * 
     * @param fileName
     *            name of file to be read
     * @param lines
     *            of string containing content of text file
     */
    static void writeTextFile(String fileName, List<String> lines)
    {
        BufferedWriter fout = null;
        try
        {
            fout = new BufferedWriter(new FileWriter(fileName));
            // write all lines to a text file
            writeAllLinesToTextFile(fout, lines);
        }
        catch (IOException e)
        {
            // might happen - empty list is returned in this case
            e.printStackTrace();
        }
        finally
        {
            closeBufferedWriter(fout);
        }
    }

    /**
     * Write list of string to a file with given name. No exception is thrown
     * during reading.
     *
     * @param reportDirectory
     * @param string
     * @param lines of string containing content of text file
     */
    static void writeTextFile(String reportDirectory, String string, List<String> lines)
    {
        writeTextFile(new File(reportDirectory, string), lines);
    }

    /**
     * Write list of string to a file with given name. No exception is thrown
     * during reading.
     * 
     * @param file
     *            representing file name
     * @param lines
     *            of string containing content of text file
     */
    static void writeTextFile(File file, List<String> lines)
    {
        writeTextFile(file.getPath(), lines);
    }

    /**
     * Try to close buffered reader.
     *
     * @param bufferedReader instance of buffered reader or null
     */
    private static void closeBufferedReader(BufferedReader bufferedReader)
    {
        // try to close the buffered reader
        try
        {
            // for the easier use, it is possible to call this method with null parameter
            if (bufferedReader != null)
            {
                bufferedReader.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Try to close buffered reader.
     * 
     * @param bufferedWriter
     *            instance of buffered writer or null
     */
    private static void closeBufferedWriter(BufferedWriter bufferedWriter)
    {
        // try to close the buffered writer
        try
        {
            // for the easier use, it is possible to call this method with null parameter
            if (bufferedWriter != null)
            {
                bufferedWriter.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Read all lines from text file and add them to a list of strings.
     * 
     * @param bufferedReader
     *            instance of buffered reader
     * @param lines
     *            list of string to be filled
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    private static void readAllLinesFromTextFile(BufferedReader bufferedReader, List<String> lines) throws IOException
    {
        String line;
        // read lines from a text file
        while ((line = bufferedReader.readLine()) != null)
        {
            lines.add(line);
        }
    }

    /**
     * Write content of the list of strings to a file.
     * 
     * @param bufferedWriter
     *            instance of buffered writer
     * @param lines
     *            list of string
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    private static void writeAllLinesToTextFile(BufferedWriter bufferedWriter, List<String> lines) throws IOException
    {
        // for all lines
        for (String line : lines)
        {
            bufferedWriter.write(line);
            // new line character should be added after each string
            bufferedWriter.write('\n');
        }
    }

    /**
     * Read one byte from given file input stream with check if EOF is
     * reached.
     * 
     * @param fileInputStream instance of already opened FileInputStream
     * @return
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    static int readOneByte(FileInputStream fileInputStream) throws IOException
    {
        // try to read one byte from the input stream
        int i = fileInputStream.read();
        // -1 means that EOF is reached
        if (i == EOF)
        {
            throw new EOFException("End of file reached!");
        }
        return i;
    }

    /**
     * Read two bytes from given file input stream with check if EOF is
     * reached.
     * 
     * @param fileInputStream instance of already opened FileInputStream
     * @return
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    static int readTwoBytes(FileInputStream fileInputStream) throws IOException
    {
        // try to read two bytes from the input stream
        int i1 = readOneByte(fileInputStream);
        int i2 = readOneByte(fileInputStream);
        // combine all two read bytes into a word
        return (i1 << 8) | (i2);
    }

    /**
     * Read four bytes from given file input stream with check if EOF is
     * reached.
     * 
     * @param fileInputStream instance of already opened FileInputStream
     * @return
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    static int readFourBytes(FileInputStream fileInputStream) throws IOException
    {
        // try to read four bytes from the input stream
        int i1 = readOneByte(fileInputStream);
        int i2 = readOneByte(fileInputStream);
        int i3 = readOneByte(fileInputStream);
        int i4 = readOneByte(fileInputStream);
        // combine all four read bytes into a word
        return (i1 << 24) | (i2 << 16) | (i3 << 8) | (i4);
    }

}