| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IOUtil |
|
| 2.6666666666666665;2.667 |
| 1 | /* | |
| 2 | * Cobertura - http://cobertura.sourceforge.net/ | |
| 3 | * | |
| 4 | * Copyright (C) 2005 Grzegorz Lukasik | |
| 5 | * Copyright (C) 2006 John Lewis | |
| 6 | * | |
| 7 | * Note: This file is dual licensed under the GPL and the Apache | |
| 8 | * Source License (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.util; | |
| 28 | ||
| 29 | import java.io.ByteArrayOutputStream; | |
| 30 | import java.io.File; | |
| 31 | import java.io.FileInputStream; | |
| 32 | import java.io.FileNotFoundException; | |
| 33 | import java.io.FileOutputStream; | |
| 34 | import java.io.IOException; | |
| 35 | import java.io.InputStream; | |
| 36 | import java.io.OutputStream; | |
| 37 | import java.io.OutputStreamWriter; | |
| 38 | import java.io.PrintWriter; | |
| 39 | import java.io.UnsupportedEncodingException; | |
| 40 | import java.io.Writer; | |
| 41 | ||
| 42 | /** | |
| 43 | * Helper class with useful I/O operations. | |
| 44 | * | |
| 45 | * @author Grzegorz Lukasik | |
| 46 | */ | |
| 47 | 0 | public abstract class IOUtil |
| 48 | { | |
| 49 | ||
| 50 | /** | |
| 51 | * Copies bytes from input stream into the output stream. Stops | |
| 52 | * when the input stream read method returns -1. Does not close | |
| 53 | * the streams. | |
| 54 | * | |
| 55 | * @throws IOException If either passed stream will throw IOException. | |
| 56 | * @throws NullPointerException If either passed stream is null. | |
| 57 | */ | |
| 58 | public static void copyStream(InputStream in, OutputStream out) | |
| 59 | throws IOException | |
| 60 | { | |
| 61 | // NullPointerException is explicity thrown to guarantee expected behaviour | |
| 62 | 31 | if (in == null || out == null) |
| 63 | 3 | throw new NullPointerException(); |
| 64 | ||
| 65 | int el; | |
| 66 | 28 | byte[] buffer = new byte[1 << 15]; |
| 67 | 105 | while ((el = in.read(buffer)) != -1) |
| 68 | { | |
| 69 | 77 | out.write(buffer, 0, el); |
| 70 | 77 | } |
| 71 | 28 | } |
| 72 | ||
| 73 | /** | |
| 74 | * Returns an array that contains values read from the | |
| 75 | * given input stream. | |
| 76 | * | |
| 77 | * @throws NullPointerException If null stream is passed. | |
| 78 | */ | |
| 79 | public static byte[] createByteArrayFromInputStream(InputStream in) | |
| 80 | throws IOException | |
| 81 | { | |
| 82 | 25 | ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); |
| 83 | 25 | copyStream(in, byteArray); |
| 84 | 24 | return byteArray.toByteArray(); |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Moves a file from one location to other. | |
| 89 | * | |
| 90 | * @throws IOException If IO exception occur during moving. | |
| 91 | * @throws NullPointerException If either passed file is null. | |
| 92 | */ | |
| 93 | public static void moveFile(File sourceFile, File destinationFile) | |
| 94 | throws IOException | |
| 95 | { | |
| 96 | 4 | if (destinationFile.exists()) |
| 97 | { | |
| 98 | 2 | destinationFile.delete(); |
| 99 | } | |
| 100 | ||
| 101 | // Move file using File method if possible | |
| 102 | 3 | boolean succesfulMove = sourceFile.renameTo(destinationFile); |
| 103 | 2 | if (succesfulMove) |
| 104 | 2 | return; |
| 105 | ||
| 106 | // Copy file from source to destination | |
| 107 | 0 | InputStream in = null; |
| 108 | 0 | OutputStream out = null; |
| 109 | try | |
| 110 | { | |
| 111 | 0 | in = new FileInputStream(sourceFile); |
| 112 | 0 | out = new FileOutputStream(destinationFile); |
| 113 | 0 | copyStream(in, out); |
| 114 | } | |
| 115 | finally | |
| 116 | { | |
| 117 | 0 | in = closeInputStream(in); |
| 118 | 0 | out = closeOutputStream(out); |
| 119 | 0 | } |
| 120 | ||
| 121 | // Remove source file | |
| 122 | 0 | sourceFile.delete(); |
| 123 | 0 | } |
| 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 | 13 | if (in != null) |
| 135 | { | |
| 136 | try | |
| 137 | { | |
| 138 | 13 | in.close(); |
| 139 | 13 | in = null; |
| 140 | } | |
| 141 | 0 | catch (IOException e) |
| 142 | { | |
| 143 | 0 | System.err.println("Cobertura: Error closing input stream."); |
| 144 | 0 | e.printStackTrace(); |
| 145 | 13 | } |
| 146 | } | |
| 147 | 13 | 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 | 13 | if (out != null) |
| 160 | { | |
| 161 | try | |
| 162 | { | |
| 163 | 12 | out.close(); |
| 164 | 12 | out = null; |
| 165 | } | |
| 166 | 0 | catch (IOException e) |
| 167 | { | |
| 168 | 0 | System.err.println("Cobertura: Error closing output stream."); |
| 169 | 0 | e.printStackTrace(); |
| 170 | 12 | } |
| 171 | } | |
| 172 | 13 | return out; |
| 173 | } | |
| 174 | ||
| 175 | public static PrintWriter getPrintWriter(File file) throws UnsupportedEncodingException, FileNotFoundException | |
| 176 | { | |
| 177 | 45 | Writer osWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); |
| 178 | 45 | PrintWriter pw = new PrintWriter(osWriter, false); |
| 179 | 45 | return pw; |
| 180 | } | |
| 181 | ||
| 182 | } |