public int readB(int bits){
    //System.err.println(this+" read: bits="+bits+", storage="+storage+", endbyte="+endbyte+
    //                        ", ptr="+ptr+", endbit="+endbit+", buf[ptr]="+buffer[ptr]);
    int ret;
    int m=32-bits;

    bits+=endbit;

    if(endbyte+4>=storage){
      /* not the main path */
      ret=-1;
      if(endbyte*8+bits>storage*8) {
	ptr+=bits/8;
	endbyte+=bits/8;
	endbit=bits&7;
	return(ret);
      }
    }

    ret=(buffer[ptr]&0xff)<<(24+endbit);
    if(bits>8){
      ret|=(buffer[ptr+1]&0xff)<<(16+endbit);
      if(bits>16){
	ret|=(buffer[ptr+2]&0xff)<<(8+endbit);
	if(bits>24){
	  ret|=(buffer[ptr+3]&0xff)<<(endbit);
	  if(bits>32 && (endbit != 0))
	    ret|=(buffer[ptr+4]&0xff)>>(8-endbit);
	}
      }
    }
    ret=(ret>>>(m>>1))>>>((m+1)>>1);

    ptr+=bits/8;
    endbyte+=bits/8;
    endbit=bits&7;
    return(ret);
  }