View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.http.util;
14  
15  import java.io.File;
16  import java.io.FileInputStream;
17  import java.io.FileOutputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.nio.channels.FileChannel;
22  import java.nio.channels.ReadableByteChannel;
23  
24  /**
25   * IO util methods
26   *
27   * @author Daniel Sendula
28   */
29  public class IOUtils {
30  
31      /**
32       * Transfer given channel to the file channel
33       * @param inputChannel file channel
34       * @param fileChannel output channel
35       * @param length length
36       * @throws IOException IO exception
37       */
38      public static void transferToFileChannel(FileChannel fileChannel, ReadableByteChannel inputChannel, long length) throws IOException {
39          // TODO this IS wrong. Encoding is needed to be catered for here!!!
40          fileChannel.transferFrom(inputChannel, 0, length);
41      }
42  
43      /**
44       * Adds two paths together always ensuring that it starts with a "/" and if path 2 is "/" to end with "/"
45       * @param path1 path one
46       * @param path2 path two
47       * @return new path
48       */
49      public static String addPaths(String path1, String path2) {
50          StringBuffer result = new StringBuffer();
51          boolean p1s = false;
52          boolean p1e = false;
53          boolean p1 = false;
54          boolean p2s = false;
55          boolean p2 = false;
56          if (path1.equals("/")) {
57              p1s = true;
58              p1e = true;
59              p1 = true;
60          } else {
61              p1s = path1.startsWith("/");
62              p1e = path1.endsWith("/");
63          }
64          if (path2.equals("/")) {
65              p2s = true;
66              p2 = true;
67          } else {
68              p2s = path2.startsWith("/");
69          }
70  
71          if (p1) {
72              if (p2) {
73                  return path1;
74              } else if (!p2s) {
75                  result.append(path1).append(path2);
76                  return result.toString();
77              }
78          }
79          if (!p1s) {
80              result.append('/');
81          }
82          result.append(path1);
83          if (p2) {
84              if (p1e) {
85                  return result.toString();
86              } else {
87                  result.append(path2);
88                  return result.toString();
89              }
90          }
91          if (p1e && p2s) {
92              result.append(path2.substring(1));
93              return result.toString();
94          } else if (!p1e && !p2s) {
95              result.append('/');
96          }
97          result.append(path2);
98  
99          return result.toString();
100     }
101 
102     public static String lastPathComponent(String path) {
103         int i = path.lastIndexOf('/');
104         if (i >= 0) {
105             return path.substring(i + 1);
106         } else {
107             return path;
108         }
109     }
110 
111     public static String parentPath(String path) {
112         if ((path == null) || (path.length() == 0)) {
113             return null;
114         }
115         int i = path.lastIndexOf('/');
116         if (i >= 0) {
117             return path.substring(0, i);
118         } else {
119             return path;
120         }
121     }
122 
123     public static String compareStreams(InputStream s1, InputStream s2) throws IOException {
124         long size = 0;
125         byte[] buf1 = new byte[1000];
126         byte[] buf2 = new byte[1000];
127         int r1 = 0;
128         while (r1 >= 0) {
129             r1 = s1.read(buf1);
130             int r2 = s2.read(buf2);
131             if (r1 < 0) {
132                 if (r2 >= 0) {
133                     return "Different sizes; s1=" + size + ", s2=" + (r2 + size);
134                 }
135                 return null;
136             }
137             if (r2 < 0) {
138                 if (r1 >= 0) {
139                     return "Different sizes; s1=" + (r1 + size) + ", s2=" + size;
140                 }
141                 return null;
142             }
143             if (r1 != r2) {
144                 return "Different sizes; s1=" + (r1 + size) + ", s2=" + (r2 + size);
145             }
146             if (r1 > 0) {
147                 String r = compareArrays(buf1, buf2, size, r1);
148                 size = size + r1;
149                 if (r != null) {
150                     return r;
151                 }
152             }
153         }
154         return null;
155     }
156 
157     public static String compareStreams(InputStream s1, InputStream s2, long size) throws IOException {
158         long loaded = 0;
159         byte[] buf1 = new byte[1000];
160         byte[] buf2 = new byte[1000];
161         int r1 = 0;
162         while ((r1 >= 0) && (size > 0)) {
163             int s = buf1.length;
164             if (s > size) {
165                 s = (int)size;
166             }
167             r1 = s1.read(buf1, 0, s);
168             int r2 = s2.read(buf2, 0, s);
169             if (r1 < 0) {
170                 if (r2 >= 0) {
171                     return "Different sizes; s1=" + loaded + ", s2=" + (r2 + loaded);
172                 }
173                 return null;
174             }
175             if (r2 < 0) {
176                 if (r1 >= 0) {
177                     return "Different sizes; s1=" + (r1 + loaded) + ", s2=" + loaded;
178                 }
179                 return null;
180             }
181             if (r1 != r2) {
182                 return "Different sizes; s1=" + (r1 + loaded) + ", s2=" + (r2 + loaded);
183             }
184             if (r1 > 0) {
185                 String r = compareArrays(buf1, buf2, loaded, r1);
186                 loaded = loaded + r1;
187                 if (r != null) {
188                     return r;
189                 }
190             }
191             size = size - r1;
192         }
193         return null;
194     }
195 
196     public static String compareArrays(byte[] buf1, byte[] buf2, long previousSize, int size) {
197         for (int i = 0; i < size; i++) {
198             if (buf1[i] != buf2[i]) {
199                 return "Different content at position " + (previousSize + i)
200                     + "; buf1[" + (previousSize + i) + "]=" + buf1[i]
201                     + " != buf2[" + (previousSize + i) + "]=" + buf2[i];
202             }
203         }
204         return null;
205     }
206 
207     public static boolean delete(File dir) {
208         if (dir.isDirectory()) {
209             File[] files = dir.listFiles();
210             if (files != null) {
211                 for (File file : files) {
212                     boolean b = delete(file);
213                     if (!b) {
214                         return false;
215                     }
216                 }
217             }
218             return dir.delete();
219         } else {
220             return dir.delete();
221         }
222     }
223 
224     public static boolean copy(File from, File to, boolean recursive) {
225         if (from.isFile()) {
226             return copyFile(from, to);
227         } else {
228             to.mkdir();
229             if (recursive) {
230                 File[] files = from.listFiles();
231                 if (files != null) {
232                     for (File file : files) {
233                         File f = new File(to, file.getName());
234                         if (!copy(file, f, true)) {
235                             return false;
236                         }
237                     }
238                 }
239             }
240             return true;
241         }
242     }
243 
244     public static boolean copyFile(File from, File to) {
245         try {
246             FileInputStream fis = new FileInputStream(from);
247             try {
248                 FileChannel fromChannel = fis.getChannel();
249                 FileOutputStream fos = new FileOutputStream(to);
250                 try {
251                     FileChannel toChannel = fos.getChannel();
252                     toChannel.transferFrom(fromChannel, 0, from.length());
253                 } finally {
254                     fos.close();
255                 }
256             } finally {
257                 fis.close();
258             }
259         } catch (IOException e) {
260             return false;
261         }
262         return true;
263     }
264 
265     public static File createRandomFile(File path, String name, int size) throws IOException {
266         File file = new File(path, name);
267         int bufsize = 1000;
268         if (bufsize > size) {
269             bufsize = size;
270         }
271         int c = 'A';
272         byte[] buf = new byte[bufsize];
273         FileOutputStream fos = new FileOutputStream(file);
274         try {
275             while (size > 0) {
276                 int s = bufsize;
277                 if (s > size) {
278                     s = size;
279                 }
280                 for (int i = 0; i < s; i++) {
281                     buf[i] = (byte)c;
282                     c = c + 1;
283                     if (c > 'Z') {
284                         c = 'A';
285                     }
286                 }
287                 fos.write(buf, 0, size);
288                 size = size - s;
289             }
290         } finally {
291             fos.close();
292         }
293         return file;
294     }
295 
296     public static void copyStreams(InputStream inputStream, OutputStream outputStream) throws IOException {
297         byte[] buffer = new byte[10240];
298         int r = inputStream.read(buffer);
299         while (r > 0) {
300             outputStream.write(buffer, 0, r);
301             r = inputStream.read(buffer);
302         }
303     }
304 }