/** Close closeable object from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); } }
/** * Return base name for given file path. Returns null if given path parameter is null. * * @param path file path string. * @return file name without extension or null. */ public static String basename(String path) { return path != null ? basename(new File(path)) : null; }
/** * Copy bytes from remote file denoted by given URL to specified local file. This method creates local file if not * already exists. * * @param url source remote file URL, * @param file target local file. * @return the number of bytes transfered. * @throws FileNotFoundException if given <code>file</code> does not exist and cannot be created. * @throws IOException if bytes transfer fails. */ public static long copy(URL url, File file) throws FileNotFoundException, IOException { return copy(url, new FileOutputStream(file)); }
/** * Binary copy to temporary file. Be aware that temporary file is removed at JVM exit. * * @param stream source stream. * @return temporarily created file. * @throws IOException if copy operation fails. */ public static File copy(InputStream stream) throws IOException { File file = createTempFile(); copy(stream, new FileOutputStream(file)); return file; }
/** * Atomic download file from specified URL to local target file. This method guarantee atomic operation: file is * actually created only if transfer completes with success. For this purpose download is performed to a temporary * file that is renamed after transfer complete. Of course, if transfer fails rename does not occur. * * @param url source file URL, * @param file local target file. * @throws IOException if transfer or local write fails. */ public static void download(URL url, File file) throws IOException { final File partialFile = new File(file.getAbsolutePath() + PARTIAL_FILE_SUFFIX); try { Files.copy(url, partialFile); renameTo(partialFile, file); } finally { // last resort to ensure partially file does not hang around on error // do not test for delete execution fail since, at this point, the partial file state is not certain partialFile.delete(); } }
/** * Guess if file is an image file based on file extension. This is a very trivial test relying on file extension and * obviously cannot guarantee correct results. * * @param file file to test. * @return true if given file is an image file. */ public static boolean isImage(File file) { return file != null ? IMAGE_FILE_EXTENSIONS.contains(Files.getExtension(file)) : false; }
@Override public void moveTo(File targetDir) throws IOException { Params.isDirectory(targetDir, "Target directory"); Files.renameTo(file, new File(targetDir, fileName)); }
/** * Create MD5 message digest for requested file content. This method returns a 16-bytes array with computed MD5 * message digest value. * * @param file file to create message digest for. * @return 16-bytes array of message digest. * @throws FileNotFoundException if <code>file</code> does not exist. * @throws IOException if file read operation fails. */ public static byte[] getFileDigest(File file) throws FileNotFoundException, IOException { return getFileDigest(new FileInputStream(file)); }
path.append(dot2path(qualifiedName)); if(fileExtension != null) { if(fileExtension.charAt(0) != '.') {
String packagePath = Files.dot2urlpath(packageName);
/** * Copy text from character stream to temporary file then close both the source reader and temporary file. Be aware * that temporary file is removed at JVM exit. * * @param reader source character reader. * @return the newly created temporarily file. * @throws IOException if copy operation fails. */ public static File copy(Reader reader) throws IOException { File file = createTempFile(); copy(reader, new FileWriter(file)); return file; }
/** * Get extension of the file denoted by given URL or empty string if not extension. Returned extension does not * contain dot separator, that is, <code>htm</code> not <code>.htm</code>. Returns null if <code>url</code> parameter * is null. * * @param url the URL of file to return extension. * @return file extension or empty string or null if <code>url</code> parameter is null. */ public static String getExtension(URL url) { return url != null ? getExtension(url.getPath()) : null; }
/** Close stream from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); }
/** * Copy bytes from requested input stream to given target file. This method creates the target file if does not * already exist. * * @param inputStream source input stream, * @param file target file. * @return the number of bytes processed. * @throws FileNotFoundException if given <code>file</code> does not exist and cannot be created. * @throws IOException if bytes transfer fails. */ public static long copy(InputStream inputStream, File file) throws FileNotFoundException, IOException { return copy(inputStream, new FileOutputStream(file)); }
/** * Copy text from byte stream using specified encoding to temporary file then close both input stream and temporary * file. Be aware that temporary file is removed at JVM exit. * * @param stream input stream to read from, * @param encoding used character encoding. * @return temporarily created file. * @throws IOException if copy operation fails. */ public static File copy(InputStream stream, String encoding) throws IOException { File file = createTempFile(); copy(new InputStreamReader(stream, encoding), new OutputStreamWriter(new FileOutputStream(file), encoding)); return file; }
/** * Get file extension as lower case or empty string. Returned extension does not contain dot separator, that is, * <code>htm</code> not <code>.htm</code>. Returns null if <code>file</code> parameter is null. * * @param file file to return extension of. * @return file extension or empty string or null if <code>file</code> parameter is null. */ public static String getExtension(File file) { return file != null ? getExtension(file.getAbsolutePath()) : null; }
/** * Construct view meta instance. * * @param file template file, * @param implementation implementation class, * @param properties optional view instance specific properties. */ public ViewMeta(File file, Class<? extends View> implementation, Properties properties) { this.name = Files.basename(file); this.file = file; this.implementation = implementation; this.properties = properties; this.string = Strings.concat(name, ':', implementation.getName(), ':', file); }
/** Close closeable object from current thread storage then remove the storage. */ @Override public void clean() { Files.close(threadLocal.get()); threadLocal.remove(); } }
/** * Copy source file to target. Copy destination should be a file and this method throws access denied if attempt to * write to a directory. Source file should exist but target is created by this method, but if not already exist. * * @param source file to read from, should exist, * @param target file to write to. * @return the number of bytes transfered. * @throws FileNotFoundException if source file does not exist or target file does not exist and cannot be created. * @throws IOException if copy operation fails, including if <code>target</code> is a directory. */ public static long copy(File source, File target) throws FileNotFoundException, IOException { return copy(new FileInputStream(source), new FileOutputStream(target)); }