001    /*
002      Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.io.EOFException;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    
025    
026    /**
027     * This class contains some utility functions for streams.
028     */
029    public class Streams {
030    
031        /**
032         * Reads an input stream until it reaches the end and store the data
033         * in an array of bytes.
034         *
035         * <p> this method was extracted "as is" from javassist 1.0
036         * from Shigeru Chiba.
037         *
038         * <p><a href="www.csg.is.titech.ac.jp/~chiba/javassist/">Javassist
039         * Homepage</a>
040         * 
041         * @param fin the input stream to read the class from
042         * @return the contents of that input stream 
043         * @throws IOException if the size of the file is equal or more than 1Mbyte.
044         */
045    
046        public static byte[] readStream(InputStream fin) throws IOException {
047            byte[][] bufs = new byte[8][];
048            int bufsize = 4096;
049            
050            for (int i = 0; i < 8; ++i) {
051                bufs[i] = new byte[bufsize];
052                int size = 0;
053                int len = 0;
054                do {
055                    len = fin.read(bufs[i], size, bufsize - size);
056                    if (len >= 0)
057                        size += len;
058                    else {
059                        byte[] result = new byte[bufsize - 4096 + size];
060                        int s = 0;
061                        for (int j = 0; j < i; ++j) {
062                            System.arraycopy(bufs[j], 0, result, s, s + 4096);
063                            s = s + s + 4096;
064                        }
065                        
066                        System.arraycopy(bufs[i], 0, result, s, size);
067                        return result;
068                    }
069                } while (size < bufsize);
070                bufsize *= 2;
071            }
072            throw new IOException("readStream function has too much data to load.");
073        }
074    
075        /**
076         * Reads an unsigned integer in little endian encoding
077         * @param in the stream to read the integer from
078         */
079        public static long readUInt(InputStream in) throws IOException {
080            long s1 = readUShort(in);
081            long s2 = readUShort(in);
082            return (s2 << 16) | s1;
083        }
084       
085        /**
086         * Reads unsigned short in little endian encoding
087         * @param in the stream to read the short integer from
088         */
089        public static  int readUShort(InputStream in) throws IOException {
090            int b1 = readUByte(in);
091            int b2 = readUByte(in);
092            return (b2 << 8) | b1;
093        }
094    
095        /**
096         * Reads unsigned byte
097         * @param in the stream to read the byte from
098         */
099        public static int readUByte(InputStream in) throws IOException {
100            int b = in.read();
101            if (b == -1)
102                throw new EOFException();
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 in, OutputStream out) 
110            throws IOException 
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    }