1
package tim.prune.load;
3
import java.io.IOException;
4
import java.io.InputStream;
7
* Class to scoop bytes from an input stream into an array.
8
* The size of the array doesn't have to be known in advance.
9
* This is used for getting images and sound files out of zip
10
* files or from remote URLs.
12
public class ByteScooper
14
/** Bucket size in bytes */
15
private static final int BUCKET_SIZE = 5000;
16
/** Amount by which barrel size is increased on demand */
17
private static final int BARREL_SIZE_INCREMENT = 100000;
20
* Scoop bytes from the given input stream and return the result
21
* @param inIs input stream to scoop bytes from
24
public static byte[] scoop(InputStream inIs) throws IOException
26
byte[] _barrel = new byte[BARREL_SIZE_INCREMENT];
27
byte[] _bucket = new byte[BUCKET_SIZE];
28
int numBytesInBarrel = 0;
29
// read from stream into the bucket
30
int numBytesRead = inIs.read(_bucket);
31
while (numBytesRead >= 0)
33
// expand barrel if necessary
34
if ((numBytesInBarrel + numBytesRead) > _barrel.length)
36
byte[] newBarrel = new byte[_barrel.length + BARREL_SIZE_INCREMENT];
37
System.arraycopy(_barrel, 0, newBarrel, 0, numBytesInBarrel);
40
// copy from bucket into barrel
41
System.arraycopy(_bucket, 0, _barrel, numBytesInBarrel, numBytesRead);
42
numBytesInBarrel += numBytesRead;
43
// read next lot from stream into the bucket
44
numBytesRead = inIs.read(_bucket);
46
// Now we know how many bytes there are, so crop to size
47
if (numBytesInBarrel == 0) return null;
48
byte[] result = new byte[numBytesInBarrel];
49
System.arraycopy(_barrel, 0, result, 0, numBytesInBarrel);