954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regarding byte arrays

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

sarath.koiloth
Light Poster
30 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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);
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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;
}
javagrendel
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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.

javagrendel
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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.

javagrendel
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You