View Javadoc
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  
19  import java.io.File;
20  import java.io.IOException;
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.Objects;
25  
26  /**
27   * General File System utilities.
28   * <p>
29   * This class provides static utility methods for general file system functions not provided before Java 6's {@link File File} class.
30   * </p>
31   * <p>
32   * The current functions provided are:
33   * </p>
34   * <ul>
35   * <li>Get the free space on a drive</li>
36   * </ul>
37   *
38   * @since 1.1
39   * @deprecated As of 2.6 deprecated without replacement. Use equivalent methods in {@link java.nio.file.FileStore} instead,
40   *             {@code Files.getFileStore(Paths.get("/home")).getUsableSpace()} or iterate over {@code FileSystems.getDefault().getFileStores()}
41   */
42  @Deprecated
43  public class FileSystemUtils {
44  
45      /**
46       * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
47       * <p>
48       * Note that some OS's are NOT currently supported, including OS/390, OpenVMS.
49       * </p>
50       *
51       * <pre>
52       * FileSystemUtils.freeSpace("C:"); // Windows
53       * FileSystemUtils.freeSpace("/volume"); // *nix
54       * </pre>
55       *
56       * @param path the path to get free space for, not null, not empty on Unix
57       * @return the amount of free drive space on the drive or volume
58       * @throws IOException              if an I/O error occurs.
59       * @throws IllegalArgumentException if the path is invalid.
60       * @since 1.1, enhanced OS support in 1.2 and 1.3
61       * @deprecated Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0
62       */
63      @Deprecated
64      public static long freeSpace(final String path) throws IOException {
65          return getFreeSpace(path);
66      }
67  
68      /**
69       * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
70       * <p>
71       * Identical to:
72       * </p>
73       *
74       * <pre>
75       * freeSpaceKb(FileUtils.current().getAbsolutePath())
76       * </pre>
77       *
78       * @return the amount of free drive space on the drive or volume in kilobytes
79       * @throws IOException              if an I/O error occurs.
80       * @throws IllegalArgumentException if the path is invalid.
81       * @since 2.0
82       * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
83       */
84      @Deprecated
85      public static long freeSpaceKb() throws IOException {
86          return freeSpaceKb(-1);
87      }
88  
89      /**
90       * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
91       * <p>
92       * Identical to:
93       * </p>
94       *
95       * <pre>
96       * freeSpaceKb(FileUtils.current().getAbsolutePath())
97       * </pre>
98       *
99       * @param timeout ignored.
100      * @return the amount of free drive space on the drive or volume in kilobytes
101      * @throws IOException              if an I/O error occurs.
102      * @throws IllegalArgumentException if the path is invalid.
103      * @since 2.0
104      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
105      */
106     @Deprecated
107     public static long freeSpaceKb(final long timeout) throws IOException {
108         return freeSpaceKb(FileUtils.current().getAbsolutePath(), timeout);
109     }
110 
111     /**
112      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
113      *
114      * <pre>
115      * FileSystemUtils.freeSpaceKb("C:"); // Windows
116      * FileSystemUtils.freeSpaceKb("/volume"); // *nix
117      * </pre>
118      *
119      * @param path the path to get free space for, not null, not empty on Unix
120      * @return the amount of free drive space on the drive or volume in kilobytes
121      * @throws IOException              if an I/O error occurs.
122      * @throws IllegalArgumentException if the path is invalid.
123      * @since 1.2, enhanced OS support in 1.3
124      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
125      */
126     @Deprecated
127     public static long freeSpaceKb(final String path) throws IOException {
128         return freeSpaceKb(path, -1);
129     }
130 
131     /**
132      * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
133      *
134      * <pre>
135      * FileSystemUtils.freeSpaceKb("C:"); // Windows
136      * FileSystemUtils.freeSpaceKb("/volume"); // *nix
137      * </pre>
138      *
139      * @param path    the path to get free space for, not null, not empty on Unix
140      * @param timeout ignored.
141      * @return the amount of free drive space on the drive or volume in kilobytes
142      * @throws IOException              if an I/O error occurs.
143      * @throws IllegalArgumentException if the path is invalid.
144      * @since 2.0
145      * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
146      */
147     @Deprecated
148     public static long freeSpaceKb(final String path, final long timeout) throws IOException {
149         return getFreeSpace(path) / FileUtils.ONE_KB;
150     }
151 
152     /**
153      * Gets the number of bytes available to this Java virtual machine on the given file store.
154      *
155      * <pre>
156      * FileSystemUtils.freeSpace("C:"); // Windows
157      * FileSystemUtils.freeSpace("/volume"); // *nix
158      * </pre>
159      *
160      * @param pathStr the path to get free space for, not null, not empty on Unix
161      * @return the amount of free drive space on the drive or volume
162      * @throws IOException              if an I/O error occurs.
163      * @throws IllegalArgumentException if the path is invalid.
164      */
165     static long getFreeSpace(final String pathStr) throws IOException {
166         final Path path = Paths.get(Objects.requireNonNull(pathStr, "pathStr"));
167         if (Files.exists(path)) {
168             // Need an absolute path for input like "" to work
169             return Files.getFileStore(path.toAbsolutePath()).getUsableSpace();
170             // return path.toAbsolutePath().toFile().getUsableSpace();
171         }
172         throw new IllegalArgumentException(path.toString());
173     }
174 
175     /**
176      * Instances should NOT be constructed in standard programming.
177      *
178      * @deprecated TODO Make private in 3.0.
179      */
180     @Deprecated
181     public FileSystemUtils() {
182         // empty
183     }
184 
185 }