001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.io;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019import static com.google.common.io.FileWriteMode.APPEND;
020import static java.util.Arrays.asList;
021import static java.util.Collections.unmodifiableList;
022
023import com.google.common.annotations.Beta;
024import com.google.common.annotations.GwtIncompatible;
025import com.google.common.annotations.J2ktIncompatible;
026import com.google.common.base.Joiner;
027import com.google.common.base.Optional;
028import com.google.common.base.Predicate;
029import com.google.common.base.Splitter;
030import com.google.common.collect.ImmutableList;
031import com.google.common.collect.ImmutableSet;
032import com.google.common.graph.SuccessorsFunction;
033import com.google.common.graph.Traverser;
034import com.google.common.hash.HashCode;
035import com.google.common.hash.HashFunction;
036import com.google.errorprone.annotations.CanIgnoreReturnValue;
037import com.google.errorprone.annotations.InlineMe;
038import com.google.j2objc.annotations.J2ObjCIncompatible;
039import java.io.BufferedReader;
040import java.io.BufferedWriter;
041import java.io.File;
042import java.io.FileInputStream;
043import java.io.FileNotFoundException;
044import java.io.FileOutputStream;
045import java.io.IOException;
046import java.io.InputStreamReader;
047import java.io.OutputStream;
048import java.io.OutputStreamWriter;
049import java.io.RandomAccessFile;
050import java.nio.MappedByteBuffer;
051import java.nio.channels.FileChannel;
052import java.nio.channels.FileChannel.MapMode;
053import java.nio.charset.Charset;
054import java.nio.charset.StandardCharsets;
055import java.util.ArrayList;
056import java.util.List;
057import org.jspecify.annotations.Nullable;
058
059/**
060 * Provides utility methods for working with {@linkplain File files}.
061 *
062 * <p>{@link java.nio.file.Path} users will find similar utilities in {@link MoreFiles} and the
063 * JDK's {@link java.nio.file.Files} class.
064 *
065 * @author Chris Nokleberg
066 * @author Colin Decker
067 * @since 1.0
068 */
069@J2ktIncompatible
070@GwtIncompatible
071public final class Files {
072
073  private Files() {}
074
075  /**
076   * Returns a buffered reader that reads from a file using the given character set.
077   *
078   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
079   * java.nio.file.Files#newBufferedReader(java.nio.file.Path, Charset)}.
080   *
081   * @param file the file to read from
082   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
083   *     helpful predefined constants
084   * @return the buffered reader
085   */
086  public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException {
087    checkNotNull(file);
088    checkNotNull(charset);
089    return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
090  }
091
092  /**
093   * Returns a buffered writer that writes to a file using the given character set.
094   *
095   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
096   * java.nio.file.Files#newBufferedWriter(java.nio.file.Path, Charset,
097   * java.nio.file.OpenOption...)}.
098   *
099   * @param file the file to write to
100   * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
101   *     helpful predefined constants
102   * @return the buffered writer
103   */
104  public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException {
105    checkNotNull(file);
106    checkNotNull(charset);
107    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
108  }
109
110  /**
111   * Returns a new {@link ByteSource} for reading bytes from the given file.
112   *
113   * @since 14.0
114   */
115  public static ByteSource asByteSource(File file) {
116    return new FileByteSource(file);
117  }
118
119  private static final class FileByteSource extends
120      ByteSource
121  {
122
123    private final File file;
124
125    private FileByteSource(File file) {
126      this.file = checkNotNull(file);
127    }
128
129    @Override
130    public FileInputStream openStream() throws IOException {
131      return new FileInputStream(file);
132    }
133
134    @Override
135    public Optional<Long> sizeIfKnown() {
136      if (file.isFile()) {
137        return Optional.of(file.length());
138      } else {
139        return Optional.absent();
140      }
141    }
142
143    @Override
144    public long size() throws IOException {
145      if (!file.isFile()) {
146        throw new FileNotFoundException(file.toString());
147      }
148      return file.length();
149    }
150
151    @Override
152    public byte[] read() throws IOException {
153      Closer closer = Closer.create();
154      try {
155        FileInputStream in = closer.register(openStream());
156        return ByteStreams.toByteArray(in, in.getChannel().size());
157      } catch (Throwable e) {
158        throw closer.rethrow(e);
159      } finally {
160        closer.close();
161      }
162    }
163
164    @Override
165    public String toString() {
166      return "Files.asByteSource(" + file + ")";
167    }
168  }
169
170  /**
171   * Returns a new {@link ByteSink} for writing bytes to the given file. The given {@code modes}
172   * control how the file is opened for writing. When no mode is provided, the file will be
173   * truncated before writing. When the {@link FileWriteMode#APPEND APPEND} mode is provided, writes
174   * will append to the end of the file without truncating it.
175   *
176   * @since 14.0
177   */
178  public static ByteSink asByteSink(File file, FileWriteMode... modes) {
179    return new FileByteSink(file, modes);
180  }
181
182  private static final class FileByteSink extends ByteSink {
183
184    private final File file;
185    private final ImmutableSet<FileWriteMode> modes;
186
187    private FileByteSink(File file, FileWriteMode... modes) {
188      this.file = checkNotNull(file);
189      this.modes = ImmutableSet.copyOf(modes);
190    }
191
192    @Override
193    public FileOutputStream openStream() throws IOException {
194      return new FileOutputStream(file, modes.contains(APPEND));
195    }
196
197    @Override
198    public String toString() {
199      return "Files.asByteSink(" + file + ", " + modes + ")";
200    }
201  }
202
203  /**
204   * Returns a new {@link CharSource} for reading character data from the given file using the given
205   * character set.
206   *
207   * @since 14.0
208   */
209  public static CharSource asCharSource(File file, Charset charset) {
210    return asByteSource(file).asCharSource(charset);
211  }
212
213  /**
214   * Returns a new {@link CharSink} for writing character data to the given file using the given
215   * character set. The given {@code modes} control how the file is opened for writing. When no mode
216   * is provided, the file will be truncated before writing. When the {@link FileWriteMode#APPEND
217   * APPEND} mode is provided, writes will append to the end of the file without truncating it.
218   *
219   * @since 14.0
220   */
221  public static CharSink asCharSink(File file, Charset charset, FileWriteMode... modes) {
222    return asByteSink(file, modes).asCharSink(charset);
223  }
224
225  /**
226   * Reads all bytes from a file into a byte array.
227   *
228   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}.
229   *
230   * @param file the file to read from
231   * @return a byte array containing all the bytes from file
232   * @throws IllegalArgumentException if the file is bigger than the largest possible byte array
233   *     (2^31 - 1)
234   * @throws IOException if an I/O error occurs
235   */
236  public static byte[] toByteArray(File file) throws IOException {
237    return asByteSource(file).read();
238  }
239
240  /**
241   * Reads all characters from a file into a {@link String}, using the given character set.
242   *
243   * @param file the file to read from
244   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
245   *     helpful predefined constants
246   * @return a string containing all the characters from the file
247   * @throws IOException if an I/O error occurs
248   * @deprecated Prefer {@code asCharSource(file, charset).read()}.
249   */
250  @Deprecated
251  @InlineMe(
252      replacement = "Files.asCharSource(file, charset).read()",
253      imports = "com.google.common.io.Files")
254  public static String toString(File file, Charset charset) throws IOException {
255    return asCharSource(file, charset).read();
256  }
257
258  /**
259   * Overwrites a file with the contents of a byte array.
260   *
261   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
262   * java.nio.file.Files#write(java.nio.file.Path, byte[], java.nio.file.OpenOption...)}.
263   *
264   * @param from the bytes to write
265   * @param to the destination file
266   * @throws IOException if an I/O error occurs
267   */
268  public static void write(byte[] from, File to) throws IOException {
269    asByteSink(to).write(from);
270  }
271
272  /**
273   * Writes a character sequence (such as a string) to a file using the given character set.
274   *
275   * @param from the character sequence to write
276   * @param to the destination file
277   * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
278   *     helpful predefined constants
279   * @throws IOException if an I/O error occurs
280   * @deprecated Prefer {@code asCharSink(to, charset).write(from)}.
281   */
282  @Deprecated
283  @InlineMe(
284      replacement = "Files.asCharSink(to, charset).write(from)",
285      imports = "com.google.common.io.Files")
286  public static void write(CharSequence from, File to, Charset charset) throws IOException {
287    asCharSink(to, charset).write(from);
288  }
289
290  /**
291   * Copies all bytes from a file to an output stream.
292   *
293   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
294   * java.nio.file.Files#copy(java.nio.file.Path, OutputStream)}.
295   *
296   * @param from the source file
297   * @param to the output stream
298   * @throws IOException if an I/O error occurs
299   */
300  public static void copy(File from, OutputStream to) throws IOException {
301    asByteSource(from).copyTo(to);
302  }
303
304  /**
305   * Copies all the bytes from one file to another.
306   *
307   * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process
308   * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you
309   * need to guard against those conditions, you should employ other file-level synchronization.
310   *
311   * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten
312   * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i>
313   * file, the contents of that file will be deleted.
314   *
315   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
316   * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}.
317   *
318   * @param from the source file
319   * @param to the destination file
320   * @throws IOException if an I/O error occurs
321   * @throws IllegalArgumentException if {@code from.equals(to)}
322   */
323  public static void copy(File from, File to) throws IOException {
324    checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to);
325    asByteSource(from).copyTo(asByteSink(to));
326  }
327
328  /**
329   * Copies all characters from a file to an appendable object, using the given character set.
330   *
331   * @param from the source file
332   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
333   *     helpful predefined constants
334   * @param to the appendable object
335   * @throws IOException if an I/O error occurs
336   * @deprecated Prefer {@code asCharSource(from, charset).copyTo(to)}.
337   */
338  @Deprecated
339  @InlineMe(
340      replacement = "Files.asCharSource(from, charset).copyTo(to)",
341      imports = "com.google.common.io.Files")
342  public
343  static void copy(File from, Charset charset, Appendable to) throws IOException {
344    asCharSource(from, charset).copyTo(to);
345  }
346
347  /**
348   * Appends a character sequence (such as a string) to a file using the given character set.
349   *
350   * @param from the character sequence to append
351   * @param to the destination file
352   * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
353   *     helpful predefined constants
354   * @throws IOException if an I/O error occurs
355   * @deprecated Prefer {@code asCharSink(to, charset, FileWriteMode.APPEND).write(from)}.
356   */
357  @Deprecated
358  @InlineMe(
359      replacement = "Files.asCharSink(to, charset, FileWriteMode.APPEND).write(from)",
360      imports = {"com.google.common.io.FileWriteMode", "com.google.common.io.Files"})
361  public
362  static void append(CharSequence from, File to, Charset charset) throws IOException {
363    asCharSink(to, charset, FileWriteMode.APPEND).write(from);
364  }
365
366  /**
367   * Returns true if the given files exist, are not directories, and contain the same bytes.
368   *
369   * @throws IOException if an I/O error occurs
370   */
371  // performance optimization to avoid lexicographical comparison in File.equals
372  @SuppressWarnings("ReferenceEquality")
373  public static boolean equal(File file1, File file2) throws IOException {
374    checkNotNull(file1);
375    checkNotNull(file2);
376    if (file1 == file2 || file1.equals(file2)) {
377      return true;
378    }
379
380    /*
381     * Some operating systems may return zero as the length for files denoting system-dependent
382     * entities such as devices or pipes, in which case we must fall back on comparing the bytes
383     * directly.
384     */
385    long len1 = file1.length();
386    long len2 = file2.length();
387    if (len1 != 0 && len2 != 0 && len1 != len2) {
388      return false;
389    }
390    return asByteSource(file1).contentEquals(asByteSource(file2));
391  }
392
393  /**
394   * Atomically creates a new directory somewhere beneath the system's temporary directory (as
395   * defined by the {@code java.io.tmpdir} system property), and returns its name.
396   *
397   * <p>The temporary directory is created with permissions restricted to the current user or, in
398   * the case of Android, the current app. If that is not possible (as is the case under the very
399   * old Android Ice Cream Sandwich release), then this method throws an exception instead of
400   * creating a directory that would be more accessible. (This behavior is new in Guava 32.0.0.
401   * Previous versions would create a directory that is more accessible, as discussed in <a
402   * href="https://github.com/google/guava/issues/4011">CVE-2020-8908</a>.)
403   *
404   * <p>Use this method instead of {@link File#createTempFile(String, String)} when you wish to
405   * create a directory, not a regular file. A common pitfall is to call {@code createTempFile},
406   * delete the file and create a directory in its place, but this leads a race condition which can
407   * be exploited to create security vulnerabilities, especially when executable files are to be
408   * written into the directory.
409   *
410   * <p>This method assumes that the temporary volume is writable, has free inodes and free blocks,
411   * and that it will not be called thousands of times per second.
412   *
413   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
414   * java.nio.file.Files#createTempDirectory}.
415   *
416   * @return the newly-created directory
417   * @throws IllegalStateException if the directory could not be created, such as if the system does
418   *     not support creating temporary directories securely
419   * @deprecated For Android users, see the <a
420   *     href="https://developer.android.com/training/data-storage" target="_blank">Data and File
421   *     Storage overview</a> to select an appropriate temporary directory (perhaps {@code
422   *     context.getCacheDir()}), and create your own directory under that. (For example, you might
423   *     use {@code new File(context.getCacheDir(), "directoryname").mkdir()}, or, if you need an
424   *     arbitrary number of temporary directories, you might have to generate multiple directory
425   *     names in a loop until {@code mkdir()} returns {@code true}.) For JRE users, prefer {@link
426   *     java.nio.file.Files#createTempDirectory}, transforming it to a {@link File} using {@link
427   *     java.nio.file.Path#toFile() toFile()} if needed. To restrict permissions as this method
428   *     does, pass {@code
429   *     PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"))} to your
430   *     call to {@code createTempDirectory}.
431   */
432  @Beta
433  @Deprecated
434  @J2ObjCIncompatible
435  public static File createTempDir() {
436    return TempFileCreator.INSTANCE.createTempDir();
437  }
438
439  /**
440   * Creates an empty file or updates the last updated timestamp on the same as the unix command of
441   * the same name.
442   *
443   * @param file the file to create or update
444   * @throws IOException if an I/O error occurs
445   */
446  @SuppressWarnings("GoodTime") // reading system time without TimeSource
447  public static void touch(File file) throws IOException {
448    checkNotNull(file);
449    if (!file.createNewFile() && !file.setLastModified(System.currentTimeMillis())) {
450      throw new IOException("Unable to update modification time of " + file);
451    }
452  }
453
454  /**
455   * Creates any necessary but nonexistent parent directories of the specified file. Note that if
456   * this operation fails it may have succeeded in creating some (but not all) of the necessary
457   * parent directories.
458   *
459   * @throws IOException if an I/O error occurs, or if any necessary but nonexistent parent
460   *     directories of the specified file could not be created.
461   * @since 4.0
462   */
463  public static void createParentDirs(File file) throws IOException {
464    checkNotNull(file);
465    File parent = file.getCanonicalFile().getParentFile();
466    if (parent == null) {
467      /*
468       * The given directory is a filesystem root. All zero of its ancestors exist. This doesn't
469       * mean that the root itself exists -- consider x:\ on a Windows machine without such a drive
470       * -- or even that the caller can create it, but this method makes no such guarantees even for
471       * non-root files.
472       */
473      return;
474    }
475    parent.mkdirs();
476    if (!parent.isDirectory()) {
477      throw new IOException("Unable to create parent directories of " + file);
478    }
479  }
480
481  /**
482   * Moves a file from one path to another. This method can rename a file and/or move it to a
483   * different directory. In either case {@code to} must be the target path for the file itself; not
484   * just the new name for the file or the path to the new parent directory.
485   *
486   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#move}.
487   *
488   * @param from the source file
489   * @param to the destination file
490   * @throws IOException if an I/O error occurs
491   * @throws IllegalArgumentException if {@code from.equals(to)}
492   */
493  public static void move(File from, File to) throws IOException {
494    checkNotNull(from);
495    checkNotNull(to);
496    checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to);
497
498    if (!from.renameTo(to)) {
499      copy(from, to);
500      if (!from.delete()) {
501        if (!to.delete()) {
502          throw new IOException("Unable to delete " + to);
503        }
504        throw new IOException("Unable to delete " + from);
505      }
506    }
507  }
508
509  /**
510   * Reads the first line from a file. The line does not include line-termination characters, but
511   * does include other leading and trailing whitespace.
512   *
513   * @param file the file to read from
514   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
515   *     helpful predefined constants
516   * @return the first line, or null if the file is empty
517   * @throws IOException if an I/O error occurs
518   * @deprecated Prefer {@code asCharSource(file, charset).readFirstLine()}.
519   */
520  @Deprecated
521  @InlineMe(
522      replacement = "Files.asCharSource(file, charset).readFirstLine()",
523      imports = "com.google.common.io.Files")
524  public
525  static @Nullable String readFirstLine(File file, Charset charset) throws IOException {
526    return asCharSource(file, charset).readFirstLine();
527  }
528
529  /**
530   * Reads all of the lines from a file. The lines do not include line-termination characters, but
531   * do include other leading and trailing whitespace.
532   *
533   * <p>This method returns a mutable {@code List}. For an {@code ImmutableList}, use {@code
534   * Files.asCharSource(file, charset).readLines()}.
535   *
536   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
537   * java.nio.file.Files#readAllLines(java.nio.file.Path, Charset)}.
538   *
539   * @param file the file to read from
540   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
541   *     helpful predefined constants
542   * @return a mutable {@link List} containing all the lines
543   * @throws IOException if an I/O error occurs
544   */
545  public static List<String> readLines(File file, Charset charset) throws IOException {
546    // don't use asCharSource(file, charset).readLines() because that returns
547    // an immutable list, which would change the behavior of this method
548    return asCharSource(file, charset)
549        .readLines(
550            new LineProcessor<List<String>>() {
551              final List<String> result = new ArrayList<>();
552
553              @Override
554              public boolean processLine(String line) {
555                result.add(line);
556                return true;
557              }
558
559              @Override
560              public List<String> getResult() {
561                return result;
562              }
563            });
564  }
565
566  /**
567   * Streams lines from a {@link File}, stopping when our callback returns false, or we have read
568   * all of the lines.
569   *
570   * @param file the file to read from
571   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
572   *     helpful predefined constants
573   * @param callback the {@link LineProcessor} to use to handle the lines
574   * @return the output of processing the lines
575   * @throws IOException if an I/O error occurs
576   * @deprecated Prefer {@code asCharSource(file, charset).readLines(callback)}.
577   */
578  @Deprecated
579  @InlineMe(
580      replacement = "Files.asCharSource(file, charset).readLines(callback)",
581      imports = "com.google.common.io.Files")
582  @CanIgnoreReturnValue // some processors won't return a useful result
583  @ParametricNullness
584  public
585  static <T extends @Nullable Object> T readLines(
586      File file, Charset charset, LineProcessor<T> callback) throws IOException {
587    return asCharSource(file, charset).readLines(callback);
588  }
589
590  /**
591   * Process the bytes of a file.
592   *
593   * <p>(If this seems too complicated, maybe you're looking for {@link #toByteArray}.)
594   *
595   * @param file the file to read
596   * @param processor the object to which the bytes of the file are passed.
597   * @return the result of the byte processor
598   * @throws IOException if an I/O error occurs
599   * @deprecated Prefer {@code asByteSource(file).read(processor)}.
600   */
601  @Deprecated
602  @InlineMe(
603      replacement = "Files.asByteSource(file).read(processor)",
604      imports = "com.google.common.io.Files")
605  @CanIgnoreReturnValue // some processors won't return a useful result
606  @ParametricNullness
607  public
608  static <T extends @Nullable Object> T readBytes(File file, ByteProcessor<T> processor)
609      throws IOException {
610    return asByteSource(file).read(processor);
611  }
612
613  /**
614   * Computes the hash code of the {@code file} using {@code hashFunction}.
615   *
616   * @param file the file to read
617   * @param hashFunction the hash function to use to hash the data
618   * @return the {@link HashCode} of all of the bytes in the file
619   * @throws IOException if an I/O error occurs
620   * @since 12.0
621   * @deprecated Prefer {@code asByteSource(file).hash(hashFunction)}.
622   */
623  @Deprecated
624  @InlineMe(
625      replacement = "Files.asByteSource(file).hash(hashFunction)",
626      imports = "com.google.common.io.Files")
627  public
628  static HashCode hash(File file, HashFunction hashFunction) throws IOException {
629    return asByteSource(file).hash(hashFunction);
630  }
631
632  /**
633   * Fully maps a file read-only in to memory as per {@link
634   * FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
635   *
636   * <p>Files are mapped from offset 0 to its length.
637   *
638   * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes.
639   *
640   * @param file the file to map
641   * @return a read-only buffer reflecting {@code file}
642   * @throws FileNotFoundException if the {@code file} does not exist
643   * @throws IOException if an I/O error occurs
644   * @see FileChannel#map(MapMode, long, long)
645   * @since 2.0
646   */
647  public static MappedByteBuffer map(File file) throws IOException {
648    checkNotNull(file);
649    return map(file, MapMode.READ_ONLY);
650  }
651
652  /**
653   * Fully maps a file in to memory as per {@link
654   * FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)} using the requested {@link
655   * MapMode}.
656   *
657   * <p>Files are mapped from offset 0 to its length.
658   *
659   * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes.
660   *
661   * @param file the file to map
662   * @param mode the mode to use when mapping {@code file}
663   * @return a buffer reflecting {@code file}
664   * @throws FileNotFoundException if the {@code file} does not exist
665   * @throws IOException if an I/O error occurs
666   * @see FileChannel#map(MapMode, long, long)
667   * @since 2.0
668   */
669  public static MappedByteBuffer map(File file, MapMode mode) throws IOException {
670    return mapInternal(file, mode, -1);
671  }
672
673  /**
674   * Maps a file in to memory as per {@link FileChannel#map(java.nio.channels.FileChannel.MapMode,
675   * long, long)} using the requested {@link MapMode}.
676   *
677   * <p>Files are mapped from offset 0 to {@code size}.
678   *
679   * <p>If the mode is {@link MapMode#READ_WRITE} and the file does not exist, it will be created
680   * with the requested {@code size}. Thus this method is useful for creating memory mapped files
681   * which do not yet exist.
682   *
683   * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes.
684   *
685   * @param file the file to map
686   * @param mode the mode to use when mapping {@code file}
687   * @return a buffer reflecting {@code file}
688   * @throws IOException if an I/O error occurs
689   * @see FileChannel#map(MapMode, long, long)
690   * @since 2.0
691   */
692  public static MappedByteBuffer map(File file, MapMode mode, long size) throws IOException {
693    checkArgument(size >= 0, "size (%s) may not be negative", size);
694    return mapInternal(file, mode, size);
695  }
696
697  private static MappedByteBuffer mapInternal(File file, MapMode mode, long size)
698      throws IOException {
699    checkNotNull(file);
700    checkNotNull(mode);
701
702    Closer closer = Closer.create();
703    try {
704      RandomAccessFile raf =
705          closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
706      FileChannel channel = closer.register(raf.getChannel());
707      return channel.map(mode, 0, size == -1 ? channel.size() : size);
708    } catch (Throwable e) {
709      throw closer.rethrow(e);
710    } finally {
711      closer.close();
712    }
713  }
714
715  /**
716   * Returns the lexically cleaned form of the path name, <i>usually</i> (but not always) equivalent
717   * to the original. The following heuristics are used:
718   *
719   * <ul>
720   *   <li>empty string becomes .
721   *   <li>. stays as .
722   *   <li>fold out ./
723   *   <li>fold out ../ when possible
724   *   <li>collapse multiple slashes
725   *   <li>delete trailing slashes (unless the path is just "/")
726   * </ul>
727   *
728   * <p>These heuristics do not always match the behavior of the filesystem. In particular, consider
729   * the path {@code a/../b}, which {@code simplifyPath} will change to {@code b}. If {@code a} is a
730   * symlink to {@code x}, {@code a/../b} may refer to a sibling of {@code x}, rather than the
731   * sibling of {@code a} referred to by {@code b}.
732   *
733   * @since 11.0
734   */
735  public static String simplifyPath(String pathname) {
736    checkNotNull(pathname);
737    if (pathname.isEmpty()) {
738      return ".";
739    }
740
741    // split the path apart
742    Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname);
743    List<String> path = new ArrayList<>();
744
745    // resolve ., .., and //
746    for (String component : components) {
747      switch (component) {
748        case ".":
749          continue;
750        case "..":
751          if (path.size() > 0 && !path.get(path.size() - 1).equals("..")) {
752            path.remove(path.size() - 1);
753          } else {
754            path.add("..");
755          }
756          break;
757        default:
758          path.add(component);
759          break;
760      }
761    }
762
763    // put it back together
764    String result = Joiner.on('/').join(path);
765    if (pathname.charAt(0) == '/') {
766      result = "/" + result;
767    }
768
769    while (result.startsWith("/../")) {
770      result = result.substring(3);
771    }
772    if (result.equals("/..")) {
773      result = "/";
774    } else if (result.isEmpty()) {
775      result = ".";
776    }
777
778    return result;
779  }
780
781  /**
782   * Returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for
783   * the given file name, or the empty string if the file has no extension. The result does not
784   * include the '{@code .}'.
785   *
786   * <p><b>Note:</b> This method simply returns everything after the last '{@code .}' in the file's
787   * name as determined by {@link File#getName}. It does not account for any filesystem-specific
788   * behavior that the {@link File} API does not already account for. For example, on NTFS it will
789   * report {@code "txt"} as the extension for the filename {@code "foo.exe:.txt"} even though NTFS
790   * will drop the {@code ":.txt"} part of the name when the file is actually created on the
791   * filesystem due to NTFS's <a
792   * href="https://learn.microsoft.com/en-us/archive/blogs/askcore/alternate-data-streams-in-ntfs">Alternate
793   * Data Streams</a>.
794   *
795   * @since 11.0
796   */
797  public static String getFileExtension(String fullName) {
798    checkNotNull(fullName);
799    String fileName = new File(fullName).getName();
800    int dotIndex = fileName.lastIndexOf('.');
801    return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
802  }
803
804  /**
805   * Returns the file name without its <a
806   * href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> or path. This is
807   * similar to the {@code basename} unix command. The result does not include the '{@code .}'.
808   *
809   * @param file The name of the file to trim the extension from. This can be either a fully
810   *     qualified file name (including a path) or just a file name.
811   * @return The file name without its path or extension.
812   * @since 14.0
813   */
814  public static String getNameWithoutExtension(String file) {
815    checkNotNull(file);
816    String fileName = new File(file).getName();
817    int dotIndex = fileName.lastIndexOf('.');
818    return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
819  }
820
821  /**
822   * Returns a {@link Traverser} instance for the file and directory tree. The returned traverser
823   * starts from a {@link File} and will return all files and directories it encounters.
824   *
825   * <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no
826   * way to ensure that a symbolic link to a directory is not followed when traversing the tree. In
827   * this case, iterables created by this traverser could contain files that are outside of the
828   * given directory or even be infinite if there is a symbolic link loop.
829   *
830   * <p>If available, consider using {@link MoreFiles#fileTraverser()} instead. It behaves the same
831   * except that it doesn't follow symbolic links and returns {@code Path} instances.
832   *
833   * <p>If the {@link File} passed to one of the {@link Traverser} methods does not exist or is not
834   * a directory, no exception will be thrown and the returned {@link Iterable} will contain a
835   * single element: that file.
836   *
837   * <p>Example: {@code Files.fileTraverser().depthFirstPreOrder(new File("/"))} may return files
838   * with the following paths: {@code ["/", "/etc", "/etc/config.txt", "/etc/fonts", "/home",
839   * "/home/alice", ...]}
840   *
841   * @since 23.5
842   */
843  public static Traverser<File> fileTraverser() {
844    return Traverser.forTree(FILE_TREE);
845  }
846
847  private static final SuccessorsFunction<File> FILE_TREE =
848      file -> {
849        // check isDirectory() just because it may be faster than listFiles() on a non-directory
850        if (file.isDirectory()) {
851          File[] files = file.listFiles();
852          if (files != null) {
853            return unmodifiableList(asList(files));
854          }
855        }
856
857        return ImmutableList.of();
858      };
859
860  /**
861   * Returns a predicate that returns the result of {@link File#isDirectory} on input files.
862   *
863   * @since 15.0
864   */
865  public static Predicate<File> isDirectory() {
866    return FilePredicate.IS_DIRECTORY;
867  }
868
869  /**
870   * Returns a predicate that returns the result of {@link File#isFile} on input files.
871   *
872   * @since 15.0
873   */
874  public static Predicate<File> isFile() {
875    return FilePredicate.IS_FILE;
876  }
877
878  private enum FilePredicate implements Predicate<File> {
879    IS_DIRECTORY {
880      @Override
881      public boolean apply(File file) {
882        return file.isDirectory();
883      }
884
885      @Override
886      public String toString() {
887        return "Files.isDirectory()";
888      }
889    },
890
891    IS_FILE {
892      @Override
893      public boolean apply(File file) {
894        return file.isFile();
895      }
896
897      @Override
898      public String toString() {
899        return "Files.isFile()";
900      }
901    }
902  }
903}