• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get File from Server as Byte Array

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can this be used for getting the file from a server?
With a URL connection?
Drew
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This can be used for any InputStream, provided you know the total number of bytes beforehand. If you don't, then use a ByteArrayOutputStream:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic