KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > io > IOutils


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.imagero.uio.io;
34
35 import com.imagero.uio.RandomAccessRO;
36 import com.imagero.uio.Sys;
37
38 import java.io.BufferedReader JavaDoc;
39 import java.io.BufferedWriter JavaDoc;
40 import java.io.EOFException JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.OutputStream JavaDoc;
45 import java.io.RandomAccessFile JavaDoc;
46
47 /**
48  * IOutils.java
49  *
50  * @author Andrei Kouznetsov
51  */

52 public class IOutils {
53
54     /**
55      * close silently stream<br>
56      * no exception it thrown
57      *
58      * @param bw
59      */

60     public static void closeStream(BufferedWriter JavaDoc bw) {
61         try {
62             if(bw != null) {
63                 bw.close();
64             }
65         }
66         catch(IOException JavaDoc ex) {
67         }
68     }
69
70     /**
71      * close silently stream<br>
72      * no exception it thrown
73      *
74      * @param br
75      */

76     public static void closeStream(BufferedReader JavaDoc br) {
77         try {
78             if(br != null) {
79                 br.close();
80             }
81         }
82         catch(IOException JavaDoc ex) {
83         }
84     }
85
86     /**
87      * close silently stream<br>
88      * no exception it thrown
89      *
90      * @param is
91      */

92     public static void closeStream(InputStream JavaDoc is) {
93         try {
94             if(is != null) {
95                 is.close();
96             }
97         }
98         catch(IOException JavaDoc ex) {
99         }
100     }
101
102     /**
103      * close silently stream<br>
104      * no exception it thrown
105      *
106      * @param os
107      */

108     public static void closeStream(OutputStream JavaDoc os) {
109         try {
110             if(os != null) {
111                 os.close();
112             }
113         }
114         catch(IOException JavaDoc ex) {
115         }
116     }
117
118     /**
119      * close silently stream<br>
120      * no exception it thrown
121      *
122      * @param raf
123      */

124     public static void closeStream(RandomAccessFile JavaDoc raf) {
125         try {
126             if(raf != null) {
127                 raf.close();
128             }
129         }
130         catch(IOException JavaDoc ex) {
131         }
132     }
133
134     /**
135      * close silently stream<br>
136      * no exception it thrown
137      *
138      * @param ro
139      */

140     public static void closeStream(RandomAccessRO ro) {
141         try {
142             if(ro != null) {
143                 ro.close();
144             }
145         }
146         catch(IOException JavaDoc ex) {
147         }
148     }
149
150     static final int[] mask = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
151     static byte b0 = (byte) '0';
152     static byte b1 = (byte) '1';
153
154     public static String JavaDoc toBinaryString(byte value) {
155         byte[] b = new byte[8];
156         int cnt = 0;
157         for(int i = 7; i > -1; i--) {
158             b[cnt++] = (value & mask[i]) == 0 ? b0 : b1;
159         }
160         return new String JavaDoc(b);
161     }
162
163     public static String JavaDoc toBinaryString(char value) {
164         byte[] b = new byte[16];
165         int cnt = 0;
166         for(int i = 15; i > -1; i--) {
167             b[cnt++] = (value & mask[i]) == 0 ? b0 : b1;
168         }
169         return new String JavaDoc(b);
170     }
171
172     public static String JavaDoc toBinaryString(int value, int length) {
173         byte[] b = new byte[length];
174         int cnt = 0;
175         for(int i = length - 1; i > -1; i--) {
176             if(((value >> i) & 1) == 1) {
177                 b[cnt++] = b1;
178             }
179             else {
180                 b[cnt++] = b0;
181             }
182         }
183         return new String JavaDoc(b);
184     }
185
186     final static byte[] digits = {
187         (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5',
188         (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b',
189         (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f'
190     };
191
192     public static String JavaDoc toHexString(byte value) {
193         return toUnsignedString(value & 0xFF, 4);
194     }
195
196     private static String JavaDoc toUnsignedString(int i, int shift) {
197         byte[] buf = new byte[]{(byte) '0', (byte) '0'};
198         int charPos = 2;
199         int radix = 1 << shift;
200         int mask = radix - 1;
201         do {
202             buf[--charPos] = digits[i & mask];
203             i >>>= shift;
204         }
205         while(i != 0);
206
207         return new String JavaDoc(buf);
208     }
209
210     public static void printHexByte(int value) {
211         printHexImpl(value & 0xFFFF, 2);
212     }
213
214     public static void printlnHexByte(int value) {
215         printHexImpl(value & 0xFFFF, 2);
216         Sys.out.println("");
217     }
218
219     public static void printHexShort(int value) {
220         printHexImpl(value & 0xFFFF, 4);
221     }
222
223     public static void printlnHexShort(int value) {
224         printHexImpl(value & 0xFFFF, 4);
225         Sys.out.println("");
226     }
227
228     public static void printHexInt(int value) {
229         printHexImpl(value & 0xFFFFFFFF, 8);
230     }
231
232     public static void printlnHexInt(int value) {
233         printHexImpl(value & 0xFFFFFFFF, 8);
234         Sys.out.println("");
235     }
236
237     public static void printHexLong(long value) {
238         printHexImpl(value & 0xFFFFFFFFFFFFFFFFL, 16);
239     }
240
241     public static void printlnHexLong(long value) {
242         printHexImpl(value & 0xFFFFFFFFFFFFFFFFL, 16);
243         Sys.out.println("");
244     }
245
246     static void printHexImpl(long value, int length) {
247         String JavaDoc s = Long.toHexString(value);
248         //Sys.out.println("***********************" + s + " " + value);
249
for(int i = 0, size = length - s.length(); i < size; i++) {
250             Sys.out.print("0");
251         }
252         Sys.out.print(s);
253     }
254
255     static void printHexImpl(int value, int length) {
256         String JavaDoc s = Integer.toHexString(value);
257         if(s.length() > length) {
258             s = s.substring(s.length() - length);
259         }
260         //Sys.out.println("***********************" + s + " " + value);
261
for(int i = 0, size = length - s.length(); i < size; i++) {
262             Sys.out.print("0");
263         }
264         Sys.out.print(s);
265     }
266
267     public static String JavaDoc getExtension(File JavaDoc f) {
268         String JavaDoc s = f.getName();
269         return s.substring(s.lastIndexOf(".") + 1).toUpperCase();
270     }
271
272     /**
273      * read little-endian short
274      */

275     public static int readShort4D(InputStream JavaDoc in) throws IOException JavaDoc {
276         return ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0);
277     }
278
279     /**
280      * read little-endian short
281      */

282     public static int readShort4D(RandomAccessFile JavaDoc in) throws IOException JavaDoc {
283         return ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0);
284     }
285
286     /**
287      * read little-endian short
288      */

289     public static int readShort4D(RandomAccessRO in) throws IOException JavaDoc {
290         return ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0);
291     }
292
293     /**
294      * read big-endian short
295      */

296     public static int readShort49(InputStream JavaDoc in) throws IOException JavaDoc {
297         return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8);
298     }
299
300     /**
301      * read big-endian short
302      */

