KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > InputFileLoader


1 /**
2  * $Id: InputFileLoader.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 1997-2003 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.net.URLConnection JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 /**
42  * Helper that loads contents of an input stream into a memory-based byte buffer.
43  *
44  * @since JWare/core 0.5
45  * @author ssmc, &copy;1997-2003 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
46  * @version 0.5
47  * @.safety guarded
48  * @.group impl,helper
49  **/

50
51 public class InputFileLoader
52 {
53     /**
54      * The default transfer buffer size ~8K.
55      **/

56     private static final int BUFSIZ=8*1024;
57
58
59     /**
60      * Symbolic integer that means "NO SIZE LIMIT" on load.
61      **/

62     public static final int NOLIMIT= Integer.MIN_VALUE;
63
64
65     /**
66      * Symbolic boolean that means "NO CACHED DATA" on load.
67      **/

68     public static final boolean NOCACHE= false;
69
70
71
72     /**
73      * Creates new input loader.
74      **/

75     public InputFileLoader()
76     {
77         this(0);
78     }
79
80
81     /**
82      * Creates new input loader for outer controlling object.
83      **/

84     public InputFileLoader(int bufsiz)
85     {
86         if (bufsiz<=0) {
87             bufsiz=BUFSIZ;
88         }
89         m_transferBuffer= new byte[bufsiz];
90     }
91
92
93     /**
94      * Loads contents of an inputstream into a new byte[] buffer with a
95      * precautionary limit to the amount of information that should be read
96      * into memory. Will return an empty byte[] if the stream is empty.
97      * @param ins source stream (non-null)
98      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
99      * @throws IOException if unable to load stream's contents
100      **/

101     public final byte[] load(InputStream JavaDoc ins, int limit)
102         throws IOException JavaDoc
103     {
104         verify_(ins!=null,"load- nonzro inputstrm");
105         byte[] bytes;
106
107         synchronized(m_transferBuffer) {
108             BufferedInputStream JavaDoc bins;
109
110             if (ins instanceof BufferedInputStream JavaDoc) {
111                 bins= (BufferedInputStream JavaDoc)ins;
112             } else {
113                 bins= new BufferedInputStream JavaDoc(ins);
114             }
115             bytes = transferBytes(bins,limit);
116             bins = null;
117         }
118
119         return bytes;
120     }
121
122
123     /**
124      * Loads all contents of an inputstream into a new byte[] buffer. Will
125      * return an empty byte[] if the stream is empty.
126      * @param ins source stream (non-null)
127      * @throws IOException if unable to completely load stream's contents
128      **/

129     public final byte[] load(InputStream JavaDoc ins)
130         throws IOException JavaDoc
131     {
132         return load(ins,NOLIMIT);
133     }
134
135
136     /**
137      * Loads contents of a fetched URL into a new byte[] buffer with a
138      * precautionary limit to the amount of information that should be read
139      * into memory. Will return an empty byte[] if the URL returns no
140      * content (but is successful).
141      * @param url source URL
142      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
143      * @param cacheOK <i>true</i> if caching data ok (see {@linkplain #NOCACHE})
144      * @throws IOException if unable to load URL contents
145      **/

146     public final byte[] loadURL(URL JavaDoc url, int limit, boolean cacheOK)
147         throws IOException JavaDoc
148     {
149         verify_(url!=null,"loadURL- nonzro URL");
150         byte[] bytes;
151
152         // We synchronize here to prevent concurrent calls from opening their
153
// streams only to have to wait. (Avoids timeouts on connection.)
154
synchronized(m_transferBuffer) {
155             try {
156                 URLConnection JavaDoc urlc = url.openConnection();
157                 urlc.setDefaultUseCaches(cacheOK);
158                 urlc.setAllowUserInteraction(false);
159
160                 BufferedInputStream JavaDoc ins = new BufferedInputStream JavaDoc(urlc.getInputStream());
161                 bytes = transferBytes(ins,limit);
162
163                 Tk.closeQuietly(ins);
164                 ins = null;
165                 urlc= null;
166             }
167             catch(Exception JavaDoc anyx) {
168                 if (anyx instanceof IOException JavaDoc) {
169                     throw (IOException JavaDoc)anyx;
170                 }
171                 throw new IOException JavaDoc("Unable to load: "+url+"("+anyx.getMessage()+")");//FIXME:
172
}
173         }//lock
174

175         return bytes;
176     }
177
178
179
180     /**
181      * Loads contents of a fetched URL into a new byte[] buffer using
182      * cached information if possible. A precautionary limit to the amount of
183      * information that should be read into memory can also be defined. Will
184      * return an empty byte[] if the URL returns no content (but is successful).
185      * @param url source URL
186      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
187      * @throws IOException if unable to load URL contents
188      **/

189     public final byte[] loadURL(URL JavaDoc url, int limit)
190         throws IOException JavaDoc
191     {
192         return loadURL(url,limit,!NOCACHE);
193     }
194
195
196
197     /**
198      * Loads all contents of a fetched URL into a new byte[] buffer. Will
199      * return an empty byte[] if the URL returns no content (but is successful).
200      * @param url source URL
201      * @throws IOException if unable to completely load URL contents
202      **/

203     public final byte[] loadURL(URL JavaDoc url)
204         throws IOException JavaDoc
205     {
206         return loadURL(url,NOLIMIT,!NOCACHE);
207     }
208
209
210
211
212     /**
213      * Loads contents of an established URL stream into a new local file with a
214      * precautionary limit to the amount of information that should be read
215      * into new file.
216      * @param urlc established connection to source (non-null)
217      * @param file destination file (non-null)
218      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
219      * @throws IOException if unable to load stream's contents
220      * @since JWare/AntX 0.5
221      **/

222     public final void loadURLToFile(URLConnection JavaDoc urlc, File JavaDoc file, int limit)
223         throws IOException JavaDoc
224     {
225         verify_(urlc!=null && file!=null,"loadURLToFile- nonzro URLcon and file");
226
227         // We synchronize here to prevent concurrent calls from opening their
228
// streams only to have to wait. (Avoids timeouts on connection.)
229
synchronized(m_transferBuffer) {
230             try {
231                 BufferedInputStream JavaDoc ins = new BufferedInputStream JavaDoc(urlc.getInputStream());
232                 transferBytes(ins,limit,file);
233                 Tk.closeQuietly(ins);
234                 ins = null;
235             }
236             catch(Exception JavaDoc anyx) {
237                 if (anyx instanceof IOException JavaDoc) {
238                     throw (IOException JavaDoc)anyx;
239                 }
240                 throw new IOException JavaDoc("Unable to load to stream: "+urlc.getURL()+
241                                       "("+anyx.getMessage()+")");
242             }
243         }//lock
244
}
245
246
247
248     /**
249      * Loads all contents of a n established URL stream into a new byte[] buffer.
250      * Will return an empty byte[] if the connection yields no content (but is
251      * successfully read).
252      * @param urlc established connection to source (non-null)
253      * @param file destination file (non-null)
254      * @throws IOException if unable to completely load stream's contents
255      * @since JWare/AntX 0.5
256      **/

257     public final void loadURLToFile(URLConnection JavaDoc urlc, File JavaDoc file)
258         throws IOException JavaDoc
259     {
260         loadURLToFile(urlc,file,NOLIMIT);
261     }
262
263
264
265     /**
266      * Loads contents of a fetched URL into a new local file with a
267      * precautionary limit to the amount of information that should be read
268      * into new file.
269      * @param url source URL (non-null)
270      * @param file destination file (non-null)
271      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
272      * @param cacheOK <i>true</i> if caching data ok (see {@linkplain #NOCACHE})
273      * @throws IOException if unable to load URL contents
274      * @since JWare/AntX 0.5
275      **/

276     public final void loadURLToFile(URL JavaDoc url, File JavaDoc file, int limit, boolean cacheOK)
277         throws IOException JavaDoc
278     {
279         verify_(url!=null && file!=null,"loadURLToFile- nonzro URL and file");
280
281         // We synchronize here to prevent concurrent calls from opening their
282
// streams only to have to wait. (Avoids timeouts on connection.)
283
synchronized(m_transferBuffer) {
284             try {
285                 URLConnection JavaDoc urlc = url.openConnection();
286                 urlc.setDefaultUseCaches(cacheOK);
287                 urlc.setAllowUserInteraction(false);
288
289                 BufferedInputStream JavaDoc ins = new BufferedInputStream JavaDoc(urlc.getInputStream());
290                 transferBytes(ins,limit,file);
291
292                 Tk.closeQuietly(ins);
293                 ins = null;
294                 urlc= null;
295             }
296             catch(Exception JavaDoc anyx) {
297                 if (anyx instanceof IOException JavaDoc) {
298                     throw (IOException JavaDoc)anyx;
299                 }
300                 throw new IOException JavaDoc("Unable to load to file: "+url+"("+anyx.getMessage()+")");
301             }
302         }//lock
303
}
304
305
306
307     /**
308      * Loads contents of a fetched URL into a new local file using
309      * cached information if possible. A precautionary limit to the amount of
310      * information that should be read into the file can also be defined.
311      * @param url source URL (non-null)
312      * @param file destination file (non-null)
313      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
314      * @throws IOException if unable to load URL contents
315      * @since JWare/AntX 0.5
316      **/

317     public final void loadURLToFile(URL JavaDoc url, File JavaDoc file, int limit)
318         throws IOException JavaDoc
319     {
320         loadURLToFile(url,file,limit,!NOCACHE);
321     }
322
323
324
325     /**
326      * Loads all contents of a fetched URL into a new byte[] buffer. Will
327      * return an empty byte[] if the URL returns no content (but is successful).
328      * @param url source URL (non-null)
329      * @param file destination file (non-null)
330      * @throws IOException if unable to completely load URL contents
331      * @since JWare/AntX 0.5
332      **/

333     public final void loadURLToFile(URL JavaDoc url, File JavaDoc file)
334         throws IOException JavaDoc
335     {
336         loadURLToFile(url,file,NOLIMIT,!NOCACHE);
337     }
338
339
340
341     /**
342      * Loads contents of a file into a new byte[] buffer with a precautionary
343      * limit to the amount of information that should be read into memory. Usually
344      * the file descriptor is either a URL or a (same-system) file path.
345      * @param descriptor path to inputfile information (URL or file path)
346      * @param limit the maximum number of bytes to read from stream (see {@linkplain #NOLIMIT})
347      * @return contents of file specified by descriptor
348      * @throws IOException if unable to load file's contents
349      **/

350     public byte[] loadFile(String JavaDoc descriptor, int limit)
351         throws IOException JavaDoc
352     {
353         verify_(!Tk.isWhitespace(descriptor), "loadFil- nonzro descrip");
354
355         URL JavaDoc url=null;
356         if (descriptor.indexOf("://")>0) {
357             url= new URL JavaDoc(descriptor);
358         }
359         else if (descriptor.toLowerCase().startsWith("file:") ||
360                  descriptor.toLowerCase().startsWith("jar:")) {
361             //Try URL conversion anyway; some windows URLs are in form
362
//"file:\<disk>:\<path>"; also jar urls are a funny format.
363
}
364         else {
365             File JavaDoc f= new File JavaDoc(descriptor);
366             try {
367                 if (f.canRead()) {
368                     url= f.toURL();
369                 } else {
370                     throw new FileNotFoundException JavaDoc(f.getPath());
371                 }
372             } catch(SecurityException JavaDoc secx) {
373                 throw new IOException JavaDoc("Security exception caught: "+secx.getMessage());
374             }
375         }
376         verify_(url!=null,"lod- nonzro url");
377         return loadURL(url,limit,!NOCACHE);
378     }
379
380
381     /**
382      * Loads all contents of an file into a new byte[] buffer. Usually file
383      * descriptors are either URLs or (same-system) files.
384      * @param descriptor path to inputfile information (URL or file name)
385      * @return contents of file specified by descriptor
386      * @throws IOException if unable to completely load file's contents
387      **/

388     public byte[] loadFile(String JavaDoc descriptor)
389         throws IOException JavaDoc
390     {
391         return loadFile(descriptor,NOLIMIT);
392     }
393
394
395
396     /**
397      * Tries to load contents of class resource file. Falls back to the system
398      * class path if resource not found in class's package.
399      * @param resource name of resource (non-null)
400      * @param forClass the resource-owning starting class (non-null)
401      * @throws java.io.FileNotFoundException if unable to find resource
402      * @throws java.io.IOException if unable to completely load resource's contents
403      **/

404     public byte[] loadResource(String JavaDoc resource, Class JavaDoc forClass)
405         throws IOException JavaDoc
406     {
407         verify_(!Tk.isWhitespace(resource), "loadRez- nonzro resname");
408         verify_(forClass!=null,"loadRez- nonzro clas");
409
410         InputStream JavaDoc rsrc = forClass.getResourceAsStream(resource);
411         if (rsrc==null) {
412             rsrc = ClassLoader.getSystemResourceAsStream(resource);
413         }
414         if (rsrc==null) {
415             throw new FileNotFoundException JavaDoc("Class= "+forClass.getName()+", Resource= "+resource);
416         }
417
418         try {
419             return load(rsrc,NOLIMIT);
420         } finally {
421             Tk.closeQuietly(rsrc);
422         }
423     }
424
425
426     /**
427      * Loads all contents of the properties file at a URL into an existing
428      * <span class="src">Properties</span> reference. Assumes encoding of source
429      * properties information is ISO8851-1.
430      * @param url source URL (non-null)
431      * @param properties [optional] properties to be updated (use <i>null</i> for new)
432      * @param cacheOK <i>true</i> if URL can be read from server caches (see {@linkplain #NOCACHE})
433      * @throws IOException if unable to completely load URL contents
434      **/

435     public final Properties JavaDoc loadProperties(URL JavaDoc url, Properties JavaDoc properties, boolean cacheOK)
436         throws IOException JavaDoc
437     {
438         verify_(url!=null,"loadProps- nonzro URL");
439
440         if (properties==null) {
441             properties = new Properties JavaDoc();
442         }
443
444         try {
445             URLConnection JavaDoc urlc = url.openConnection();
446             urlc.setDefaultUseCaches(cacheOK);
447             urlc.setAllowUserInteraction(false);
448
449             properties.load(urlc.getInputStream());
450             urlc= null;
451         }
452         catch(Exception JavaDoc anyx) {
453             if (anyx instanceof IOException JavaDoc) {
454                 throw (IOException JavaDoc)anyx;
455             }
456             throw new IOException JavaDoc("Unable to load Properties: "+url+"("+anyx.getMessage()+")");
457         }
458
459         return properties;
460     }
461
462
463     /**
464      * Labor of transferring read bytes from a buffer input to the outgoing
465      * byte array. Returns an empty byte[] for an empty input stream.
466      **/

467     private byte[] transferBytes(BufferedInputStream JavaDoc ins, int limit)
468         throws IOException JavaDoc
469     {
470         byte[] bytes=null;
471         int cc;
472
473         // We assume incoming data to be less-than or ~8K of data for this
474
// not be be grossly inefficient (ssmc).
475
final int maxChars = m_transferBuffer.length;
476         if (limit<=0) {
477             limit = Integer.MAX_VALUE-1;
478         }
479         while ((cc=ins.read(m_transferBuffer,0,maxChars))!= -1) {
480             if (cc==0) { //?hmmm?
481
continue;
482             }
483             if (bytes==null) {
484                 bytes= new byte[cc];
485                 System.arraycopy(m_transferBuffer,0,bytes,0,cc);
486             } else {
487                 int newcc= cc+bytes.length;
488                 byte[] newbytes= new byte[newcc];
489                 System.arraycopy(bytes,0,newbytes,0,bytes.length);
490                 bytes=null;
491                 System.arraycopy(m_transferBuffer,0,newbytes,newcc-cc,cc);
492                 bytes= newbytes;
493             }
494             if (bytes.length>=limit) {
495                 break;
496             }
497         }
498         if (bytes==null) {
499             bytes = new byte[0];
500         }
501         return bytes;
502     }
503
504
505
506
507     /**
508      * Labor of transferring read bytes from a buffer input to the outgoing
509      * byte array. Returns an empty byte[] for an empty input stream.
510      * @since JWare/AntX 0.5
511      **/

512     private void transferBytes(BufferedInputStream JavaDoc ins, int limit, File JavaDoc output)
513         throws IOException JavaDoc
514     {
515         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(output);
516         int cc, cctotal=0;
517
518         // We assume incoming data to be less-than or ~8K of data for this
519
// not be be grossly inefficient (ssmc).
520
final int maxChars = m_transferBuffer.length;
521         if (limit<=0) {
522             limit = Integer.MAX_VALUE-1;
523         }
524         try {
525             while ((cc=ins.read(m_transferBuffer,0,maxChars))!= -1) {
526                 if (cc==0) { //?hmmm?
527
continue;
528                 }
529                 fos.write(m_transferBuffer,0,cc);
530                 fos.flush();
531                 cctotal += cc;
532                 if (cctotal>=limit) {
533                     break;
534                 }
535             }
536         } finally {
537             Tk.closeQuietly(fos);
538         }
539     }
540
541
542
543
544     /**
545      * Convenience; facilitates frequent need to load test files.
546      * @param url the source to be loaded (non-null)
547      * @throws IOException if unable to load string
548      **/

549     public final static String JavaDoc loadString(URL JavaDoc url)
550         throws IOException JavaDoc
551     {
552         InputFileLoader fl= new InputFileLoader();
553         byte[] bytes = fl.loadURL(url,NOLIMIT,NOCACHE);
554         String JavaDoc s = bytes.length>0 ? new String JavaDoc(bytes) : "";
555         fl.dispose();
556         fl= null;
557         bytes= null;
558         return s;
559     }
560
561
562     /**
563      * Convenience; facilitates frequent need to load test files.
564      * @param filename the file to be loaded (non-null)
565      * @throws IOException if unable to load string
566      **/

567     public final static String JavaDoc loadString(String JavaDoc filename)
568         throws IOException JavaDoc
569     {
570         InputFileLoader fl= new InputFileLoader();
571         byte[] bytes = fl.loadFile(filename);
572         String JavaDoc s = bytes.length>0 ? new String JavaDoc(bytes) : "";
573         fl.dispose();
574         fl= null;
575         bytes= null;
576         return s;
577     }
578
579
580     /**
581      * Convenience; facilitates frequent need to load tests files.
582      * @param file the file to be loaded (non-null,can-read)
583      * @throws IOException if unable to load string
584      **/

585     public final static String JavaDoc loadString(File JavaDoc file)
586         throws IOException JavaDoc
587     {
588         return loadString(file.getAbsolutePath());
589     }
590
591
592     /**
593      * Convenience; facilitates frequent need to load test files.
594      * @param resource class resource name (non-null)
595      * @param forClass the owning class (non-null)
596      * @throws IOException if unable to load string
597      **/

598     public final static String JavaDoc loadString(String JavaDoc resource, Class JavaDoc forClass)
599         throws IOException JavaDoc
600     {
601         InputFileLoader fl= new InputFileLoader();
602         byte[] bytes = fl.loadResource(resource, forClass);
603         String JavaDoc s = bytes.length>0 ? new String JavaDoc(bytes) : "";
604         fl.dispose();
605         fl= null;
606         bytes= null;
607         return s;
608     }
609
610
611     /**
612      * Convenience; facilitates frequent need to load test properties files.
613      * @param url the source to be loaded (non-null)
614      * @param p [optional] existing properties to be updated (use <i>null</i> for new)
615      * @throws IOException if unable to load Properties
616      **/

617     public final static Properties JavaDoc loadProperties(URL JavaDoc url, Properties JavaDoc p)
618         throws IOException JavaDoc
619     {
620         return new InputFileLoader().loadProperties(url,p,NOCACHE);
621     }
622
623
624     /**
625      * Clears this loader's underlying scratch buffer. This loader
626      * is no longer valid after this method returns.
627      **/

628     public void dispose()
629     {
630         m_transferBuffer = null;
631     }
632
633
634     /**
635      * Zapped assertable api.
636      **/

637     private void verify_(boolean c, String JavaDoc msg)
638     {
639         if (!c) {
640             throw new IllegalArgumentException JavaDoc("REQUIRE: InputFileLoader: "+msg);
641         }
642     }
643
644
645     private byte[] m_transferBuffer;//$mem cost
646
}
647
648 /* end-of-InputFileLoader.java */
649
Popular Tags