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;
020
021import com.google.common.annotations.Beta;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import com.google.common.base.Joiner;
025import com.google.common.base.Optional;
026import com.google.common.base.Predicate;
027import com.google.common.base.Splitter;
028import com.google.common.collect.ImmutableList;
029import com.google.common.collect.ImmutableSet;
030import com.google.common.collect.Lists;
031import com.google.common.graph.SuccessorsFunction;
032import com.google.common.graph.Traverser;
033import com.google.common.hash.HashCode;
034import com.google.common.hash.HashFunction;
035import com.google.errorprone.annotations.CanIgnoreReturnValue;
036import com.google.errorprone.annotations.InlineMe;
037import com.google.j2objc.annotations.J2ObjCIncompatible;
038import java.io.BufferedReader;
039import java.io.BufferedWriter;
040import java.io.File;
041import java.io.FileInputStream;
042import java.io.FileNotFoundException;
043import java.io.FileOutputStream;
044import java.io.IOException;
045import java.io.InputStreamReader;
046import java.io.OutputStream;
047import java.io.OutputStreamWriter;
048import java.io.RandomAccessFile;
049import java.nio.MappedByteBuffer;
050import java.nio.channels.FileChannel;
051import java.nio.channels.FileChannel.MapMode;
052import java.nio.charset.Charset;
053import java.nio.charset.StandardCharsets;
054import java.util.ArrayList;
055import java.util.Arrays;
056import java.util.Collections;
057import java.util.List;
058import org.jspecify.annotations.Nullable;
059
060/**
061 * Provides utility methods for working with {@linkplain File files}.
062 *
063 * <p>{@link java.nio.file.Path} users will find similar utilities in {@link MoreFiles} and the
064 * JDK's {@link java.nio.file.Files} class.
065 *
066 * @author Chris Nokleberg
067 * @author Colin Decker
068 * @since 1.0
069 */
070@J2ktIncompatible
071@GwtIncompatible
072public final class Files {
073
074  private Files() {}
075
076  /**
077   * Returns a buffered reader that reads from a file using the given character set.
078   *
079   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
080   * java.nio.file.Files#newBufferedReader(java.nio.file.Path, Charset)}.
081   *
082   * @param file the file to read from
083   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
084   *     helpful predefined constants
085   * @return the buffered reader
086   */
087  public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException {
088    checkNotNull(file);
089    checkNotNull(charset);
090    return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
091  }
092
093  /**
094   * Returns a buffered writer that writes to a file using the given character set.
095   *
096   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
097   * java.nio.file.Files#newBufferedWriter(java.nio.file.Path, Charset,
098   * java.nio.file.OpenOption...)}.
099   *
100   * @param file the file to write to
101   * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
102   *     helpful predefined constants
103   * @return the buffered writer
104   */
105  public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException {
106    checkNotNull(file);
107    checkNotNull(charset);
108    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
109  }
110
111  /**
112   * Returns a new {@link ByteSource} for reading bytes from the given file.
113   *
114   * @since 14.0
115   */
116  public static ByteSource asByteSource(File file) {
117    return new FileByteSource(file);
118  }
119
120  private static final class FileByteSource extends
121      ByteSource
122  {
123
124    private final File file;
125
126    private FileByteSource(File file) {
127      this.file = checkNotNull(file);
128    }
129
130    @Override
131    public FileInputStream openStream() throws IOException {
132      return new FileInputStream(file);
133    }
134
135    @Override
136    public Optional<Long> sizeIfKnown() {
137      if (file.isFile()) {
138        return Optional.of(file.length());
139      } else {
140        return Optional.absent();
141      }
142    }
143
144    @Override
145    public long size() throws IOException {
146      if (!file.isFile()) {
147        throw new FileNotFoundException(file.toString());
148      }
149      return file.length();
150    }
151
152    @Override
153    public byte[] read() throws IOException {
154      Closer closer = Closer.create();
155      try {
156        FileInputStream in = closer.register(openStream());
157        return ByteStreams.toByteArray(in, in.getChannel().size());
158      } catch (Throwable e) {
159        throw closer.rethrow(e);
160      } finally {
161        closer.close();
162      }
163    }
164
165    @Override
166    public String toString() {
167      return "Files.asByteSource(" + file + ")";
168    }
169  }
170
171  /**
172   * Returns a new {@link ByteSink} for writing bytes to the given file. The given {@code modes}
173   * control how the file is opened for writing. When no mode is provided, the file will be
174   * truncated before writing. When the {@link FileWriteMode#APPEND APPEND} mode is provided, writes
175   * will append to the end of the file without truncating it.
176   *
177   * @since 14.0
178   */
179  public static ByteSink asByteSink(File file, FileWriteMode... modes) {
180    return new FileByteSink(file, modes);
181  }
182
183  private static final class FileByteSink extends ByteSink {
184
185    private final File file;
186    private final ImmutableSet<FileWriteMode> modes;
187
188    private FileByteSink(File file, FileWriteMode... modes) {
189      this.file = checkNotNull(file);
190      this.modes = ImmutableSet.copyOf(modes);
191    }
192
193    @Override
194    public FileOutputStream openStream() throws IOException {
195      return new FileOutputStream(file, modes.contains(APPEND));
196    }
197
198    @Override
199    public String toString() {
200      return "Files.asByteSink(" + file + ", " + modes + ")";
201    }
202  }
203
204  /**
205   * Returns a new {@link CharSource} for reading character data from the given file using the given
206   * character set.
207   *
208   * @since 14.0
209   */
210  public static CharSource asCharSource(File file, Charset charset) {
211    return asByteSource(file).asCharSource(charset);
212  }
213
214  /**
215   * Returns a new {@link CharSink} for writing character data to the given file using the given
216   * character set. The given {@code modes} control how the file is opened for writing. When no mode
217   * is provided, the file will be truncated before writing. When the {@link FileWriteMode#APPEND
218   * APPEND} mode is provided, writes will append to the end of the file without truncating it.
219   *
220   * @since 14.0
221   */
222  public static CharSink asCharSink(File file, Charset charset, FileWriteMode... modes) {
223    return asByteSink(file, modes).asCharSink(charset);
224  }
225
226  /**
227   * Reads all bytes from a file into a byte array.
228   *
229   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}.
230   *
231   * @param file the file to read from
232   * @return a byte array containing all the bytes from file
233   * @throws IllegalArgumentException if the file is bigger than the largest possible byte array
234   *     (2^31 - 1)
235   * @throws IOException if an I/O error occurs
236   */
237  public static byte[] toByteArray(File file) throws IOException {
238    return asByteSource(file).read();
239  }
240
241  /**
242   * Reads all characters from a file into a {@link String}, using the given character set.
243   *
244   * @param file the file to read from
245   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
246   *     helpful predefined constants
247   * @return a string containing all the characters from the file
248   * @throws IOException if an I/O error occurs
249   * @deprecated Prefer {@code asCharSource(file, charset).read()}.
250   */
251  @Deprecated
252  @InlineMe(
253      replacement = "Files.asCharSource(file, charset).read()",
254      imports = "com.google.common.io.Files")
255  public static String toString(File file, Charset charset) throws IOException {
256    return asCharSource(file, charset).read();
257  }
258
259  /**
260   * Overwrites a file with the contents of a byte array.
261   *
262   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
263   * java.nio.file.Files#write(java.nio.file.Path, byte[], java.nio.file.OpenOption...)}.
264   *
265   * @param from the bytes to write
266   * @param to the destination file
267   * @throws IOException if an I/O error occurs
268   */
269  public static void write(byte[] from, File to) throws IOException {
270    asByteSink(to).write(from);
271  }
272
273  /**
274   * Writes a character sequence (such as a string) to a file using the given character set.
275   *
276   * @param from the character sequence to write
277   * @param to the destination file
278   * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
279   *     helpful predefined constants
280   * @throws IOException if an I/O error occurs
281   * @deprecated Prefer {@code asCharSink(to, charset).write(from)}.
282   */
283  @Deprecated
284  @InlineMe(
285      replacement = "Files.asCharSink(to, charset).write(from)",
286      imports = "com.google.common.io.Files")
287  public static void write(CharSequence from, File to, Charset charset) throws IOException {
288    asCharSink(to, charset).write(from);
289  }
290
291  /**
292   * Copies all bytes from a file to an output stream.
293   *
294   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
295   * java.nio.file.Files#copy(java.nio.file.Path, OutputStream)}.
296   *
297   * @param from the source file
298   * @param to the output stream
299   * @throws IOException if an I/O error occurs
300   */
301  public static void copy(File from, OutputStream to) throws IOException {
302    asByteSource(from).copyTo(to);
303  }
304
305  /**
306   * Copies all the bytes from one file to another.
307   *
308   * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process
309   * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you
310   * need to guard against those conditions, you should employ other file-level synchronization.
311   *
312   * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten
313   * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i>
314   * file, the contents of that file will be deleted.
315   *
316   * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
317   * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}.
318   *
319   * @param from the source file
320   * @param to the destination file
321   * @throws IOException if an I/O error occurs
322   * @throws IllegalArgumentException if {@code from.equals(to)}
323   */
324  public static void copy(File from, File to) throws IOException {
325    checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to);
326    asByteSource(from).copyTo(asByteSink(to));
327  }
328
329  /**
330   * Copies all characters from a file to an appendable object, using the given character set.
331   *
332   * @param from the source file
333   * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
334   *     helpful predefined constants
335   * @param to the appendable object
336   * @throws IOException if an I/O error occurs
337   * @deprecated Prefer {@code asCharSource(from, charset).copyTo(to)}.
338   */
339  @Deprecated
340  @InlineMe(
341      replacement = "Files.asCharSource(from, charset).copyTo(to)",
342      imports = "com.google.common.io.Files")
343  public
344  static void copy(File from, Charset charset, Appendable to) throws IOException {
345    asCharSource(from, charset).copyTo(to);
346  }
347
348  /**
349   * Appends a character sequence (such as a string) to a file using the given character set.
350   *
351   * @param from the character sequence to append
352   * @param to the destination file
353   * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
354   *     helpful predefined constants
355   * @throws IOException if an I/O error occurs
356   * @deprecated Prefer {@code asCharSink(to, charset, FileWriteMode.APPEND).write(from)}. This
357   *     method is scheduled to be removed in October 2019.
358   */
359  @Deprecated
360  @InlineMe(
361      replacement = "Files.asCharSink(to, charset, FileWriteMode.APPEND).write(from)",
362      imports = {"com.google.common.io.FileWriteMode", "com.google.common.io.Files"})
363  public
364  static void append(CharSequence from, File to, Charset charset) throws IOException {
365    asCharSink(to, charset, FileWriteMode.APPEND).write(from);
366  }
367
368  /**
369   * Returns true if the given files exist, are not directories, and contain the same bytes.
370   *
371   * @throws IOException if an I/O error occurs
372   */
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 = Lists.newArrayList();
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.length() == 0) {
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 ("".equals(result)) {
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      new SuccessorsFunction<File>() {
849        @Override
850        public Iterable<File> successors(File file) {
851          // check isDirectory() just because it may be faster than listFiles() on a non-directory
852          if (file.isDirectory()) {
853            File[] files = file.listFiles();
854            if (files != null) {
855              return Collections.unmodifiableList(Arrays.asList(files));
856            }
857          }
858
859          return ImmutableList.of();
860        }
861      };
862
863  /**
864   * Returns a predicate that returns the result of {@link File#isDirectory} on input files.
865   *
866   * @since 15.0
867   */
868  public static Predicate<File> isDirectory() {
869    return FilePredicate.IS_DIRECTORY;
870  }
871
872  /**
873   * Returns a predicate that returns the result of {@link File#isFile} on input files.
874   *
875   * @since 15.0
876   */
877  public static Predicate<File> isFile() {
878    return FilePredicate.IS_FILE;
879  }
880
881  private enum FilePredicate implements Predicate<File> {
882    IS_DIRECTORY {
883      @Override
884      public boolean apply(File file) {
885        return file.isDirectory();
886      }
887
888      @Override
889      public String toString() {
890        return "Files.isDirectory()";
891      }
892    },
893
894    IS_FILE {
895      @Override
896      public boolean apply(File file) {
897        return file.isFile();
898      }
899
900      @Override
901      public String toString() {
902        return "Files.isFile()";
903      }
904    }
905  }
906}