public int buffer(int size){
    // first, clear out any space that has been previously returned
    if(returned!=0){
      fill-=returned;
      if(fill>0){
        System.arraycopy(data, returned, data, 0, fill);
      }
      returned=0;
    }

    if(size>storage-fill){
      // We need to extend the internal buffer
      int newsize=size+fill+4096; // an extra page to be nice
      if(data!=null){
        byte[] foo=new byte[newsize];
        System.arraycopy(data, 0, foo, 0, data.length);
        data=foo;
      }
      else{
        data=new byte[newsize];
      }
      storage=newsize;
    }

    return (fill);
  }