001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io; 018 019import java.io.File; 020import java.io.IOException; 021import java.nio.file.Files; 022import java.nio.file.Path; 023import java.nio.file.Paths; 024import java.util.Objects; 025 026/** 027 * General File System utilities. 028 * <p> 029 * This class provides static utility methods for general file system functions not provided before Java 6's {@link File File} class. 030 * </p> 031 * <p> 032 * The current functions provided are: 033 * </p> 034 * <ul> 035 * <li>Get the free space on a drive</li> 036 * </ul> 037 * 038 * @since 1.1 039 * @deprecated As of 2.6 deprecated without replacement. Use equivalent methods in {@link java.nio.file.FileStore} instead, 040 * {@code Files.getFileStore(Paths.get("/home")).getUsableSpace()} or iterate over {@code FileSystems.getDefault().getFileStores()} 041 */ 042@Deprecated 043public class FileSystemUtils { 044 045 /** 046 * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store. 047 * <p> 048 * Note that some OS's are NOT currently supported, including OS/390, OpenVMS. 049 * </p> 050 * 051 * <pre> 052 * FileSystemUtils.freeSpace("C:"); // Windows 053 * FileSystemUtils.freeSpace("/volume"); // *nix 054 * </pre> 055 * 056 * @param path the path to get free space for, not null, not empty on UNIX 057 * @return the amount of free drive space on the drive or volume 058 * @throws IOException if an I/O error occurs. 059 * @throws IllegalArgumentException if the path is invalid. 060 * @since 1.1, enhanced OS support in 1.2 and 1.3 061 * @deprecated Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0 062 */ 063 @Deprecated 064 public static long freeSpace(final String path) throws IOException { 065 return getFreeSpace(path); 066 } 067 068 /** 069 * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store. 070 * <p> 071 * Identical to: 072 * </p> 073 * 074 * <pre> 075 * freeSpaceKb(FileUtils.current().getAbsolutePath()) 076 * </pre> 077 * 078 * @return the amount of free drive space on the drive or volume in kilobytes 079 * @throws IOException if an I/O error occurs. 080 * @throws IllegalArgumentException if the path is invalid. 081 * @since 2.0 082 * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}. 083 */ 084 @Deprecated 085 public static long freeSpaceKb() throws IOException { 086 return freeSpaceKb(-1); 087 } 088 089 /** 090 * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store. 091 * <p> 092 * Identical to: 093 * </p> 094 * 095 * <pre> 096 * freeSpaceKb(FileUtils.current().getAbsolutePath()) 097 * </pre> 098 * 099 * @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}