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.

2
  • 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? Commented Dec 6, 2011 at 1: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. Commented Dec 6, 2011 at 1:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.