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.