Re: Reading from a socket: first characters, then octets
Stefan Ram wrote:
When a client sends an HTTP PUT request to my web server,
I start to read characters from the socket:
this.br =
new java.io.BufferedReader
( new java.io.InputStreamReader
( socket.getInputStream(), "ANSI_X3.4-1968" ));
But sometimes, after the initial text, the socket will
change to emit binary data (octets), that is, it will
send me the actual data of the PUT request during the
same transmission (TCP session).
I don't know that charset, but as long as a line break
is there 0x0a and/or 0x0d as well you can do the following:
br = new BufferedReader(new InputStreamReader(is, "8859_1"));
String line;
while ((line = br.readLine()) != null){
String realText = new String(line.getBytes("8859_1"), "ANSI_X3.4-1968");
if (lineFitsCondition(realText)){
break;
}
}
char[] buf = new char[BUF_SIZE];
int read;
while ((read = br.read(buf)) != -1){
addBinaryDataToWhatever(new String(buf, 0, read).getBytes("8859_1"));
}
It's ugly, but it works and you don't need to fiddle around with
the internals of InputStreamReader (which is buffering with
quite some buffer - AFAIR 4096 bytes).
Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!