|
| 1 | +package org.apache.commons.io; |
| 2 | + |
| 3 | +/* ==================================================================== |
| 4 | + * The Apache Software License, Version 1.1 |
| 5 | + * |
| 6 | + * Copyright (c) 2001 The Apache Software Foundation. All rights |
| 7 | + * reserved. |
| 8 | + * |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions |
| 11 | + * are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * |
| 16 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | + * notice, this list of conditions and the following disclaimer in |
| 18 | + * the documentation and/or other materials provided with the |
| 19 | + * distribution. |
| 20 | + * |
| 21 | + * 3. The end-user documentation included with the redistribution, |
| 22 | + * if any, must include the following acknowledgment: |
| 23 | + * "This product includes software developed by the |
| 24 | + * Apache Software Foundation (http://www.apache.org/)." |
| 25 | + * Alternately, this acknowledgment may appear in the software itself, |
| 26 | + * if and wherever such third-party acknowledgments normally appear. |
| 27 | + * |
| 28 | + * 4. The names "Apache" and "Apache Software Foundation" and |
| 29 | + * "Apache Turbine" must not be used to endorse or promote products |
| 30 | + * derived from this software without prior written permission. For |
| 31 | + * written permission, please contact apache@apache.org. |
| 32 | + * |
| 33 | + * 5. Products derived from this software may not be called "Apache", |
| 34 | + * "Apache Turbine", nor may "Apache" appear in their name, without |
| 35 | + * prior written permission of the Apache Software Foundation. |
| 36 | + * |
| 37 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 38 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 39 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 40 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 41 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 42 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 43 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 44 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 45 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 46 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 47 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 48 | + * SUCH DAMAGE. |
| 49 | + * ==================================================================== |
| 50 | + * |
| 51 | + * This software consists of voluntary contributions made by many |
| 52 | + * individuals on behalf of the Apache Software Foundation. For more |
| 53 | + * information on the Apache Software Foundation, please see |
| 54 | + * <http://www.apache.org/>. |
| 55 | + */ |
| 56 | + |
| 57 | +import java.io.File; |
| 58 | +import java.io.FileInputStream; |
| 59 | +import java.io.FileOutputStream; |
| 60 | + |
| 61 | +/** |
| 62 | + * Common {@link java.io.File} manipulation routines. |
| 63 | + * |
| 64 | + * Taken from the commons-utils repo. |
| 65 | + * |
| 66 | + * @author <a href="mailto:sanders@apache.org">Scott Sanders</a> |
| 67 | + * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> |
| 68 | + * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a> |
| 69 | + * @version $Id: FileUtils.java,v 1.1 2002/01/26 02:47:42 sanders Exp $ |
| 70 | + */ |
| 71 | +public class FileUtils |
| 72 | +{ |
| 73 | + /** |
| 74 | + * The number of bytes in a kilobyte. |
| 75 | + */ |
| 76 | + public static final int ONE_KB = 1024; |
| 77 | + |
| 78 | + /** |
| 79 | + * The number of bytes in a megabyte. |
| 80 | + */ |
| 81 | + public static final int ONE_MB = ONE_KB * ONE_KB; |
| 82 | + |
| 83 | + /** |
| 84 | + * The number of bytes in a gigabyte. |
| 85 | + */ |
| 86 | + public static final int ONE_GB = ONE_KB * ONE_MB; |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a human-readable version of the file size (original is in |
| 90 | + * bytes). |
| 91 | + * |
| 92 | + * @param size The number of bytes. |
| 93 | + * @return A human-readable display value (includes units). |
| 94 | + */ |
| 95 | + public static String byteCountToDisplaySize(int size) |
| 96 | + { |
| 97 | + String displaySize; |
| 98 | + |
| 99 | + if (size / ONE_GB > 0) |
| 100 | + { |
| 101 | + displaySize = String.valueOf(size / ONE_GB) + " GB"; |
| 102 | + } |
| 103 | + else if (size / ONE_MB > 0) |
| 104 | + { |
| 105 | + displaySize = String.valueOf(size / ONE_MB) + " MB"; |
| 106 | + } |
| 107 | + else if (size / ONE_KB > 0) |
| 108 | + { |
| 109 | + displaySize = String.valueOf(size / ONE_KB) + " KB"; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + displaySize = String.valueOf(size) + " bytes"; |
| 114 | + } |
| 115 | + |
| 116 | + return displaySize; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the directory path portion of a file specification string. |
| 121 | + * Matches the equally named unix command. |
| 122 | + * @return The directory portion excluding the ending file separator. |
| 123 | + */ |
| 124 | + public static String dirname(String filename) |
| 125 | + { |
| 126 | + int i = filename.lastIndexOf(File.separator); |
| 127 | + return (i >= 0 ? filename.substring(0, i) : ""); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns the filename portion of a file specification string. |
| 132 | + * @return The filename string with extension. |
| 133 | + */ |
| 134 | + public static String filename(String filename) |
| 135 | + { |
| 136 | + int i = filename.lastIndexOf(File.separator); |
| 137 | + return (i >= 0 ? filename.substring(i + 1) : filename); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns the filename portion of a file specification string. |
| 142 | + * Matches the equally named unix command. |
| 143 | + * @return The filename string without extension. |
| 144 | + */ |
| 145 | + public static String basename(String filename) |
| 146 | + { |
| 147 | + return basename(filename, extension(filename)); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns the filename portion of a file specification string. |
| 152 | + * Matches the equally named unix command. |
| 153 | + */ |
| 154 | + public static String basename(String filename, String suffix) |
| 155 | + { |
| 156 | + int i = filename.lastIndexOf(File.separator) + 1; |
| 157 | + int lastDot = ((suffix != null) && (suffix.length() > 0)) |
| 158 | + ? filename.lastIndexOf(suffix) : -1; |
| 159 | + |
| 160 | + if (lastDot >= 0) |
| 161 | + { |
| 162 | + return filename.substring(i, lastDot); |
| 163 | + } |
| 164 | + else if (i > 0) |
| 165 | + { |
| 166 | + return filename.substring(i); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + return filename; // else returns all (no path and no extension) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns the extension portion of a file specification string. |
| 176 | + * This everything after the last dot '.' in the filename (including |
| 177 | + * the dot). |
| 178 | + */ |
| 179 | + public static String extension(String filename) |
| 180 | + { |
| 181 | + int lastDot = filename.lastIndexOf('.'); |
| 182 | + |
| 183 | + if (lastDot >= 0) |
| 184 | + { |
| 185 | + return filename.substring(lastDot); |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + return ""; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Check if a file exits. |
| 195 | + * |
| 196 | + * @param fileName The name of the file to check. |
| 197 | + * @return true if file exists. |
| 198 | + */ |
| 199 | + public static boolean fileExists(String fileName) |
| 200 | + { |
| 201 | + File file = new File(fileName); |
| 202 | + return file.exists(); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Reads the contents of a file. |
| 207 | + * |
| 208 | + * @param fileName The name of the file to read. |
| 209 | + * @return The file contents or null if read failed. |
| 210 | + */ |
| 211 | + public static String fileRead(String fileName) throws Exception |
| 212 | + { |
| 213 | + StringBuffer buf = new StringBuffer(); |
| 214 | + |
| 215 | + FileInputStream in = new FileInputStream(fileName); |
| 216 | + |
| 217 | + int count; |
| 218 | + byte[] b = new byte[512]; |
| 219 | + while ( (count = in.read(b)) > 0 ) // blocking read |
| 220 | + { |
| 221 | + buf.append( new String(b, 0, count) ); |
| 222 | + } |
| 223 | + |
| 224 | + in.close(); |
| 225 | + |
| 226 | + return buf.toString(); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Writes data to a file. The file will be created if it does not exist. |
| 231 | + * |
| 232 | + * @param fileName The name of the file to write. |
| 233 | + * @param data The content to write to the file. |
| 234 | + */ |
| 235 | + public static void fileWrite(String fileName, String data) throws Exception |
| 236 | + { |
| 237 | + FileOutputStream out = new FileOutputStream(fileName); |
| 238 | + out.write( data.getBytes() ); |
| 239 | + out.close(); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Deletes a file. |
| 244 | + * |
| 245 | + * @param fileName The name of the file to delete. |
| 246 | + */ |
| 247 | + public static void fileDelete(String fileName) |
| 248 | + { |
| 249 | + File file = new File(fileName); |
| 250 | + file.delete(); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Waits for NFS to propagate a file creation, imposing a timeout. |
| 255 | + * |
| 256 | + * @param fileName The name of the file. |
| 257 | + * @param seconds The maximum time in seconds to wait. |
| 258 | + * @return True if file exists. |
| 259 | + */ |
| 260 | + public static boolean waitFor(String fileName, int seconds) |
| 261 | + { |
| 262 | + File file = new File(fileName); |
| 263 | + int timeout = 0; |
| 264 | + int tick = 0; |
| 265 | + while ( ! file.exists() ) |
| 266 | + { |
| 267 | + if (tick++ >= 10) |
| 268 | + { |
| 269 | + tick = 0; |
| 270 | + if (timeout++ > seconds) |
| 271 | + { |
| 272 | + return false; |
| 273 | + } |
| 274 | + } |
| 275 | + try |
| 276 | + { |
| 277 | + Thread.sleep(100); |
| 278 | + } |
| 279 | + catch(InterruptedException ignore) |
| 280 | + { |
| 281 | + } |
| 282 | + catch(Exception ex) |
| 283 | + { |
| 284 | + break; |
| 285 | + } |
| 286 | + } |
| 287 | + return true; |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Creates a file handle. |
| 292 | + * |
| 293 | + * @param fileName The name of the file. |
| 294 | + * @return A <code>File</code> instance. |
| 295 | + */ |
| 296 | + public static File getFile(String fileName) |
| 297 | + { |
| 298 | + return new File(fileName); |
| 299 | + } |
| 300 | +} |
0 commit comments