| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.any23.util; |
| 19 | |
|
| 20 | |
import java.io.BufferedInputStream; |
| 21 | |
import java.io.BufferedOutputStream; |
| 22 | |
import java.io.ByteArrayOutputStream; |
| 23 | |
import java.io.File; |
| 24 | |
import java.io.FileInputStream; |
| 25 | |
import java.io.FileNotFoundException; |
| 26 | |
import java.io.FileOutputStream; |
| 27 | |
import java.io.FileWriter; |
| 28 | |
import java.io.FilenameFilter; |
| 29 | |
import java.io.IOException; |
| 30 | |
import java.io.InputStream; |
| 31 | |
import java.io.PrintWriter; |
| 32 | |
import java.util.ArrayList; |
| 33 | |
import java.util.List; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class FileUtils { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public static File mv(File target, File dest) { |
| 50 | 0 | if (!dest.isDirectory()) { |
| 51 | 0 | throw new IllegalArgumentException("destination must be a directory."); |
| 52 | |
} |
| 53 | |
|
| 54 | 0 | final File newFile = new File(dest, target.getName()); |
| 55 | 0 | boolean success = target.renameTo(newFile); |
| 56 | 0 | if (!success) { |
| 57 | 0 | throw new IllegalStateException( |
| 58 | |
String.format("Cannot move target file [%s] to destination [%s]", target, newFile) |
| 59 | |
); |
| 60 | |
} |
| 61 | 0 | return newFile; |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public static void cp(InputStream is, File dest) { |
| 72 | 0 | if (dest.exists()) { |
| 73 | 0 | throw new IllegalArgumentException("Destination must not exist."); |
| 74 | |
} |
| 75 | 0 | BufferedInputStream bis = null; |
| 76 | 0 | BufferedOutputStream bos = null; |
| 77 | |
try { |
| 78 | 0 | bis = new BufferedInputStream(is); |
| 79 | 0 | FileOutputStream fos = new FileOutputStream(dest); |
| 80 | 0 | bos = new BufferedOutputStream(fos); |
| 81 | 0 | final byte[] buffer = new byte[1024 * 4]; |
| 82 | |
int read; |
| 83 | |
while (true) { |
| 84 | 0 | read = bis.read(buffer); |
| 85 | 0 | if (read == -1) { |
| 86 | 0 | break; |
| 87 | |
} |
| 88 | 0 | bos.write(buffer, 0, read); |
| 89 | |
} |
| 90 | 0 | } catch (Exception e) { |
| 91 | 0 | throw new RuntimeException("Error while copying stream into file.", e); |
| 92 | |
} finally { |
| 93 | 0 | StreamUtils.closeGracefully(bis); |
| 94 | 0 | StreamUtils.closeGracefully(bos); |
| 95 | 0 | } |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static void cp(File src, File dest) throws FileNotFoundException { |
| 106 | 0 | FileInputStream fis = null; |
| 107 | |
try { |
| 108 | 0 | fis = new FileInputStream(src); |
| 109 | 0 | cp(fis, dest); |
| 110 | |
} finally { |
| 111 | 0 | StreamUtils.closeGracefully(fis); |
| 112 | 0 | } |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public static void dumpContent(File f, String content) throws IOException { |
| 123 | 0 | FileWriter fw = new FileWriter(f); |
| 124 | |
try { |
| 125 | 0 | fw.write(content); |
| 126 | |
} finally { |
| 127 | 0 | StreamUtils.closeGracefully(fw); |
| 128 | 0 | } |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public static void dumpContent(File f, Throwable t) throws IOException { |
| 139 | 0 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 140 | 0 | final PrintWriter pw = new PrintWriter(baos); |
| 141 | 0 | t.printStackTrace(pw); |
| 142 | 0 | pw.close(); |
| 143 | 0 | dumpContent(f, baos.toString()); |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public static String readResourceContent(Class clazz, String resource) throws IOException { |
| 155 | 0 | return StreamUtils.asString( clazz.getResourceAsStream(resource) ); |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public static String readResourceContent(String resource) throws IOException { |
| 166 | 0 | return readResourceContent(FileUtils.class, resource); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public static String readFileContent(File f) throws IOException { |
| 177 | 0 | FileInputStream fis = new FileInputStream(f); |
| 178 | 0 | return StreamUtils.asString(fis, true); |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public static String[] readFileLines(File f) throws IOException { |
| 189 | 0 | FileInputStream fis = new FileInputStream(f); |
| 190 | 0 | return StreamUtils.asLines(fis); |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public static File[] listFilesRecursively(File dir, FilenameFilter filenameFilter) { |
| 201 | 0 | if( ! dir.isDirectory() ) { |
| 202 | 0 | throw new IllegalArgumentException(dir.getAbsolutePath() + " must be a directory."); |
| 203 | |
} |
| 204 | 0 | final List<File> result = new ArrayList<File>(); |
| 205 | 0 | visitFilesRecursively(dir, filenameFilter, result); |
| 206 | 0 | return result.toArray( new File[result.size()] ); |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
private static void visitFilesRecursively(File dir, FilenameFilter filenameFilter, List<File> result) { |
| 217 | 0 | for (File file : dir.listFiles()) { |
| 218 | 0 | if (!file.isDirectory()) { |
| 219 | 0 | if (filenameFilter == null || filenameFilter.accept(dir, file.getName())) { |
| 220 | 0 | result.add(file); |
| 221 | |
} |
| 222 | |
} else { |
| 223 | 0 | visitFilesRecursively(file, filenameFilter, result); |
| 224 | |
} |
| 225 | |
} |
| 226 | 0 | } |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | 0 | private FileUtils() {} |
| 232 | |
|
| 233 | |
} |