FileSystemUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.Files;
  21. import java.nio.file.Path;
  22. import java.nio.file.Paths;
  23. import java.util.Objects;

  24. /**
  25.  * General File System utilities.
  26.  * <p>
  27.  * This class provides static utility methods for general file system functions not provided before Java 6's {@link File File} class.
  28.  * </p>
  29.  * <p>
  30.  * The current functions provided are:
  31.  * </p>
  32.  * <ul>
  33.  * <li>Get the free space on a drive</li>
  34.  * </ul>
  35.  *
  36.  * @since 1.1
  37.  * @deprecated As of 2.6 deprecated without replacement. Use equivalent methods in {@link java.nio.file.FileStore} instead,
  38.  *             {@code Files.getFileStore(Paths.get("/home")).getUsableSpace()} or iterate over {@code FileSystems.getDefault().getFileStores()}
  39.  */
  40. @Deprecated
  41. public class FileSystemUtils {

  42.     /**
  43.      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
  44.      * <p>
  45.      * Note that some OS's are NOT currently supported, including OS/390, OpenVMS.
  46.      * </p>
  47.      *
  48.      * <pre>
  49.      * FileSystemUtils.freeSpace("C:"); // Windows
  50.      * FileSystemUtils.freeSpace("/volume"); // *nix
  51.      * </pre>
  52.      *
  53.      * @param path the path to get free space for, not null, not empty on Unix
  54.      * @return the amount of free drive space on the drive or volume
  55.      * @throws IOException              if an I/O error occurs.
  56.      * @throws IllegalArgumentException if the path is invalid.
  57.      * @since 1.1, enhanced OS support in 1.2 and 1.3
  58.      * @deprecated Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0
  59.      */
  60.     @Deprecated
  61.     public static long freeSpace(final String path) throws IOException {
  62.         return getFreeSpace(path);
  63.     }

  64.     /**
  65.      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
  66.      * <p>
  67.      * Identical to:
  68.      * </p>
  69.      *
  70.      * <pre>
  71.      * freeSpaceKb(FileUtils.current().getAbsolutePath())
  72.      * </pre>
  73.      *
  74.      * @return the amount of free drive space on the drive or volume in kilobytes
  75.      * @throws IOException              if an I/O error occurs.
  76.      * @throws IllegalArgumentException if the path is invalid.
  77.      * @since 2.0
  78.      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
  79.      */
  80.     @Deprecated
  81.     public static long freeSpaceKb() throws IOException {
  82.         return freeSpaceKb(-1);
  83.     }

  84.     /**
  85.      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
  86.      * <p>
  87.      * Identical to:
  88.      * </p>
  89.      *
  90.      * <pre>
  91.      * freeSpaceKb(FileUtils.current().getAbsolutePath())
  92.      * </pre>
  93.      *
  94.      * @param timeout ignored.
  95.      * @return the amount of free drive space on the drive or volume in kilobytes
  96.      * @throws IOException              if an I/O error occurs.
  97.      * @throws IllegalArgumentException if the path is invalid.
  98.      * @since 2.0
  99.      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
  100.      */
  101.     @Deprecated
  102.     public static long freeSpaceKb(final long timeout) throws IOException {
  103.         return freeSpaceKb(FileUtils.current().getAbsolutePath(), timeout);
  104.     }

  105.     /**
  106.      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
  107.      *
  108.      * <pre>
  109.      * FileSystemUtils.freeSpaceKb("C:"); // Windows
  110.      * FileSystemUtils.freeSpaceKb("/volume"); // *nix
  111.      * </pre>
  112.      *
  113.      * @param path the path to get free space for, not null, not empty on Unix
  114.      * @return the amount of free drive space on the drive or volume in kilobytes
  115.      * @throws IOException              if an I/O error occurs.
  116.      * @throws IllegalArgumentException if the path is invalid.
  117.      * @since 1.2, enhanced OS support in 1.3
  118.      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
  119.      */
  120.     @Deprecated
  121.     public static long freeSpaceKb(final String path) throws IOException {
  122.         return freeSpaceKb(path, -1);
  123.     }

  124.     /**
  125.      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
  126.      *
  127.      * <pre>
  128.      * FileSystemUtils.freeSpaceKb("C:"); // Windows
  129.      * FileSystemUtils.freeSpaceKb("/volume"); // *nix
  130.      * </pre>
  131.      *
  132.      * @param path    the path to get free space for, not null, not empty on Unix
  133.      * @param timeout ignored.
  134.      * @return the amount of free drive space on the drive or volume in kilobytes
  135.      * @throws IOException              if an I/O error occurs.
  136.      * @throws IllegalArgumentException if the path is invalid.
  137.      * @since 2.0
  138.      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
  139.      */
  140.     @Deprecated
  141.     public static long freeSpaceKb(final String path, final long timeout) throws IOException {
  142.         return getFreeSpace(path) / FileUtils.ONE_KB;
  143.     }

  144.     /**
  145.      * Gets the number of bytes available to this Java virtual machine on the given file store.
  146.      *
  147.      * <pre>
  148.      * FileSystemUtils.freeSpace("C:"); // Windows
  149.      * FileSystemUtils.freeSpace("/volume"); // *nix
  150.      * </pre>
  151.      *
  152.      * @param pathStr the path to get free space for, not null, not empty on Unix
  153.      * @return the amount of free drive space on the drive or volume
  154.      * @throws IOException              if an I/O error occurs.
  155.      * @throws IllegalArgumentException if the path is invalid.
  156.      */
  157.     static long getFreeSpace(final String pathStr) throws IOException {
  158.         final Path path = Paths.get(Objects.requireNonNull(pathStr, "pathStr"));
  159.         if (Files.exists(path)) {
  160.             // Need an absolute path for input like "" to work
  161.             return Files.getFileStore(path.toAbsolutePath()).getUsableSpace();
  162.             // return path.toAbsolutePath().toFile().getUsableSpace();
  163.         }
  164.         throw new IllegalArgumentException(path.toString());
  165.     }

  166.     /**
  167.      * Instances should NOT be constructed in standard programming.
  168.      *
  169.      * @deprecated TODO Make private in 3.0.
  170.      */
  171.     @Deprecated
  172.     public FileSystemUtils() {
  173.         // empty
  174.     }

  175. }