303     public static int readShort49(RandomAccessFile JavaDoc in) throws IOException JavaDoc {
304         return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8);
305     }
306
307     /**
308      * read big-endian short
309      */

310     public static int readShort49(RandomAccessRO in) throws IOException JavaDoc {
311         return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8);
312     }
313
314     /**
315      * read little-endian int
316      */

317     public static int readInt4D(InputStream JavaDoc in) throws IOException JavaDoc {
318         return (((in.read() & 0xFF) << 24) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0));
319     }
320
321     /**
322      * read little-endian int
323      */

324     public static int readInt4D(RandomAccessFile JavaDoc in) throws IOException JavaDoc {
325         int b0 = (in.read() & 0xFF);
326         int b1 = (in.read() & 0xFF);
327         int b2 = (in.read() & 0xFF);
328         int b3 = (in.read() & 0xFF);
329         return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
330     }
331
332     /**
333      * read little-endian int
334      */

335     public static int readInt4D(RandomAccessRO in) throws IOException JavaDoc {
336         int b0 = (in.read() & 0xFF);
337         int b1 = (in.read() & 0xFF);
338         int b2 = (in.read() & 0xFF);
339         int b3 = (in.read() & 0xFF);
340         return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
341     }
342
343     /**
344      * read big-endian int
345      */

