/* * Copyright (C) 2007 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.io; import com.google.common.base.Preconditions; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.nio.charset.Charset; import java.security.MessageDigest; import java.util.List; import java.util.zip.Checksum; /** * Provides utility methods for working with files. * *
All method parameters must be non-null unless documented otherwise.
*
* @author Chris Nokleberg
* @since 2009.09.15 tentative
*/
public final class Files {
/** Maximum loop count when creating temp directories. */
private static final int TEMP_DIR_ATTEMPTS = 10000;
private Files() {}
/**
* Returns a buffered reader that reads from a file using the given
* character set.
*
* @param file the file to read from
* @param charset the character set used when writing the file
* @return the buffered reader
*/
public static BufferedReader newReader(File file, Charset charset)
throws FileNotFoundException {
return new BufferedReader(
new InputStreamReader(new FileInputStream(file), charset));
}
/**
* Returns a buffered writer that writes to a file using the given
* character set.
*
* @param file the file to write to
* @param charset the character set used when writing the file
* @return the buffered writer
*/
public static BufferedWriter newWriter(File file, Charset charset)
throws FileNotFoundException {
return new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), charset));
}
/**
* Returns a factory that will supply instances of {@link FileInputStream}
* that read from a file.
*
* @param file the file to read from
* @return the factory
*/
public static InputSupplier Use this method instead of {@link File#createTempFile(String, String)}
* when you wish to create a directory, not a regular file. A common pitfall
* is to call {@code createTempFile}, delete the file and create a
* directory in its place, but this leads a race condition which can be
* exploited to create security vulnerabilities, especially when executable
* files are to be written into the directory.
*
* This method assumes that the temporary volume is writable, has free
* inodes and free blocks, and that it will not be called thousands of times
* per second.
*
* @return the newly-created directory
* @throws IllegalStateException if the directory could not be created
*/
public static File createTempDir() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
String baseName = System.currentTimeMillis() + "-";
for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
File tempDir = new File(baseDir, baseName + counter);
if (tempDir.mkdir()) {
return tempDir;
}
}
throw new IllegalStateException("Failed to create directory within "
+ TEMP_DIR_ATTEMPTS + " attempts (tried "
+ baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
}
/**
* Creates an empty file or updates the last updated timestamp on the
* same as the unix command of the same name.
*
* @param file the file to create or update
* @throws IOException if an I/O error occurs
*/
public static void touch(File file) throws IOException {
if (!file.createNewFile()
&& !file.setLastModified(System.currentTimeMillis())) {
throw new IOException("Unable to update modification time of " + file);
}
}
/**
* Moves the file from one path to another. This method can rename a file or
* move it to a different directory, like the Unix {@code mv} command.
*
* @param from the source file
* @param to the destination file
* @throws IOException if an I/O error occurs
*/
public static void move(File from, File to) throws IOException {
Preconditions.checkNotNull(to);
Preconditions.checkArgument(!from.equals(to),
"Source %s and destination %s must be different", from, to);
if (!from.renameTo(to)) {
copy(from, to);
if (!from.delete()) {
if (!to.delete()) {
throw new IOException("Unable to delete " + to);
}
throw new IOException("Unable to delete " + from);
}
}
}
/**
* Deletes all the files within a directory. Does not delete the
* directory itself.
*
* If the file argument is a symbolic link or there is a symbolic
* link in the path leading to the directory, this method will do
* nothing. Symbolic links within the directory are not followed.
*
* @param directory the directory to delete the contents of
* @throws IllegalArgumentException if the argument is not a directory
* @throws IOException if an I/O error occurs
* @see #deleteRecursively
*/
public static void deleteDirectoryContents(File directory)
throws IOException {
Preconditions.checkArgument(directory.isDirectory(),
"Not a directory: %s", directory);
// Symbolic links will have different canonical and absolute paths
if (!directory.getCanonicalPath().equals(directory.getAbsolutePath())) {
return;
}
File[] files = directory.listFiles();
if (files == null) {
throw new IOException("Error listing files for " + directory);
}
for (File file : files) {
deleteRecursively(file);
}
}
/**
* Deletes a file or directory and all contents recursively.
*
* If the file argument is a symbolic link the link will be deleted
* but not the target of the link. If the argument is a directory,
* symbolic links within the directory will not be followed.
*
* @param file the file to delete
* @throws IOException if an I/O error occurs
* @see #deleteDirectoryContents
*/
public static void deleteRecursively(File file) throws IOException {
if (file.isDirectory()) {
deleteDirectoryContents(file);
}
if (!file.delete()) {
throw new IOException("Failed to delete " + file);
}
}
/**
* Reads the first line from a file. The line does not include
* line-termination characters, but does include other leading and
* trailing whitespace.
*
* @param file the file to read from
* @param charset the character set used when writing the file
* @return the first line, or null if the file is empty
* @throws IOException if an I/O error occurs
*/
public static String readFirstLine(File file, Charset charset)
throws IOException {
return CharStreams.readFirstLine(Files.newReaderSupplier(file, charset));
}
/**
* Reads all of the lines from a file. The lines do not include
* line-termination characters, but do include other leading and
* trailing whitespace.
*
* @param file the file to read from
* @param charset the character set used when writing the file
* @return a mutable {@link List} containing all the lines
* @throws IOException if an I/O error occurs
*/
public static List (If this seems too complicated, maybe you're looking for
* {@link #toByteArray}.)
*
* @param file the file to read
* @param processor the object to which the bytes of the file are passed.
* @return the result of the byte processor
* @throws IOException if an I/O error occurs
*/
public static Files are mapped from offset 0 to its length.
*
* This only works for files <= {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @return a read-only buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
*
* @see FileChannel#map(MapMode, long, long)
* @since 2010.01.04 tentative
*/
public static MappedByteBuffer map(File file) throws IOException {
return map(file, MapMode.READ_ONLY);
}
/**
* Fully maps a file in to memory as per
* {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}
* using the requested {@link MapMode}.
*
* Files are mapped from offset 0 to its length.
*
* This only works for files <= {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @param mode the mode to use when mapping {@code file}
* @return a buffer reflecting {@code file}
* @throws FileNotFoundException if the {@code file} does not exist
* @throws IOException if an I/O error occurs
*
* @see FileChannel#map(MapMode, long, long)
* @since 2010.01.04 tentative
*/
public static MappedByteBuffer map(File file, MapMode mode)
throws IOException {
if (!file.exists()) {
throw new FileNotFoundException(file.toString());
}
return map(file, mode, file.length());
}
/**
* Maps a file in to memory as per
* {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}
* using the requested {@link MapMode}.
*
* Files are mapped from offset 0 to {@code size}.
*
* If the mode is {@link MapMode#READ_WRITE} and the file does not exist,
* it will be created with the requested {@code size}. Thus this method is
* useful for creating memory mapped files which do not yet exist.
*
* This only works for files <= {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @param mode the mode to use when mapping {@code file}
* @return a buffer reflecting {@code file}
* @throws IOException if an I/O error occurs
*
* @see FileChannel#map(MapMode, long, long)
* @since 2010.01.04 tentative
*/
public static MappedByteBuffer map(File file, MapMode mode, long size)
throws FileNotFoundException, IOException {
RandomAccessFile raf =
new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw");
boolean threw = true;
try {
MappedByteBuffer mbb = map(raf, mode, size);
threw = false;
return mbb;
} finally {
Closeables.close(raf, threw);
}
}
private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode,
long size) throws IOException {
FileChannel channel = raf.getChannel();
boolean threw = true;
try {
MappedByteBuffer mbb = channel.map(mode, 0, size);
threw = false;
return mbb;
} finally {
Closeables.close(channel, threw);
}
}
}