KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > util > IoUtilities


1 package de.java2html.util;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.BufferedOutputStream JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.io.Writer JavaDoc;
14
15 /**
16  * @author Markus Gebhard
17  */

18 public class IoUtilities {
19   private IoUtilities() {
20     //no instance available
21
}
22
23   public static byte[] readBytes(InputStream JavaDoc inputStream) throws IOException JavaDoc {
24     ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
25     copyStream(inputStream, byteOut);
26     return byteOut.toByteArray();
27   }
28
29   public static void copyStream(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
30     byte[] buffer = new byte[4096];
31     while (true) {
32       int bytesRead = in.read(buffer);
33       if (bytesRead == -1) {
34         break;
35       }
36       out.write(buffer, 0, bytesRead);
37     }
38   }
39
40   public static void close(OutputStream JavaDoc outputStream) {
41     if (outputStream != null) {
42       try {
43         outputStream.close();
44       }
45       catch (IOException JavaDoc e) {
46         //nothing to do
47
}
48     }
49   }
50
51   public static void close(InputStream JavaDoc inputStream) {
52     if (inputStream != null) {
53       try {
54         inputStream.close();
55       }
56       catch (IOException JavaDoc e) {
57         //nothing to do
58
}
59     }
60   }
61
62   public static void close(Writer JavaDoc writer) {
63     if (writer != null) {
64       try {
65         writer.close();
66       }
67       catch (IOException JavaDoc e) {
68         //nothing to do
69
}
70     }
71   }
72
73   public static void close(Reader JavaDoc reader) {
74     if (reader != null) {
75       try {
76         reader.close();
77       }
78       catch (IOException JavaDoc e) {
79         //nothing to do
80
}
81     }
82   }
83
84   public static void copy(File JavaDoc sourceFile, File JavaDoc destinationFile) throws IOException JavaDoc {
85     if (!ensureFoldersExist(destinationFile.getParentFile())) {
86       throw new IOException JavaDoc("Unable to create necessary output directory " //$NON-NLS-1$
87
+ destinationFile.getParentFile());
88     }
89     BufferedInputStream JavaDoc bis = null;
90     BufferedOutputStream JavaDoc bos = null;
91     try {
92       bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(sourceFile));
93       bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(destinationFile));
94       copyStream(bis, bos);
95     }
96     finally {
97       close(bis);
98       close(bos);
99     }
100   }
101
102   public static boolean ensureFoldersExist(File JavaDoc folder) {
103     if (folder.exists()) {
104       return true;
105     }
106     return folder.mkdirs();
107   }
108
109   public static File JavaDoc exchangeFileExtension(File JavaDoc file, String JavaDoc newFileExtension) {
110     String JavaDoc fileName = file.getAbsolutePath();
111     int index = fileName.lastIndexOf('.');
112     if (index == -1) {
113       throw new IllegalStateException JavaDoc("Unable to determine file extension from file name '" //$NON-NLS-1$
114
+ fileName
115           + "'"); //$NON-NLS-1$
116
}
117     return new File JavaDoc(fileName.substring(0, index + 1) + newFileExtension);
118   }
119 }
Popular Tags