InputStream unreadable



Here's a case I cannot solve:

I am trying to write a program that downloads documents from the web
via Sockets, analyzes the HTTP headers and then, if desired, writes the
document to disk, the document may be ASCII or binary.

Reading the header implies requiring ASCII reading, so I use a
BufferedReader on the input stream - most importantly to detect the
double CRLF denoting the end of the header.

I want to then switch to binary reading, in case the file is, say, a
Word doc or a PDF.

Thr trouble is that once the InputStream has been wrapped in the
BufferedReader, even if I keep an explicit reference to the stream, it
won't read.

Example:
//==================
import java.io.*;
import java.net.*;

public class TestStreamReading {

public static void main(String[] args) throws Exception {
System.out.println("\u0007");
new TestStreamReading().run();
}

public void run() throws Exception {
ServerSocket ss = new ServerSocket(1526);
while(true) {
Socket xion = ss.accept();

InputStream in = xion.getInputStream();

//* If the InputStreamReader is used, the InputStream no longer is
useable alone.
InputStreamReader isr = new InputStreamReader(in);
char[] cbuf = new char[50];
isr.read(cbuf,0,cbuf.length);
System.out.print(cbuf);
/*
isr = null;// this doesn't change a thing (destroying the reader
reference)
Runtime rt = Runtime.getRuntime();
rt.gc();
//*/

byte[] buff = new byte[50];
in.read(buff,0,buff.length);
//isr.super.read(buff,0,buff.length); // isr is a reader, not a
stream. "super" can only be used on classes

System.out.print(new String( buff ) +"/done" );
}
}

}
// ==================

(run the prog and then feed it data via localhost on port 1526 - using
a web browser for instance)

When run, the input stream reader (or any reader for that matter)
reads and prints the input back as expected. The subsequent call to the
raw input stream however causes the printing to yeild empty characters.

With further extension, we find that writing the data to a file simply
yeilds an empty file (not full of whitespace - just empty!)

I can't seem to find any documentation on this behaviour. Is it normal?
Is there a way around this, short of writing a special dual reading
class?

.



Relevant Pages

  • Re: InputStream unreadable
    ... >Reading the header implies requiring ASCII reading, ... >BufferedReader on the input stream - most importantly to detect the ... >double CRLF denoting the end of the header. ...
    (comp.lang.java.programmer)
  • Re: How to determine integer values in a txt file?
    ... the fixed header, skip 2 bytes and read the 5th byte to determine how many ... The problem boils down to one of positioning and then reading - how it can ... the stream then the task is easy - just read the header in! ... really, quite easy to accomplish. ...
    (comp.lang.java.programmer)
  • Re: confused about word templates
    ... dialog and the ³same as previous² setting in the header and footer settings ... And congratulations on reading the documentation. ... stubborn as I am when I have a formatting problem. ... > textedit files are in word format and open in Word} ...
    (microsoft.public.mac.office.word)
  • Re: Parsing Multipart formdata
    ... Read the header, ... Start reading the part header, ... Stop reading into the string buffer when you see the boundary again. ... The array of bytes is your binary data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A reference to a hash member? (to an objects member variable)
    ... I have an object, which reads a header ... To handle those 2 reading states ... (reading a header; reading a chat string) ... Also, OFFSET, in this case, is not the file offset. ...
    (comp.lang.perl.misc)