Re: what happens to buffer ?



On 30 Mar 2006 02:30:31 -0800, gk wrote:
what should be the size of buffer ?

is byte[] buffer = new byte[512]; ENOUGH ?

suppose, at some point of time huge number of bytes (say 1000 bytes)
stormed .

Then what will happen ? the buffer cant accept more than 512 bytes
.....will the additional bytes 1000-512 = 488 will still be in the
stream ? or they will be lost ?

the reason is, some people use

byte[] buffer = new byte[256];
byte[] buffer = new byte[512];
byte[] buffer = new byte[1024];

which one is good ?

or anything is ok . does it matter really ? does the coder
responsible for choosing the size of the byte ?

Each time you call read(), the new bytes are written at the start of
the buffer unless you tell read() to do otherwise. If there were
already some data in the buffer from a previous read, it will be
overwritten with the new data.

Also, read() will never read more than the number of characters you
request, or the length of the buffer if you don't specify. Note that
read() can and often will return *fewer* characters than you request,
so you need to check the return value.

Any bytes you don't read will wait nicely in the stream until you
choose to read them.

So you can decide to read as much or as little as you want each time,
and can choose an apropriate buffer size. Normally it's more efficient
to read a lot of data each time and in powers of two, but depending on
your application you may want to read less.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
.



Relevant Pages

  • Re: Questions about buffered streams
    ... Buffer classes are supposed to help us with that, ... even if buffered stream doesn’t immediately call ... then PrintStream converts characters into bytes using specified ... PrintWriter is one of the character-based classes. ...
    (comp.lang.java.programmer)
  • Re: scanf() quesion?
    ... characters are intended to ... When a stream is fully buffered, ... when a buffer is filled. ... If standard output refers to an interactive device, ...
    (comp.lang.c)
  • Questions about buffered streams
    ... Buffer classes are supposed to help us with that, ... data and thus don’t necessarelly call underlying system for each byte. ... even if buffered stream doesn’t immediately call ... then PrintStream converts characters into bytes using specified ...
    (comp.lang.java.programmer)
  • Re: scanf() quesion?
    ... this is because the standard output ... stream is typically line-buffered. ... characters are intended to ... when a buffer is filled. ...
    (comp.lang.c)
  • Re: scanf() quesion?
    ... characters are intended to ... When a stream is fully buffered, ... when a buffer is filled. ... input), _standard output_, and ...
    (comp.lang.c)