This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Get File from Server as Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Get File from Server as Byte Array" Watch "Get File from Server as Byte Array" New topic
Author

Get File from Server as Byte Array

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I need to get some files from the server as a byte array. I'm currently using a URL connection and DataInputStream with the following code snippet to read the file:

Is there a better/faster way to do the above snippet?
Can I read multiple bytes at a time?
Thanks,
Drew
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you're just going to read all bytes into an array, there's really no use for a DataInputStream or anything other than the original FileInputStream. You can certainly read multiple bytes at once, and it's preferable:


"I'm not back." - Bill Harding, Twister
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Can this be used for getting the file from a server?
With a URL connection?
Drew
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I am referring to a remote server, which I can't access except through the Internet. (most likely though an HTTP connection)
Drew
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
This can be used for any InputStream, provided you know the total number of bytes beforehand. If you don't, then use a ByteArrayOutputStream:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get File from Server as Byte Array
 
Similar Threads
FileLock in XP environment
reading binary file in java(with code also)
Blocking an InputStream
reading big file using java nio
using byte array to copy contents of one file to another