~ubuntu-branches/ubuntu/trusty/cobertura/trusty

« back to all changes in this revision

Viewing changes to test/net/sourceforge/cobertura/ant/Util.java

  • Committer: Bazaar Package Importer
  • Author(s): Yulia Novozhilova
  • Date: 2009-06-24 13:56:29 UTC
  • Revision ID: james.westby@ubuntu.com-20090624135629-hgvo8631yye7ofj3
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cobertura - http://cobertura.sourceforge.net/
 
3
 *
 
4
 * Copyright (C) 2006 John Lewis
 
5
 * Copyright (C) 2006 Mark Doliner
 
6
 * 
 
7
 * Note: This file is dual licensed under the GPL and the Apache
 
8
 * Source License 1.1 (so that it can be used from both the main
 
9
 * Cobertura classes and the ant tasks).
 
10
 *
 
11
 * Cobertura is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published
 
13
 * by the Free Software Foundation; either version 2 of the License,
 
14
 * or (at your option) any later version.
 
15
 *
 
16
 * Cobertura is distributed in the hope that it will be useful, but
 
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
19
 * General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with Cobertura; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
24
 * USA
 
25
 */
 
26
 
 
27
package net.sourceforge.cobertura.ant;
 
28
 
 
29
import java.io.BufferedReader;
 
30
import java.io.ByteArrayOutputStream;
 
31
import java.io.File;
 
32
import java.io.FileNotFoundException;
 
33
import java.io.FileReader;
 
34
import java.io.IOException;
 
35
import java.io.PrintStream;
 
36
 
 
37
class Util
 
38
{
 
39
 
 
40
        static File createTemporaryTextFile(String prefix) throws IOException
 
41
        {
 
42
                File outputFile;
 
43
                outputFile = File.createTempFile(prefix, ".txt");
 
44
                outputFile.deleteOnExit();
 
45
                return outputFile;
 
46
        }
 
47
 
 
48
        /**
 
49
         * Returns the text of a file as a string.
 
50
         * 
 
51
         * @param file The file to read.
 
52
         * @return A string containing the text of the file
 
53
         */
 
54
        static String getText(File file) throws FileNotFoundException, IOException
 
55
        {
 
56
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
57
                PrintStream ps = new PrintStream(baos);
 
58
                BufferedReader reader = null;
 
59
                try
 
60
                {
 
61
                        reader = new BufferedReader(new FileReader(file));
 
62
                        String line;
 
63
                        while ((line = reader.readLine()) != null)
 
64
                        {
 
65
                                ps.println(line);
 
66
                        }
 
67
                        ps.close();
 
68
                }
 
69
                finally
 
70
                {
 
71
                        ps.close();
 
72
                        if (reader != null)
 
73
                        {
 
74
                                try
 
75
                                {
 
76
                                        reader.close();
 
77
                                }
 
78
                                catch (IOException e)
 
79
                                {
 
80
                                        System.err.println("IOException when closing file " + file.getAbsolutePath());
 
81
                                        e.printStackTrace();
 
82
                                }
 
83
                        }
 
84
                }
 
85
 
 
86
                return baos.toString();
 
87
        }
 
88
 
 
89
}