classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A patch for BufferedInputStream


From: Wu, Gansha
Subject: A patch for BufferedInputStream
Date: Fri, 24 Aug 2001 10:38:25 +0800

BufferedInputStream has the flexibility for its subclass to control buffer 
behavior. So it leaves some fields such as "count" and "pos" accessible by 
its subclass to realize some specific buffer behaviors such as recyclable 
buffer. 
So we should consious about the usage of such fields. For example, a 
buffer will set "pos" to discretional position or set "count" to discretional 
number 
(for a recyclable buffer, it could set it to 0). 
So it's too weak in current implementation of read that just verify 
"if(pos == count)" to decide whether refill buffer.
A patch should like this:

public synchronized int
read() throws IOException
{
-  if ((pos == count) || !primed)
+   if ((pos >= count) || !primed)

    {
      refillBuffer(1);
      
-      if (pos == count)
+           if (pos >= count)
        return(-1);
    }

  ++pos;

  return((buf[pos - 1] & 0xFF));
}


public synchronized int
read(byte[] buf, int offset, int len) throws IOException
{
    ... ...
  // Read the rest of the bytes
  try
    {
      for(;total_read != len;)
        {
-          if (pos == count)
+          if (pos >= count)
            refillBuffer(len - total_read);

-          if (pos == count)
+          if (pos >= count)
            if (total_read == 0)
              return(-1);
            else
              return(total_read);
    ... ...
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]