static int lowLevelCompressBytes ( final byte [ ] byteArray , // input final int numBytesToEncode , // input, must be an int final short [ ] encodingTable , // input final int [ ] compressedWords ) { // output int nextWordIndex = 0 ; long bitBuf = 0 ; // bits are packed into this first, then are flushed to compressedWords int bufBits = 0 ; // number of bits currently in bitbuf; must be between 0 and 31 for ( int byteIndex = 0 ; byteIndex < numBytesToEncode ; byteIndex ++ ) { final int theByte = byteArray [ byteIndex ] & 0XFF ; final long codeInfo = ( encodingTable [ theByte ] & 0XFFFF L ) ; final long codeVal = codeInfo & 0XFFF L ; final int codeWordLength = ( int ) ( codeInfo >>> 12 ) ; bitBuf |= ( codeVal << bufBits ) ; bufBits += codeWordLength ; //MAYBE_FLUSH_BITBUF(compressedWords, nextWordIndex); if ( bufBits >= 32 ) { compressedWords [ nextWordIndex ++ ] = ( int ) bitBuf ; bitBuf >>>= 32 ; bufBits -= 32 ; } } //Pad the bitstream with 11 zero-bits so that the decompressor's 12-bit peek // can't overrun its input. bufBits += 11 ; //MAYBE_FLUSH_BITBUF(compressedWords, nextWordIndex); if ( bufBits >= 32 ) { compressedWords [ nextWordIndex ++ ] = ( int ) bitBuf ; bitBuf >>>= 32 ; bufBits -= 32 ; } if ( bufBits > 0 ) { // We are done encoding now, so we flush the bit buffer. assert ( bufBits < 32 ) ; compressedWords [ nextWordIndex ++ ] = ( int ) bitBuf ; } return nextWordIndex ; }
It is the caller s responsibility to ensure that the compressedWords array is long enough .