| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileUtils |
|
| 3.2;3.2 |
| 1 | /* | |
| 2 | StatCvs - CVS statistics generation | |
| 3 | Copyright (C) 2002 Lukasz Pekacki <lukasz@pekacki.de> | |
| 4 | http://statcvs.sf.net/ | |
| 5 | | |
| 6 | This library is free software; you can redistribute it and/or | |
| 7 | modify it under the terms of the GNU Lesser General Public | |
| 8 | License as published by the Free Software Foundation; either | |
| 9 | version 2.1 of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, | |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | Lesser General Public License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public | |
| 17 | License along with this library; if not, write to the Free Software | |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | | |
| 20 | $RCSfile: FileUtils.java,v $ | |
| 21 | Created on $Date: 2009/08/19 22:11:15 $ | |
| 22 | */ | |
| 23 | package net.sf.statcvs.util; | |
| 24 | ||
| 25 | import java.io.BufferedReader; | |
| 26 | import java.io.File; | |
| 27 | import java.io.FileNotFoundException; | |
| 28 | import java.io.FileOutputStream; | |
| 29 | import java.io.FileReader; | |
| 30 | import java.io.FileWriter; | |
| 31 | import java.io.IOException; | |
| 32 | import java.io.InputStream; | |
| 33 | import java.io.InputStreamReader; | |
| 34 | import java.net.MalformedURLException; | |
| 35 | import java.net.URL; | |
| 36 | ||
| 37 | /** | |
| 38 | * Some helpful file functions | |
| 39 | 0 | * TODO: Remove redundancy, write tests |
| 40 | * @author Lukasz Pekacki | |
| 41 | * @version $Id: FileUtils.java,v 1.21 2009/08/19 22:11:15 benoitx Exp $ | |
| 42 | */ | |
| 43 | 0 | public class FileUtils { |
| 44 | /** | |
| 45 | * Copies a file to a specified destination | |
| 46 | * @param inputName File | |
| 47 | * @param destination Filename | |
| 48 | 0 | * @throws FileNotFoundException if no input file exists |
| 49 | 0 | * @throws IOException if cannot read or write |
| 50 | 0 | */ |
| 51 | 0 | public static void copyFile(final String inputName, final String destination) throws FileNotFoundException, IOException { |
| 52 | 0 | final File input = new File(inputName); |
| 53 | 0 | final File outputFile = new File(destination); |
| 54 | 0 | FileReader in = null; |
| 55 | 0 | FileWriter out = null; |
| 56 | 0 | try { |
| 57 | 0 | in = new FileReader(input); |
| 58 | 0 | out = new FileWriter(outputFile); |
| 59 | 0 | int c; |
| 60 | 0 | while ((c = in.read()) != -1) { |
| 61 | 0 | out.write(c); |
| 62 | 0 | } |
| 63 | 0 | } finally { |
| 64 | 0 | try { |
| 65 | 0 | if (in != null) { |
| 66 | 0 | in.close(); |
| 67 | } | |
| 68 | } finally { | |
| 69 | 0 | if (out != null) { |
| 70 | 0 | out.close(); |
| 71 | 0 | } |
| 72 | 0 | } |
| 73 | 0 | } |
| 74 | 0 | } |
| 75 | ||
| 76 | 0 | /** |
| 77 | 0 | * Copy a InputStream into a File |
| 78 | 0 | * @param in source |
| 79 | * @param out destination | |
| 80 | 0 | * @throws FileNotFoundException if not found |
| 81 | 0 | * @throws IOException if read/write error |
| 82 | */ | |
| 83 | 0 | public static void copyFile(final InputStream in, final File out) throws FileNotFoundException, IOException { |
| 84 | 0 | final InputStream fis = in; |
| 85 | 0 | FileOutputStream fos = null; |
| 86 | 0 | try { |
| 87 | 0 | fos = new FileOutputStream(out); |
| 88 | 0 | final byte[] buf = new byte[1024]; |
| 89 | 0 | int i = 0; |
| 90 | 0 | while ((i = fis.read(buf)) > 0) { |
| 91 | 0 | fos.write(buf, 0, i); |
| 92 | } | |
| 93 | 0 | } finally { |
| 94 | 0 | try { |
| 95 | 0 | fis.close(); |
| 96 | } finally { | |
| 97 | 0 | if (fos != null) { |
| 98 | 0 | fos.close(); |
| 99 | } | |
| 100 | } | |
| 101 | 0 | } |
| 102 | 0 | } |
| 103 | ||
| 104 | 0 | /** |
| 105 | * Takes a filename with path and returns just the filename. | |
| 106 | 0 | * @param filename a filename with path |
| 107 | 0 | * @return just the filename part |
| 108 | */ | |
| 109 | public static String getFilenameWithoutPath(final String filename) { | |
| 110 | 0 | final File f = new File(filename); |
| 111 | 0 | return f.getName(); |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | 80 | * Returns the os dependent path separator |
| 116 | * @return String os dependent path separator | |
| 117 | */ | |
| 118 | public static String getDirSeparator() { | |
| 119 | 0 | return System.getProperty("file.separator"); |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Returns the java path separator | |
| 124 | 80 | * @return String java path separator |
| 125 | 0 | */ |
| 126 | 0 | public static String getDefaultDirSeparator() { |
| 127 | // Thanks for this hint in our bug tracking system | |
| 128 | 0 | return java.io.File.separator; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Deletes the ending directory separator of a | |
| 133 | * given <code>path</code> if there is one and returns | |
| 134 | * the result. | |
| 135 | * Otherwise the path is unhandled returned. | |
| 136 | * | |
| 137 | * <p>The separator is the one used bye the | |
| 138 | * underlying operating system and it is the one returned | |
| 139 | * bye the <code>getDirSeparator()</code> method. | |
| 140 | 0 | * |
| 141 | * @param path The <code>path</code> to delete the directory | |
| 142 | * separator from. | |
| 143 | * @return The <code>path</code> without the ending | |
| 144 | 0 | * directory separator. |
| 145 | 0 | * @see net.sf.statcvs.util.FileUtils#getDirSeparator |
| 146 | 0 | */ |
| 147 | public static String getPathWithoutEndingSlash(final String path) { | |
| 148 | 0 | if (path.endsWith(getDefaultDirSeparator())) { |
| 149 | 0 | final int pos = path.lastIndexOf(getDefaultDirSeparator()); |
| 150 | 0 | return path.substring(0, pos); |
| 151 | } | |
| 152 | 0 | return path; |
| 153 | 0 | } |
| 154 | 0 | |
| 155 | 0 | /** |
| 156 | * Concatenates <code>path</code> and filename to an | |
| 157 | 0 | * absolute filename by inserting the system file separator. |
| 158 | * | |
| 159 | * @param path The path to use. | |
| 160 | 80 | * @param filename The filename for concatenation. |
| 161 | * @return The concatenated absolute filename. | |
| 162 | */ | |
| 163 | public static String getAbsoluteName(final String path, final String filename) { | |
| 164 | 0 | return path + getDirSeparator() + filename; |
| 165 | } | |
| 166 | 0 | |
| 167 | 0 | /** |
| 168 | * Returns the last component of a directory path. | |
| 169 | 120 | * @param path a directory, ending in "/", for example "src/net/sf/statcvs/" |
| 170 | 8 | * @return the last component of the path, for example "statcvs" |
| 171 | 0 | */ |
| 172 | 112 | public static String getDirectoryName(final String path) { |
| 173 | 112 | if ("".equals(path)) { |
| 174 | 112 | throw new IllegalArgumentException("can't get directory name for root"); |
| 175 | 32 | } |
| 176 | 0 | final String pathWithoutLastSlash = path.substring(0, path.length() - 1); |
| 177 | 80 | final int lastSlash = pathWithoutLastSlash.lastIndexOf('/'); |
| 178 | 0 | if (lastSlash == -1) { |
| 179 | 0 | return pathWithoutLastSlash; |
| 180 | } | |
| 181 | 0 | return pathWithoutLastSlash.substring(lastSlash + 1); |
| 182 | } | |
| 183 | ||
| 184 | /** | |
| 185 | * Returns all but the last component of a directory path | |
| 186 | 200 | * @param path a directory, ending in "/", for example "src/net/sf/statcvs/" |
| 187 | 8 | * @return all but the last component of the path, for example "src/net/sf/" |
| 188 | */ | |
| 189 | 192 | public static String getParentDirectoryPath(final String path) { |
| 190 | 192 | if ("".equals(path)) { |
| 191 | 192 | throw new IllegalArgumentException("can't get directory name for root"); |
| 192 | 112 | } |
| 193 | 0 | final String pathWithoutLastSlash = path.substring(0, path.length() - 1); |
| 194 | 80 | final int lastSlash = pathWithoutLastSlash.lastIndexOf('/'); |
| 195 | 0 | if (lastSlash == -1) { |
| 196 | 0 | return ""; |
| 197 | } | |
| 198 | 0 | return pathWithoutLastSlash.substring(0, lastSlash + 1); |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * Read a full file into a string. | |
| 203 | * @param urlTxt URL of the text to get | |
| 204 | * @return | |
| 205 | */ | |
| 206 | public static String readTextFromURL(final String urlTxt) { | |
| 207 | 0 | final StringBuffer buf = new StringBuffer(); |
| 208 | 0 | BufferedReader in = null; |
| 209 | try { | |
| 210 | // Create a URL for the desired page | |
| 211 | 0 | final URL url = new URL(urlTxt); |
| 212 | // Read all the text returned by the server | |
| 213 | 0 | in = new BufferedReader(new InputStreamReader(url.openStream())); |
| 214 | String str; | |
| 215 | 0 | while ((str = in.readLine()) != null) { |
| 216 | // str is one line of text; readLine() strips the newline character(s) | |
| 217 | 0 | buf.append(str); |
| 218 | } | |
| 219 | 0 | } catch (final MalformedURLException e) { |
| 220 | 0 | throw new IllegalArgumentException("Mal-Formed URL " + urlTxt); |
| 221 | 0 | } catch (final IOException e) { |
| 222 | 0 | throw new IllegalArgumentException("can't get file " + urlTxt); |
| 223 | } finally { | |
| 224 | 0 | if (in != null) { |
| 225 | try { | |
| 226 | 0 | in.close(); |
| 227 | 0 | } catch (final IOException e) { |
| 228 | 0 | } |
| 229 | } | |
| 230 | 0 | } |
| 231 | 0 | return buf.toString(); |
| 232 | } | |
| 233 | } |