Re: Read all from stdin

"soubok" <[email protected]>
Newsgroups gmane.comp.mozilla.devel.nspr
Organization http://groups.google.com
Message-ID <[email protected]>
On Feb 13, 4:36 pm, Wan-Teh Chang <[email protected]> wrote:
> soubok wrote:
> > Hello,
> > I need to read all remaining data from stdin (PR_StandardInput) but it
> > seems that PR_Available do not work with stdin file.
>
> PR_Available should work with stdin on Unix (including Linux
> and Mac OS X).  On Windows PR_Available does not work with
> stdin because the implementation of PR_Available uses a Winsock
> function.
>
> > Is there a way to PR_Read all datas from stdin at once ?
>
> This depends on exactly what you want to do.  Again, Windows
> presents a challenge.
>
> 1. You can put PR_StandardInput in non-blocking mode and
> keep reading from PR_StandardInput until PR_Read returns
> 0 or -1.
>
> This method only works on Unix (including Linux and Mac OS
> X).  This method doesn't work on Windows because the function
> to set a PRFileDesc in non-blocking mode calls Winsock functions.
>
> 2. If you know the maximum number of bytes that you may
> read from stdin, you can allocate a buffer of that size,
> and call PR_Read on stdin (in the default blocking mode).
> When PR_Read returns, you have read all the data.
>
> 3. If you control how the peer sends data to your stdin,
> you can arrange for the peer to send the byte count before
> the data.  After you have read the byte count, you know
> how many bytes of data will follow.
>
> Wan-Teh

Thanks for your response.

I am writing a CGI ( for Linux and win32 ) and I need to read data on
stdin.
The first solution I have is to read several times small amount of
data:

  var data='';
  for ( var tmp; tmp = File.stdin.Read(1000); tmp == 1000 )
    data += tmp;

But I do not like this solution because string concatenation causes a
slight overhead.

The other solution is to read a big amount of data, but I waste
memory.

This is why I wondering if there is a way to know in advance how many
data I can read.

Soubok.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.