一、前言
关于apache的commons-io源码包org.apache.commons.io.FileSystemUtils文件系统工具类,对指定路径系统进行空间释放freeSpace、打开执行进程命令操作openProcess等操作,其中依赖org.apache.commons.io.FilenameUtils类参见其他文章内容,详情参见源码说明。
二、源码说明
package org.apache.commons.io;@b@@b@import java.io.BufferedReader;@b@import java.io.File;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.List;@b@import java.util.Locale;@b@import java.util.StringTokenizer;@b@@b@public class FileSystemUtils@b@{@b@ private static final FileSystemUtils INSTANCE = new FileSystemUtils();@b@ private static final int INIT_PROBLEM = -1;@b@ private static final int OTHER = 0;@b@ private static final int WINDOWS = 1;@b@ private static final int UNIX = 2;@b@ private static final int POSIX_UNIX = 3;@b@ private static final int OS;@b@ private static final String DF;@b@@b@ @Deprecated@b@ public static long freeSpace(String path)@b@ throws IOException@b@ {@b@ return INSTANCE.freeSpaceOS(path, OS, false, -1L);@b@ }@b@@b@ public static long freeSpaceKb(String path)@b@ throws IOException@b@ {@b@ return freeSpaceKb(path, -1L);@b@ }@b@@b@ public static long freeSpaceKb(String path, long timeout)@b@ throws IOException@b@ {@b@ return INSTANCE.freeSpaceOS(path, OS, true, timeout);@b@ }@b@@b@ public static long freeSpaceKb()@b@ throws IOException@b@ {@b@ return freeSpaceKb(-1L);@b@ }@b@@b@ public static long freeSpaceKb(long timeout)@b@ throws IOException@b@ {@b@ return freeSpaceKb(new File(".").getAbsolutePath(), timeout);@b@ }@b@@b@ long freeSpaceOS(String path, int os, boolean kb, long timeout)@b@ throws IOException@b@ {@b@ if (path == null)@b@ throw new IllegalArgumentException("Path must not be empty");@b@@b@ switch (os)@b@ {@b@ case 1:@b@ return ((kb) ? freeSpaceWindows(path, timeout) / 1024L : freeSpaceWindows(path, timeout));@b@ case 2:@b@ return freeSpaceUnix(path, kb, false, timeout);@b@ case 3:@b@ return freeSpaceUnix(path, kb, true, timeout);@b@ case 0:@b@ throw new IllegalStateException("Unsupported operating system");@b@ }@b@ throw new IllegalStateException("Exception caught when determining operating system");@b@ }@b@@b@ long freeSpaceWindows(String path, long timeout)@b@ throws IOException@b@ {@b@ path = FilenameUtils.normalize(path, false);@b@ if ((path.length() > 0) && (path.charAt(0) != '"')) {@b@ path = new StringBuilder().append("\"").append(path).append("\"").toString();@b@ }@b@@b@ String[] cmdAttribs = { "cmd.exe", "/C", new StringBuilder().append("dir /a /-c ").append(path).toString() };@b@@b@ List lines = performCommand(cmdAttribs, 2147483647, timeout);@b@@b@ for (int i = lines.size() - 1; i >= 0; --i) {@b@ String line = (String)lines.get(i);@b@ if (line.length() > 0)@b@ return parseDir(line, path);@b@@b@ }@b@@b@ throw new IOException(new StringBuilder().append("Command line 'dir /-c' did not return any info for path '").append(path).append("'").toString());@b@ }@b@@b@ long parseDir(String line, String path)@b@ throws IOException@b@ {@b@ char c;@b@ int bytesStart = 0;@b@ int bytesEnd = 0;@b@ int j = line.length() - 1;@b@ while (j >= 0) {@b@ c = line.charAt(j);@b@ if (Character.isDigit(c))@b@ {@b@ bytesEnd = j + 1;@b@ break;@b@ }@b@ --j;@b@ }@b@ while (j >= 0) {@b@ c = line.charAt(j);@b@ if ((!(Character.isDigit(c))) && (c != ',') && (c != '.'))@b@ {@b@ bytesStart = j + 1;@b@ break;@b@ }@b@ --j;@b@ }@b@ if (j < 0) {@b@ throw new IOException(new StringBuilder().append("Command line 'dir /-c' did not return valid info for path '").append(path).append("'").toString());@b@ }@b@@b@ StringBuilder buf = new StringBuilder(line.substring(bytesStart, bytesEnd));@b@ for (int k = 0; k < buf.length(); ++k)@b@ if ((buf.charAt(k) == ',') || (buf.charAt(k) == '.'))@b@ buf.deleteCharAt(k--);@b@@b@@b@ return parseBytes(buf.toString(), path); } @b@ long freeSpaceUnix(String path, boolean kb, boolean posix, long timeout) throws IOException { @b@ } @b@ long parseBytes(String freeSpace, String path) throws IOException { long bytes;@b@ try { bytes = Long.parseLong(freeSpace);@b@ if (bytes < 0L) {@b@ throw new IOException(new StringBuilder().append("Command line '").append(DF).append("' did not find free space in response ").append("for path '").append(path).append("'- check path is valid").toString());@b@ }@b@@b@ return bytes;@b@ }@b@ catch (NumberFormatException ex) {@b@ throw new IOExceptionWithCause(new StringBuilder().append("Command line '").append(DF).append("' did not return numeric data as expected ").append("for path '").append(path).append("'- check path is valid").toString(), ex);@b@ }@b@ }@b@@b@ List<String> performCommand(String[] cmdAttribs, int max, long timeout)@b@ throws IOException@b@ {@b@ List lines = new ArrayList(20);@b@ Process proc = null;@b@ InputStream in = null;@b@ OutputStream out = null;@b@ InputStream err = null;@b@ BufferedReader inr = null;@b@ try@b@ {@b@ Thread monitor = ThreadMonitor.start(timeout);@b@@b@ proc = openProcess(cmdAttribs);@b@ in = proc.getInputStream();@b@ out = proc.getOutputStream();@b@ err = proc.getErrorStream();@b@ inr = new BufferedReader(new InputStreamReader(in));@b@ String line = inr.readLine();@b@ while ((line != null) && (lines.size() < max)) {@b@ line = line.toLowerCase(Locale.ENGLISH).trim();@b@ lines.add(line);@b@ line = inr.readLine();@b@ }@b@@b@ proc.waitFor();@b@@b@ ThreadMonitor.stop(monitor);@b@@b@ if (proc.exitValue() != 0)@b@ {@b@ throw new IOException(new StringBuilder().append("Command line returned OS error code '").append(proc.exitValue()).append("' for command ").append(Arrays.asList(cmdAttribs)).toString());@b@ }@b@@b@ if (lines.isEmpty())@b@ {@b@ throw new IOException(new StringBuilder().append("Command line did not return any info for command ").append(Arrays.asList(cmdAttribs)).toString());@b@ }@b@@b@ List localList1 = lines;@b@@b@ return localList1;@b@ }@b@ catch (InterruptedException ex)@b@ {@b@ }@b@ finally@b@ {@b@ IOUtils.closeQuietly(in);@b@ IOUtils.closeQuietly(out);@b@ IOUtils.closeQuietly(err);@b@ IOUtils.closeQuietly(inr);@b@ if (proc != null)@b@ proc.destroy();@b@ }@b@ }@b@@b@ Process openProcess(String[] cmdAttribs)@b@ throws IOException@b@ {@b@ return Runtime.getRuntime().exec(cmdAttribs);@b@ }@b@@b@ static@b@ {@b@ int os = 0;@b@ String dfPath = "df";@b@ try {@b@ String osName = System.getProperty("os.name");@b@ if (osName == null)@b@ throw new IOException("os.name not found");@b@@b@ osName = osName.toLowerCase(Locale.ENGLISH);@b@@b@ if (osName.indexOf("windows") != -1) {@b@ os = 1;@b@ } else if ((osName.indexOf("linux") != -1) || (osName.indexOf("mpe/ix") != -1) || (osName.indexOf("freebsd") != -1) || (osName.indexOf("irix") != -1) || (osName.indexOf("digital unix") != -1) || (osName.indexOf("unix") != -1) || (osName.indexOf("mac os x") != -1))@b@ {@b@ os = 2;@b@ } else if ((osName.indexOf("sun os") != -1) || (osName.indexOf("sunos") != -1) || (osName.indexOf("solaris") != -1))@b@ {@b@ os = 3;@b@ dfPath = "/usr/xpg4/bin/df";@b@ } else if ((osName.indexOf("hp-ux") != -1) || (osName.indexOf("aix") != -1))@b@ {@b@ os = 3;@b@ } else {@b@ os = 0;@b@ }@b@ }@b@ catch (Exception ex) {@b@ os = -1;@b@ }@b@ OS = os;@b@ DF = dfPath;@b@ }@b@}