[SOLVED] InputStream into ByteBuffer
I have an InputStream. I want to put the entire contents of the stream into a ByteBuffer. Is there a better way than this? At least there must be a nicer way to have a growable array.
Code:
InputStream in;
int BUFSIZE = 1024;
Vector<Byte> byteV = new Vector<Byte>();
byte[] tmp1 = new byte[BUFSIZE];
while (true) {
int r = in.read(tmp1, 0, BUFSIZE);
if (r == -1) break;
for (int i=0; i<r; i++) {
byteV.add(tmp1[i]);
}
}
byte[] tmp2 = new byte[byteV.size()];
for (int i=0; i<byteV.size(); i++) {
tmp2[i] = byteV.elementAt(i);
}
ByteBuffer buf = ByteBuffer.wrap(tmp2);