2

I'd like to read text and binary from a stream, where the stream could be a file or a URL connection. Both streams have the same format, where there's an ASCII text header followed by a large binary block of data. I'm using DataInputStream to do this. For files, I'm using

DataInputStream dis = new DataInputStream(new FileInputStream(new File("test")));

For URL's, I'm using (where uc is initialized to point to a URL):

DataInputStream dis = new DataInputStream(new BufferedInputStream(uc.getInputStream()));

After setting up the DataInputStream, I follow that with:

dis.readLine()
dis.read(buf);

This works, but I noticed that readLine is depecrated (even though there are posts that refer to using it). Is it OK to keep using it since my text is ASCII? If that's not a good idea, and I go with the JDK recommendation to use BufferedReader, is there a way to access both text and binary? I tried BufferedReader to get the text header, but then I got incorrect binary data when using the underlying stream, probably because some of it was already consumed.

Community
  • 1
  • 1
Ravi
  • 3,718
  • 7
  • 39
  • 57
  • 2
    How long are the ASCII text headers? Are they of fixed length? Could you read the header as "binary data" and convert to one or more Strings? – John Glassmyer Dec 06 '11 at 01:46
  • 1
    The header is variable length, and I need to look for a delimiter to know where the binary data starts. But as you say, I think I can read the text as binary (using readByte) and build up a String. Thanks. – Ravi Dec 06 '11 at 01:51

1 Answers1

0

The reason it was deprecated was the failure to properly convert bytes to chars. Since this example is limited to ASCII, there's no problem in using it.

Ravi
  • 3,718
  • 7
  • 39
  • 57