File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\IOUtils.java.html

1 package hudson.util;
2
3 import hudson.Functions;
4 import hudson.os.PosixAPI;
5 import hudson.os.PosixException;
6
7 import java.io.*;
8 import java.util.regex.Pattern;
9
10 /**
11 * Adds more to commons-io.
12 *
13 * @author Kohsuke Kawaguchi
14 * @since 1.337
15 */
16 public class IOUtils extends org.apache.commons.io.IOUtils {
17 /**
18 * Drains the input stream and closes it.
19 */
20 public static void drain(InputStream in) throws IOException {
21 copy(in,new NullStream());
22 in.close();
23 }
24
25 public static void copy(File src, OutputStream out) throws IOException {
26 FileInputStream in = new FileInputStream(src);
27 try {
28 copy(in,out);
29 } finally {
30 closeQuietly(in);
31 }
32 }
33
34 public static void copy(InputStream in, File out) throws IOException {
35 FileOutputStream fos = new FileOutputStream(out);
36 try {
37 copy(in,fos);
38 } finally {
39 closeQuietly(fos);
40 }
41 }
42
43 /**
44 * Ensures that the given directory exists (if not, it's created, including all the parent directories.)
45 *
46 * @return
47 * This method returns the 'dir' parameter so that the method call flows better.
48 */
49 public static File mkdirs(File dir) throws IOException {
50 if(dir.mkdirs() || dir.exists())
51 return dir;
52
53 // following Ant <mkdir> task to avoid possible race condition.
54 try {
55 Thread.sleep(10);
56 } catch (InterruptedException e) {
57 // ignore
58 }
59
60 if (dir.mkdirs() || dir.exists())
61 return dir;
62
63 throw new IOException("Failed to create a directory at "+dir);
64 }
65
66 /**
67 * Fully skips the specified size from the given input stream.
68 *
69 * <p>
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.
73 *
74 * <p>
75 * So to reliably skip just the N bytes, we'll actually read all those bytes.
76 *
77 * @since 1.349
78 */
79 public static InputStream skip(InputStream in, long size) throws IOException {
80 DataInputStream di = new DataInputStream(in);
81
82 while (size>0) {
83 int chunk = (int)Math.min(SKIP_BUFFER.length,size);
84 di.readFully(SKIP_BUFFER,0,chunk);
85 size -= chunk;
86 }
87
88 return in;
89 }
90
91 /**
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.
94 * <p>
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()
100 */
101 public static File absolutize(File base, String path) {
102 if (isAbsolute(path))
103 return new File(path);
104 return new File(base, path);
105 }
106
107 /**
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
111 */
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();
115 }
116
117
118 /**
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
122 */
123 public static int mode(File f) throws PosixException {
124 if(Functions.isWindows()) return -1;
125 return PosixAPI.get().stat(f.getPath()).mode();
126 }
127
128 /**
129 * Read the first line of the given stream, close it, and return that line.
130 *
131 * @param encoding
132 * If null, use the platform default encoding.
133 * @since 1.422
134 */
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));
138 try {
139 return reader.readLine();
140 } finally {
141 reader.close();
142 }
143 }
144
145 private static final byte[] SKIP_BUFFER = new byte[8192];
146 }
147