3
import hudson.Functions;
4
import hudson.os.PosixAPI;
5
import hudson.os.PosixException;
8
import java.util.regex.Pattern;
11
* Adds more to commons-io.
13
* @author Kohsuke Kawaguchi
16
public class IOUtils extends org.apache.commons.io.IOUtils {
18
* Drains the input stream and closes it.
20
public static void drain(InputStream in) throws IOException {
21
copy(in,new NullStream());
25
public static void copy(File src, OutputStream out) throws IOException {
26
FileInputStream in = new FileInputStream(src);
34
public static void copy(InputStream in, File out) throws IOException {
35
FileOutputStream fos = new FileOutputStream(out);
44
* Ensures that the given directory exists (if not, it's created, including all the parent directories.)
47
* This method returns the 'dir' parameter so that the method call flows better.
49
public static File mkdirs(File dir) throws IOException {
50
if(dir.mkdirs() || dir.exists())
53
// following Ant <mkdir> task to avoid possible race condition.
56
} catch (InterruptedException e) {
60
if (dir.mkdirs() || dir.exists())
63
throw new IOException("Failed to create a directory at "+dir);
67
* Fully skips the specified size from the given input stream.
70
* {@link InputStream#skip(long)} has two problems. One is that
71
* it doesn't let us reliably differentiate "hit EOF" case vs "inpustream just returning 0 since there's no data
72
* currently available at hand", and some subtypes (such as {@link FileInputStream#skip(long)} returning -1.
75
* So to reliably skip just the N bytes, we'll actually read all those bytes.
79
public static InputStream skip(InputStream in, long size) throws IOException {
80
DataInputStream di = new DataInputStream(in);
83
int chunk = (int)Math.min(SKIP_BUFFER.length,size);
84
di.readFully(SKIP_BUFFER,0,chunk);
92
* Resolves the given path with respect to given base. If the path represents an absolute path, a file representing
93
* it is returned, otherwise a file representing a path relative to base is returned.
95
* It would be nice if File#File(File, String) were doing this.
96
* @param base File that represents the parent, may be null if path is absolute
97
* @param path Path of the file, may not be null
98
* @return new File(name) if name represents an absolute path, new File(base, name) otherwise
99
* @see hudson.FilePath#absolutize()
101
public static File absolutize(File base, String path) {
102
if (isAbsolute(path))
103
return new File(path);
104
return new File(base, path);
108
* See {@link hudson.FilePath#isAbsolute(String)}.
109
* @param path String representing <code> Platform Specific </code> (unlike FilePath, which may get Platform agnostic paths), may not be null
110
* @return true if String represents absolute path on this platform, false otherwise
112
public static boolean isAbsolute(String path) {
113
Pattern DRIVE_PATTERN = Pattern.compile("[A-Za-z]:[\\\\/].*");
114
return path.startsWith("/") || DRIVE_PATTERN.matcher(path).matches();
119
* Gets the mode of a file/directory, if appropriate.
120
* @return a file mode, or -1 if not on Unix
121
* @throws PosixException if the file could not be statted, e.g. broken symlink
123
public static int mode(File f) throws PosixException {
124
if(Functions.isWindows()) return -1;
125
return PosixAPI.get().stat(f.getPath()).mode();
129
* Read the first line of the given stream, close it, and return that line.
132
* If null, use the platform default encoding.
135
public static String readFirstLine(InputStream is, String encoding) throws IOException {
136
BufferedReader reader = new BufferedReader(
137
encoding==null ? new InputStreamReader(is) : new InputStreamReader(is,encoding));
139
return reader.readLine();
145
private static final byte[] SKIP_BUFFER = new byte[8192];