346     public static int readInt49(InputStream JavaDoc in) throws IOException JavaDoc {
347         return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 24);
348     }
349
350     /**
351      * read big-endian int
352      */

353     public static int readInt49(RandomAccessFile JavaDoc in) throws IOException JavaDoc {
354         return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 24);
355     }
356
357     /**
358      * read big-endian int
359      */

360     public static int readInt49(RandomAccessRO in) throws IOException JavaDoc {
361         return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 24);
362     }
363
364     /**
365      * read little-endian long
366      */

367     public static long readLong4D(InputStream JavaDoc in) throws IOException JavaDoc {
368         return ((long) (readInt4D(in)) << 32) + (readInt4D(in) & 0xFFFFFFFFL);
369     }
370
371     /**
372      * read little-endian long
373      */

374     public static long readLong4D(RandomAccessFile JavaDoc in) throws IOException JavaDoc {
375         return ((long) (readInt4D(in)) << 32) + (readInt4D(in) & 0xFFFFFFFFL);
376     }
377
378     /**
379      * read little-endian long
380      */

381     public static long readLong4D(RandomAccessRO in) throws IOException JavaDoc {
382         return ((long) (readInt4D(in)) << 32) + (readInt4D(in) & 0xFFFFFFFFL);
383     }
384
385     /**
386      * read big-endian long
387      */

388     public static long readLong49(InputStream JavaDoc in) throws IOException JavaDoc {
389         return ((long) (readInt49(in)) & 0xFFFFFFFFL) + (readInt49(in) << 32);
390     }
391
392     /**
393      * read big-endian long
394      */

395     public static long readLong49(RandomAccessFile JavaDoc in) throws IOException JavaDoc {
396         return ((long) (readInt49(in)) & 0xFFFFFFFFL) + (readInt49(in) << 32);
397     }
398
399     /**
400      * read big-endian long
401      */

402     public static long readLong49(RandomAccessRO in) throws IOException JavaDoc {
403         return ((long) (readInt49(in)) & 0xFFFFFFFFL) + (readInt49(in) << 32);
404     }
405
406
407     public static void readFully(InputStream JavaDoc in, byte b[]) throws IOException JavaDoc {
408         readFully(in, b, 0, b.length);
409     }
410
411     public static void readFully(InputStream JavaDoc in, byte b[], int off, int len) throws IOException JavaDoc {
412         int n = 0;
413         do {
414             int count = in.read(b, off + n, len - n);
415             if(count < 0) {
416                 throw new EOFException JavaDoc("eof");
417 // return;
418
}
419             n += count;
420         }
421         while(n < len);
422     }
423
424     /**
425      * this method is like readFully, but instead of throwing <code>EOFException</code> it returns count of read bytes
426      *
427      * @param in InputStream to read
428      * @param b byte array to fill
429      *
430      * @return number of bytes read into the buffer, or -1 if EOF was reached
431      *
432      * @throws IOException
433      */

434     public static int readFully2(InputStream JavaDoc in, byte b[]) throws IOException JavaDoc {
435         return readFully2(in, b, 0, b.length);
436     }
437
438     /**
439      * this method is like readFully, but instead of throwing <code>EOFException</code> it returns count of read bytes
440      *
441      * @param in InputStream to read
442      * @param b byte array to fill
443      * @param off start offset in byte array
444      * @param len number of bytes to read
445      *
446      * @return number of bytes read into the buffer, or -1 if EOF was reached
447      *
448      * @throws IOException
449      */

450     public static int readFully2(InputStream JavaDoc in, byte b[], int off, int len) throws IOException JavaDoc {
451         int n = 0;
452         do {
453             int count = in.read(b, off + n, len - n);
454             if(count < 0) {
455                 return n == 0 ? -1 : n;
456             }
457             n += count;
458         }
459         while(n < len);
460         return n;
461     }
462 }
463
Popular Tags