How to read the contents from an InputStream reference and store it in a byte array?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Monday, July 24, 2006 04:25 AM

Can anyone please tell me how to read the contents from an InputStream reference and store them in a byte array? Actualy I have got a file containing a list of String representaion of url's, like : file://java.sun.com/images/one.jpg file://java.sun.com/images/two.jpg . . . So what i want is to convert these String representaions t actual URL objects, an from the underlying InputStream reference of each URL object i have to read the contents and convert it into a byte array? also there is a method, like : void store(String id,String name,InputStream iStream){} inside which i have to create an instance of another class called "   More>>

Can anyone please tell me how to read the contents from an InputStream
reference and store them in a byte array?
Actualy I have got a file containing a list of String representaion of
url's, like :

file://java.sun.com/images/one.jpg
file://java.sun.com/images/two.jpg
.
.
.

So what i want is to convert these String representaions t actual URL
objects, an from the underlying InputStream reference of each URL object
i have to read the contents and convert it into a byte array?

also there is a method, like :

void store(String id,String name,InputStream iStream){}

inside which i have to create an instance of another class called "FileEntry"
whose attributes will be :

FileEntry(String id,String name,InputStream iStream)

also inside this method i have to :
1) convert the InputStream object to a URL object
2) before doing step1 read the complete contents from the InputStream and store it
in a byte array.

If these two things can be written in diffrent methods, that convinient
for me

Thanks and Regards
Subin

   <<Less

Re: How to read the contents from an InputStream reference and store it in a byte array?

Posted By:   Stephen_Ostermiller  
Posted On:   Sunday, August 27, 2006 07:10 PM

Can anyone please tell me how to read the contents from an InputStream reference and store them in a byte array?

The easiest way would be to read the bytes and write them to a ByteArrayOutputStream. The ByteArrayOutputStream is backed by a byte array that it resizes for you. When you are done reading and writing, you can retrieve the byte array from the ByteArrayOutputStream.

Actualy I have got a file containing a list of String representaion of url's, like : file://java.sun.com/images/one.jpg file://java.sun.com/images/two.jpg . . . So what i want is to convert these String representaions t actual URL objects, an from the underlying InputStream reference of each URL object i have to read the contents and convert it into a byte array?


It sounds like a byte array is the wrong tool for the job. You really should be reading a text file using a character stream. Instead of opening the file using a FileInputStream, consider opening it with a FileReader. You can get character data from a reader, as opposed to byte data from the input stream. If you don't have access to a file and are handed an InputStream by your API, you should convert the input stream to a Reader with a InputStreamReader class.


After you have a reader, you need to tokenize the stream into urls. If the urls are one per line, I would recommend the BufferedReader and using the readLine() method. Otherwise, you may have to read one character at a time, add to a string buffer, and then create the url object once the entire url has been read.

About | Sitemap | Contact