KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > IoUtils


1 /* IoUtils
2  *
3  * Created on Dec 8, 2004
4  *
5  * Copyright (C) 2004 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.util;
24
25 import it.unimi.dsi.fastutil.io.FastBufferedOutputStream;
26
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.EOFException JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.nio.charset.Charset JavaDoc;
35 import java.nio.charset.IllegalCharsetNameException JavaDoc;
36 import java.nio.charset.UnsupportedCharsetException JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * I/O Utility methods.
44  * @author stack
45  * @version $Date: 2007/01/13 01:31:39 $, $Revision: 1.11.4.1 $
46  */

47 public class IoUtils {
48     protected static Logger JavaDoc logger =
49         Logger.getLogger(IoUtils.class.getName());
50     
51     /**
52      * @param file File to operate on.
53      * @return Path suitable for use getting resources off the CLASSPATH
54      * (CLASSPATH resources always use '/' as path separator, even on
55      * windows).
56      */

57     public static String JavaDoc getClasspathPath(File JavaDoc file) {
58         String JavaDoc path = file.getPath();
59         if (File.separatorChar != '/') {
60             // OK. We're probably on a windows system. Strip
61
// drive if its present and convert '\' to '/'.
62
path = path.replace(File.separatorChar, '/');
63             int index = path.indexOf(':');
64             if (index > 0 && index < 3) {
65                 path = path.substring(index + 1);
66             }
67         }
68         return path;
69     }
70     
71     /**
72      * Ensure writeable directory.
73      *
74      * If doesn't exist, we attempt creation.
75      *
76      * @param dir Directory to test for exitence and is writeable.
77      *
78      * @return The passed <code>dir</code>.
79      *
80      * @exception IOException If passed directory does not exist and is not
81      * createable, or directory is not writeable or is not a directory.
82      */

83     public static File JavaDoc ensureWriteableDirectory(String JavaDoc dir)
84     throws IOException JavaDoc {
85         return ensureWriteableDirectory(new File JavaDoc(dir));
86     }
87     
88     /**
89      * Ensure writeable directories.
90      *
91      * If doesn't exist, we attempt creation.
92      *
93      * @param dirs List of Files to test.
94      *
95      * @return The passed <code>dirs</code>.
96      *
97      * @exception IOException If passed directory does not exist and is not
98      * createable, or directory is not writeable or is not a directory.
99      */

100     public static List JavaDoc ensureWriteableDirectory(List JavaDoc<File JavaDoc> dirs)
101     throws IOException JavaDoc {
102         for (Iterator JavaDoc<File JavaDoc> i = dirs.iterator(); i.hasNext();) {
103              ensureWriteableDirectory(i.next());
104         }
105         return dirs;
106     }
107
108     /**
109      * Ensure writeable directory.
110      *
111      * If doesn't exist, we attempt creation.
112      *
113      * @param dir Directory to test for exitence and is writeable.
114      *
115      * @return The passed <code>dir</code>.
116      *
117      * @exception IOException If passed directory does not exist and is not
118      * createable, or directory is not writeable or is not a directory.
119      */

120     public static File JavaDoc ensureWriteableDirectory(File JavaDoc dir)
121     throws IOException JavaDoc {
122         if (!dir.exists()) {
123             dir.mkdirs();
124         } else {
125             if (!dir.canWrite()) {
126                 throw new IOException JavaDoc("Dir " + dir.getAbsolutePath() +
127                     " not writeable.");
128             } else if (!dir.isDirectory()) {
129                 throw new IOException JavaDoc("Dir " + dir.getAbsolutePath() +
130                     " is not a directory.");
131             }
132         }
133
134         return dir;
135     }
136
137     /**
138      * Read the entire stream to EOF, returning what's read as a String.
139      *
140      * @param inputStream
141      * @return String of the whole inputStream's contents
142      * @throws IOException
143      */

144     public static String JavaDoc readFullyAsString(InputStream JavaDoc inputStream)
145     throws IOException JavaDoc {
146         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
147         int c;
148         while((c = inputStream.read()) > -1) {
149             sb.append((char)c);
150         }
151         return sb.toString();
152     }
153     
154     /**
155      * Read the entire stream to EOF into the passed file.
156      * @param is
157      * @param toFile File to read into .
158      * @throws IOException
159      * @throws IOException
160      */

161     public static void readFullyToFile(InputStream JavaDoc is,
162             File JavaDoc toFile) throws IOException JavaDoc {
163         readFullyToFile(is, toFile, new byte[4096]);
164     }
165     
166     /**
167      * Read the entire stream to EOF into the passed file.
168      * Closes <code>is</code> when done or if an exception.
169      * @param is Stream to read.
170      * @param toFile File to read into .
171      * @param buffer Buffer to use reading.
172      * @return Count of bytes read.
173      * @throws IOException
174      */

175     public static long readFullyToFile(final InputStream JavaDoc is, final File JavaDoc toFile,
176             final byte [] buffer)
177     throws IOException JavaDoc {
178         long totalcount = -1;
179         OutputStream os =
180             new FastBufferedOutputStream(new FileOutputStream JavaDoc(toFile));
181         InputStream JavaDoc localIs = (is instanceof BufferedInputStream JavaDoc)?
182             is: new BufferedInputStream JavaDoc(is);
183         try {
184             for (int count = -1;
185                 (count = localIs.read(buffer, 0, buffer.length)) != -1;
186                     totalcount += count) {
187                 os.write(buffer, 0, count);
188             }
189         } finally {
190             os.close();
191             if (localIs != null) {
192                 localIs.close();
193             }
194         }
195         return totalcount;
196     }
197
198     /**
199      * Wrap generic Throwable as a checked IOException
200      * @param e wrapped exception
201      * @return IOException
202      */

203     public static IOException JavaDoc wrapAsIOException(Throwable JavaDoc e) {
204         IOException JavaDoc ioe = new IOException JavaDoc(e.toString());
205         ioe.initCause(e);
206         return ioe;
207     }
208     
209     
210     public static void readFully(InputStream JavaDoc input, byte[] buf)
211     throws IOException JavaDoc {
212         int max = buf.length;
213         int ofs = 0;
214         while (ofs < max) {
215             int l = input.read(buf, ofs, max - ofs);
216             if (l == 0) {
217                 throw new EOFException JavaDoc();
218             }
219             ofs += l;
220         }
221     }
222     
223     /**
224      * Return the maximum number of bytes per character in the named
225      * encoding, or 0 if encoding is invalid or unsupported.
226      *
227      * @param encoding Encoding to consider. For now, should be java
228      * canonical name for the encoding.
229      *
230      * @return True if multibyte encoding.
231      */

232     public static float encodingMaxBytesPerChar(String JavaDoc encoding) {
233         boolean isMultibyte = false;
234         final Charset JavaDoc cs;
235         try {
236             if (encoding != null && encoding.length() > 0) {
237                 cs = Charset.forName(encoding);
238                 if(cs.canEncode()) {
239                     return cs.newEncoder().maxBytesPerChar();
240                 } else {
241                     logger.info("Encoding not fully supported: " + encoding
242                             + ". Defaulting to single byte.");
243                 }
244             }
245         } catch (IllegalArgumentException JavaDoc e) {
246             // Unsupported encoding
247
logger.log(Level.INFO,"Illegal encoding name: " + encoding,e);
248         }
249
250         logger.fine("Encoding " + encoding + " is multibyte: "
251             + ((isMultibyte) ? Boolean.TRUE : Boolean.FALSE));
252         // default: return 0
253
return 0;
254     }
255 }
Popular Tags