KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Streams


1 /*
2   Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.EOFException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25
26 /**
27  * This class contains some utility functions for streams.
28  */

29 public class Streams {
30
31     /**
32      * Reads an input stream until it reaches the end and store the data
33      * in an array of bytes.
34      *
35      * <p> this method was extracted "as is" from javassist 1.0
36      * from Shigeru Chiba.
37      *
38      * <p><a HREF="www.csg.is.titech.ac.jp/~chiba/javassist/">Javassist
39      * Homepage</a>
40      *
41      * @param fin the input stream to read the class from
42      * @return the contents of that input stream
43      * @throws IOException if the size of the file is equal or more than 1Mbyte.
44      */

45
46     public static byte[] readStream(InputStream JavaDoc fin) throws IOException JavaDoc {
47         byte[][] bufs = new byte[8][];
48         int bufsize = 4096;
49     
50         for (int i = 0; i < 8; ++i) {
51             bufs[i] = new byte[bufsize];
52             int size = 0;
53             int len = 0;
54             do {
55                 len = fin.read(bufs[i], size, bufsize - size);
56                 if (len >= 0)
57                     size += len;
58                 else {
59                     byte[] result = new byte[bufsize - 4096 + size];
60                     int s = 0;
61                     for (int j = 0; j < i; ++j) {
62                         System.arraycopy(bufs[j], 0, result, s, s + 4096);
63                         s = s + s + 4096;
64                     }
65             
66                     System.arraycopy(bufs[i], 0, result, s, size);
67                     return result;
68                 }
69             } while (size < bufsize);
70             bufsize *= 2;
71         }
72         throw new IOException JavaDoc("readStream function has too much data to load.");
73     }
74
75     /**
76      * Reads an unsigned integer in little endian encoding
77      * @param in the stream to read the integer from
78      */

79     public static long readUInt(InputStream JavaDoc in) throws IOException JavaDoc {
80         long s1 = readUShort(in);
81         long s2 = readUShort(in);
82         return (s2 << 16) | s1;
83     }
84    
85     /**
86      * Reads unsigned short in little endian encoding
87      * @param in the stream to read the short integer from
88      */

89     public static int readUShort(InputStream JavaDoc in) throws IOException JavaDoc {
90         int b1 = readUByte(in);
91         int b2 = readUByte(in);
92         return (b2 << 8) | b1;
93     }
94
95     /**
96      * Reads unsigned byte
97      * @param in the stream to read the byte from
98      */

99     public static int readUByte(InputStream JavaDoc in) throws IOException JavaDoc {
100         int b = in.read();
101         if (b == -1)
102             throw new EOFException JavaDoc();
103         return b;
104     }
105
106     /**
107      * Read data from an InputStream and writes it to an OutputStream
108      */

109     public static void copy(InputStream JavaDoc in, OutputStream JavaDoc out)
110         throws IOException JavaDoc
111     {
112         int bufferSize = 1024;
113         byte[] buffer = new byte[bufferSize];
114         int read;
115         while ((read=in.read(buffer,0,bufferSize))!=-1) {
116             out.write(buffer,0,read);
117         }
118     }
119 }
120
Popular Tags