943,534 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6887
  • Java RSS
Jan 9th, 2008
0

Regarding byte arrays

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sarath.koiloth is offline Offline
30 posts
since Sep 2006
Jan 9th, 2008
0

Re: Regarding byte arrays

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 9th, 2008
0

Re: Regarding byte arrays

Maybe you need something like this:
java Syntax (Toggle Plain Text)
  1. InputStream in = zipFile.getInputStream();
  2. OutputStream out = new FileOutputStream(new File("a.bin"));
  3. byte buf[] = new byte[1024 * 8]; /* declare a 8kB buffer */
  4. int len = -1;
  5. while((len = in.read(buf)) != -1) {
  6. out.write(buf, 0, len);
  7. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
May 10th, 2010
0
Re: Regarding byte arrays
Convert an InputStream to a byte array:
Java Syntax (Toggle Plain Text)
  1. InputStream is = readInputStreamFromSomewhere();
  2. byte[] data = {};
  3. byte[] temp = new byte[1024];//declare a 1kb array, but really any size you want.
  4. while( is.available() > 0 ){
  5. int read = is.read( temp );
  6. byte[] temp2 = new byte[data.length + read];
  7. System.arraycopy( data, 0, temp2, 0, data.length );
  8. System.arraycopy( temp, 0, temp2, data.length, read );
  9. data = temp2;
  10. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javagrendel is offline Offline
3 posts
since May 2010
May 10th, 2010
0
Re: Regarding byte arrays
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.
Last edited by masijade; May 10th, 2010 at 1:25 pm.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 10th, 2010
0
Re: Regarding byte arrays
Click to Expand / Collapse  Quote originally posted by masijade ...
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.
Last edited by javagrendel; May 10th, 2010 at 1:51 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javagrendel is offline Offline
3 posts
since May 2010
May 10th, 2010
0
Re: Regarding byte arrays
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.
Last edited by masijade; May 10th, 2010 at 2:48 pm.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 10th, 2010
0
Re: Regarding byte arrays
Click to Expand / Collapse  Quote originally posted by masijade ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javagrendel is offline Offline
3 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Statement::excuteUpdate vs Statement::excuteQuery
Next Thread in Java Forum Timeline: Creat a tree from list of strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC