Friends,

I am stuck here , I am downloading some files from a url uing GZIPInputStream.I need to store this files.So i declared a bytearray with specified length.As i am downloading files with different size, i think i need to declare a dynamic byte array.

Please help me to solve these issue.

Thank You

Recommended Answers

All 7 Replies

Show your code, so we can better understand what you're talking about. Generally speaking though, you do not need a "dynamic array", you need to use your "static array" properly.

Maybe you need something like this:

InputStream in = zipFile.getInputStream();
OutputStream out = new FileOutputStream(new File("a.bin"));
byte buf[] = new byte[1024 * 8]; /* declare a 8kB buffer */
int len = -1;
while((len = in.read(buf)) != -1) {
    out.write(buf, 0, len);
}

Convert an InputStream to a byte array:

InputStream is = readInputStreamFromSomewhere();
byte[] data = {};
byte[] temp = new byte[1024];//declare a 1kb array, but really any size you want.
while( is.available() > 0 ){
     int read = is.read( temp );
     byte[] temp2 = new byte[data.length + read];
     System.arraycopy( data, 0, temp2, 0, data.length );
     System.arraycopy( temp, 0, temp2, data.length, read );
     data = temp2;
}

That is not "converting" an InputStream to anything. That is reading an inputstream and storing (rather inefficiently) all of the read bytes into an array. Better would be to read it, and write it to a ByteArrayOutputStream and then simply get the bytes from that.

That is not "converting" an InputStream to anything. That is reading an inputstream and storing (rather inefficiently) all of the read bytes into an array. Better would be to read it, and write it to a ByteArrayOutputStream and then simply get the bytes from that.

I didn't claim it was the best way to do it. I was merely trying to show the OP that they didn't require a 'dynamic' ByteBuffer implementation to read an unknown length input stream, something you pointed out in your previous post to this thread without going into specifics. Now I've lost a precious few minutes of my life I could have been surfing porn.

/edit The data was available as an input stream, and now it's available as a byte array. How is that not converting an InputStream into a byte array? Sure, the process of conversion required reading the stream and storing the bytes in an array (efficiency, or lack thereof, notwithstanding). It was still converted nonetheless.

Nevermind, I am not going to get into an argument over "half-knowledge" and spoonfeeding.

Edit: Especially in a thread that is over two years dead. And by "not going to into detail" I assume you mean that I ddin't spoonfeed code. I wanted to see his code, first. It was, after all, his job to do, not mine.

Nevermind, I am not going to get into an argument over "half-knowledge" and spoonfeeding.

Edit: Especially in a thread that is over two years dead. And by "not going to into detail" I assume you mean that I ddin't spoonfeed code. I wanted to see his code, first. It was, after all, his job to do, not mine.

You're right. Arguing in a dead thread is beyond dumb. I'll admit that I responded to a certain tone of superiority in your post, chalk it up to it being Monday today and drop it. Have a good one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.