Re: InputStream

From: Steve Horsley (shoot_at_the.moon)
Date: 01/04/05


Date: Tue, 04 Jan 2005 19:39:20 +0000

JScoobyCed wrote:
> Hi,
>
> I have been trying several solutions lately as to how to manage a
> non-blocking inputstream "listener".
> I have a InputStream, from which I read in a separate Thread.
> When data come, I know the first 4 bytes are the size of data coming.
> Then I create a byte array to receive the number of expected bytes. When
> I have received the correct amount, I reset the size of data to 0. This
> tell me that next 4 bytes are a new message size.
>
> My questions:
> - is there a limit in the number of byte to read for an InputStream (I
> could choose to use 10 bytes for the size of data to receive)

Not that I am aware of (other than the max length of a byte[]). The docs
for read() say that anything from 1 up to the size of the array will be
read. The return value tells you how many you actually got.

> - is there a EOT byte or some byte value that would make the
> InputStream.read(byte[] data) stop before the end of the expected size ?

No. But there is no guarantee that the "expected" size will arrive in one
read() operation. Especially if reading from a network connection or
networked file/drive.

> (i.e.
> <sniplet>
> int expected = getSize();
> byte[] data = new byte[expected];
> int read = is.read(data);
> System.out.println("Read: " + read + " bytes.");
> </sniplet>
>
> The displayed message always says that the read amount is lower than the
> size expected if I send binary data (PNG image captured from a camera).
>

You're sending more than will fit in one Ethernet packet, and hoping that it
will all arrive in one go? Most unlikely!

> - is there a clean way to read 10-100 Kb binary data from an InoutStream ?
>

Use a loop, reading repeatdly until the correct number of bytes has been
received. Keep track of how far up the buffer is filled, and how much more
you want (always ask for the max remaining space to be filled).
read(byte[] buffer, int offset, int maxWanted) is for doing this.

If youre not comfortable with that, DataInputStream.readFully(byte[])
does it for you.

Steve



Relevant Pages

  • Can NetworkStream.Write() block?
    ... myNetworkStream.Write(byte, int offset, int size); ... From the MSDN documentation: ... If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code, and refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error. ... It says the call blocks until the bytes are sent but aren't the bytes just queued to the write buffer until the OS feels like sending them over the wire? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can NetworkStream.Write() block?
    ... myNetworkStream.BeginWrite(bytebuffer, int offset, int size) since all that does is create a backlog of BeginWrite threads all waiting to complete. ... In general, you can assume asynchronous calls are *more* efficient than their synchronous companions, since those *always* take up a thread (namely the thread waiting for completion) while the asynchronous ones generally only take up a thread when executing the completion callback. ... The overlapped requests are simply queued until there's an opportunity to process them. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generics Questions
    ... What am I missing? ... public class SingleByteConverter: IByteConverter{ ... public int WriteToBuffer(float value, bytebuffer, int offset) { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with VS and memcpy...
    ... directly in buffer? ... memcpy(buf, temp, sizeof temp); ... It looks to me like temp has a terminating zero ...
    (microsoft.public.vc.language)