K
kevin.osborne
I'm reading some bytes over a socket off a non-http server.
After writing a message to the socket, I grab the return stream with:
InputStream stream = _ssl_socket.getInputStream();
stream.available() [result: 0]
(new InputStreamReader(stream)).ready() [result: false]
And yet:
(new BufferedReader(new InputStreamReader(stream))).readLine()
Returns the expected response from the server.
However, readLine clobbers cr/lf chars, which are integral parts of my
incoming datastream, and define its structure. There are separate 0x0a
& 0x0d carhs scattered throughout the stream, and no quibbling; I need
them.
My peeve here is that because stream.available() returns 0, I cannot
predeclare a char array of a commensurate size with the inclong data,
and must instead allocate unncessary memory to an oversize array. i.e
because:
char[] char_buff = new char[stream.available()];
read_result = stream_reader.read(char_buff, 0, stream.available());
doesn't work, I have to do:
char[] char_buff = new char[MAX_MESSAGE_SIZE];
read_result = stream_reader.read(char_buff, 0, MAX_MESSAGE_SIZE);
where MAX_MESSAGE_SIZE is dictated by the foriegn server, and is
enormous. and no I cannot change the foreign server behaviour in any
way shape or form.
So my options are:
- use readLine, and butcher my input beyond usable form
- waste memory on hugely oversized char arrays for mostly tiny
messages
- use int read() and crawl through the stream char by char
i.e. cost myself correctness, memory and cpu/network performance
respectively.
Ideally I'd like a working available() call to be able to sensibly
declare my char array, and then do the read. If it helps the conundrum
of ready=false/available=0/readLine=lots'o'data, this is all happenning
via an SSLSocket connection.
TIA
Kevin
After writing a message to the socket, I grab the return stream with:
InputStream stream = _ssl_socket.getInputStream();
From this object, I get the following advisements:
stream.available() [result: 0]
(new InputStreamReader(stream)).ready() [result: false]
And yet:
(new BufferedReader(new InputStreamReader(stream))).readLine()
Returns the expected response from the server.
However, readLine clobbers cr/lf chars, which are integral parts of my
incoming datastream, and define its structure. There are separate 0x0a
& 0x0d carhs scattered throughout the stream, and no quibbling; I need
them.
My peeve here is that because stream.available() returns 0, I cannot
predeclare a char array of a commensurate size with the inclong data,
and must instead allocate unncessary memory to an oversize array. i.e
because:
char[] char_buff = new char[stream.available()];
read_result = stream_reader.read(char_buff, 0, stream.available());
doesn't work, I have to do:
char[] char_buff = new char[MAX_MESSAGE_SIZE];
read_result = stream_reader.read(char_buff, 0, MAX_MESSAGE_SIZE);
where MAX_MESSAGE_SIZE is dictated by the foriegn server, and is
enormous. and no I cannot change the foreign server behaviour in any
way shape or form.
So my options are:
- use readLine, and butcher my input beyond usable form
- waste memory on hugely oversized char arrays for mostly tiny
messages
- use int read() and crawl through the stream char by char
i.e. cost myself correctness, memory and cpu/network performance
respectively.
Ideally I'd like a working available() call to be able to sensibly
declare my char array, and then do the read. If it helps the conundrum
of ready=false/available=0/readLine=lots'o'data, this is all happenning
via an SSLSocket connection.
TIA
Kevin