⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inputbitstream.java

📁 MG4J (Managing Gigabytes for Java) is a free full-text search engine for large document collections
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		}		else throw new UnsupportedOperationException( "position() can only be called if the underlying byte stream implements the RepositionableStream interface or if the getChannel() method of the underlying byte stream exists and returns a FileChannel" );		final int residual = (int)( position & 7 );		if ( DEBUG ) System.err.println( this + ": residual=" + residual );		if ( residual != 0 ) {			current = read();			fill = 8 - residual;		}	}	/** Tests if this stream supports the {@link #mark(int)} and {@link #reset()} methods.	 *	 * <P>This method will just delegate the test to the underlying {@link InputStream}.	 * @return whether this stream supports {@link #mark(int)}/{@link #reset()}.	 */	public boolean markSupported() {		return is.markSupported();	}	 	/** Marks the current position in this input stream. A subsequent call to	 * the {@link #reset()} method repositions this stream at the last marked position so	 * that subsequent reads re-read the same bits.	 *	 * <P>This method will just delegate the mark to the underlying {@link InputStream}.	 * Moreover, it will throw an exception if you try to mark outsite byte boundaries.	 *	 * @param readLimit the maximum limit of bytes that can be read before the mark position becomes invalid.	 * @throws IOException if you try to mark outside byte boundaries.	 */	public void mark( final int readLimit ) throws IOException {		if ( fill != 0 ) throw new IOException( "You cannot mark a bit stream outside of byte boundaries." );		is.mark( readLimit );	}	/** Repositions this bit stream to the position at the time the {@link #mark(int)} method was last called.	 *	 * <P>This method will just {@link #flush() flush the stream} and delegate	 * the reset to the underlying {@link InputStream}.	 */	public void reset() throws IOException {		flush();		is.reset();	}	/** Reads a natural number in unary coding.	 *	 * @return the next unary-encoded natural number.	 * @see OutputBitStream#writeUnary(int)	 */	public int readUnary() throws IOException {		if ( ASSERTS ) assert fill < 32 : fill + " >= " + 32;		int x;				if ( fill < 16 ) refill();		if ( fill != 0 ) {			// Clean up current and check whether it is nonzero			final int currentLeftAligned = current << 32 - fill;			if ( currentLeftAligned != 0 ) {				//System.err.println( Integer.toBinaryString( currentLeftAligned ) + ", " + Integer.toHexString( currentLeftAligned ) );				if ( ( currentLeftAligned & 0xFF000000 ) != 0 ) x = 8 - Fast.BYTEMSB[ currentLeftAligned >>> 24 ];				else if ( ( currentLeftAligned & 0xFF0000 ) != 0 ) x = 16 - Fast.BYTEMSB[ currentLeftAligned >>> 16 ];				else if ( ( currentLeftAligned & 0xFF00 ) != 0 ) x = 24 - Fast.BYTEMSB[ currentLeftAligned >>> 8 ];				else x = 32 - Fast.BYTEMSB[ currentLeftAligned & 0xFF ];				readBits += x;				fill -= x;				return x - 1;			}		}		x = fill;		while( ( current = read() ) == 0 ) x += 8;		x += 7 - ( fill = Fast.BYTEMSB[ current ] );		readBits += x + 1;		return x;	}	/** Reads a long natural number in unary coding.	 *	 * Note that by unary coding we mean that 1 encodes 0, 01 encodes 1 and so on.	 *	 * @return the next unary-encoded long natural number.	 * @see OutputBitStream#writeUnary(int)	 */	public long readLongUnary() throws IOException {		if ( ASSERTS ) assert fill < 32 : fill + " >= " + 32;		// Clean up current and check whether it is nonzero		if ( ( current & ( 1 << fill ) - 1 ) != 0 ) return readUnary(); 		long x = fill;		while( ( current = read() ) == 0 ) x += 8;		x += 7 - ( fill = Fast.BYTEMSB[ current ] );		readBits += x + 1;		return x;	}	/** Reads a natural number in &gamma; coding.	 *	 * @return the next &gamma;-encoded natural number.	 * @see OutputBitStream#writeGamma(int)	 * @see #skipGammas(int)	 */	public int readGamma() throws IOException {		int preComp;		if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {			readBits += preComp >> 16;			fill -= preComp >> 16;			return preComp & 0xFFFF;		}		final int msb = readUnary();		return ( ( 1 << msb ) | readInt( msb ) ) - 1;	}	/** Reads a long natural number in &gamma; coding.	 *	 * @return the next &gamma;-encoded long natural number.	 * @see OutputBitStream#writeGamma(int)	 * @see #skipGammas(int)	 */	public long readLongGamma() throws IOException {		int preComp;		if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {			readBits += preComp >> 16;			fill -= preComp >> 16;			return preComp & 0xFFFF;		}		final int msb = readUnary();		return ( ( 1L << msb ) | readLong( msb ) ) - 1;	}	/** Skips a given number of &gamma;-coded integers.	 *	 * <p>This method should be significantly quicker than iterating <code>n</code> times on	 * {@link #readGamma()} or {@link #readLongGamma()}, as precomputed tables are used directly,	 * so the number of method calls is greatly reduced, and the result is discarded, so	 * {@link #skip(long)} can be invoked instead of more specific decoding methods.	 * 	 * @param n the number of &gamma;-coded integers to be skipped.	 * @see #readGamma()	 */	public void skipGammas( int n ) throws IOException {		int preComp;		while( n-- != 0) {			if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] >> 16 ) != 0 ) {				readBits += preComp;				fill -= preComp;				continue;			}			skip( (long)readUnary() );		}	}	/** Reads a given number of &gamma;-coded integers.	 * 	 * <p>This method should be significantly quicker than iterating <code>n</code> times on	 * {@link #readGamma()}, as precomputed tables are used directly,	 * so the number of method calls is greatly reduced.	 *	 * @param a an array of at least <code>count</code> integers where the result	 * wil be written starting at the first position. 	 * @param count the number of &gamma;-coded integers to be read.	 * @see #readGamma()	 */	public void readGammas( final int[] a, final int count ) throws IOException {		int preComp, msb;		for( int i = 0; i < count; i++ ) {			if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {				readBits += preComp >> 16;				fill -= preComp >> 16;				a[ i ] = preComp & 0xFFFF;				continue;			}			a[ i ] = ( ( 1 << ( msb = readUnary() ) ) | readInt( msb ) ) - 1;		}	}	/** Reads a natural number in shifted &gamma; coding.	 *	 * @return the next shifted-&gamma;&ndash;encoded natural number.	 * @see OutputBitStream#writeShiftedGamma(int)	 * @see #skipShiftedGammas(int)	 */	public int readShiftedGamma() throws IOException {		int preComp;		if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = SHIFTED_GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {			readBits += preComp >> 16;			fill -= preComp >> 16;			return preComp & 0xFFFF;		}		final int msb = readUnary() - 1;		return msb == -1 ? 0 : ( ( 1 << msb ) | readInt( msb ) );	}	/** Reads a natural number in shifted &gamma; coding.	 *	 * @return the next shifted-&gamma;&ndash;encoded natural number.	 * @see OutputBitStream#writeShiftedGamma(int)	 * @see #skipShiftedGammas(int)	 */	public long readLongShiftedGamma() throws IOException {		int preComp;		if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = SHIFTED_GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {			readBits += preComp >> 16;			fill -= preComp >> 16;			return preComp & 0xFFFF;		}		final int msb = readUnary() - 1;		return msb == -1 ? 0 : ( ( 1L << msb ) | readLong( msb ) );	}	/** Skips a given number of shited-&gamma;-coded integers.	 *	 * <p>This method should be significantly quicker than iterating <code>n</code> times on	 * {@link #readShiftedGamma()} or {@link #readLongShiftedGamma()}, as precomputed tables are used directly,	 * so the number of method calls is greatly reduced, and the result is discarded, so	 * {@link #skip(long)} can be invoked instead of more specific decoding methods.	 * 	 * @param n the number of shited-&gamma;-coded integers to be skipped.	 * @see #readShiftedGamma()	 */	public void skipShiftedGammas( int n ) throws IOException {		int preComp;		while( n-- != 0) {			if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = SHIFTED_GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] >> 16 ) != 0 ) {				readBits += preComp;				fill -= preComp;				continue;			}			final long msb = readUnary() - 1;			if ( msb > 0 ) skip( msb );		}	}	/** Reads a given number of shifted-&gamma;-coded integers.	 * 	 * <p>This method should be significantly quicker than iterating <code>n</code> times on	 * {@link #readShiftedGamma()}, as precomputed tables are used directly,	 * so the number of method calls is greatly reduced.	 *	 * @param a an array of at least <code>count</code> integers where the result	 * wil be written starting at the first position. 	 * @param count the number of shifted-&gamma;-coded integers to be read.	 * @see #readShiftedGamma()	 */	public void readShiftedGammas( final int[] a, final int count ) throws IOException {		int preComp, msb;		for( int i = 0; i < count; i++ ) {			if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = SHIFTED_GAMMA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {				readBits += preComp >> 16;				fill -= preComp >> 16;				a[ i ] = preComp & 0xFFFF;				continue;			}			msb = readUnary() - 1;			a[ i ] = msb == -1 ? 0 : ( ( 1 << msb ) | readInt( msb ) );		}	}	/** Reads a natural number in &delta; coding.	 *	 * @return the next &delta;-encoded natural number.	 * @see OutputBitStream#writeDelta(int)	 * @see #skipDeltas(int)	 */	public int readDelta() throws IOException {		int preComp;		if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = DELTA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {			readBits += preComp >> 16;			fill -= preComp >> 16;			return preComp & 0xFFFF;		}		final int msb = readGamma();		return ( ( 1 << msb ) | readInt( msb ) ) - 1;	}	/** Reads a long natural number in &delta; coding.	 *	 * @return the next &delta;-encoded long natural number.	 * @see OutputBitStream#writeDelta(int)	 * @see #skipDeltas(int)	 */	public long readLongDelta() throws IOException {		int preComp;		if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = DELTA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {			readBits += preComp >> 16;			fill -= preComp >> 16;			return preComp & 0xFFFF;		}		final int msb = readGamma();		return ( ( 1L << msb ) | readLong( msb ) ) - 1;	}	/** Skips a given number of &delta;-coded integers.	 * 	 * <p>This method should be significantly quicker than iterating <code>n</code> times on	 * {@link #readDelta()} or {@link #readLongDelta()}, as precomputed tables are used directly,	 * so the number of method calls is greatly reduced, and the result is discarded, so	 * {@link #skip(long)} can be invoked instead of more specific decoding methods.	 *	 * @param n the number of &delta;-coded integers to be skipped.	 * @see #readDelta()	 */	public void skipDeltas( int n ) throws IOException {		int preComp;		while( n-- != 0) {			if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = DELTA[ current >> ( fill - 16 ) & 0xFFFF ] >> 16 ) != 0 ) {				readBits += preComp;				fill -= preComp;				continue;			}			skip( (long)readGamma() );		}	}	/** Reads a given number of &delta;-coded integers.	 * 	 * <p>This method should be significantly quicker than iterating <code>n</code> times on	 * {@link #readDelta()}, as precomputed tables are used directly,	 * so the number of method calls is greatly reduced.	 *	 * @param a an array of at least <code>count</code> integers where the result	 * wil be written starting at the first position. 	 * @param count the number of &delta;-coded integers to be read.	 * @see #readDelta()	 */	public void readDeltas( final int[] a, final int count ) throws IOException {		int preComp, msb;		for( int i = 0; i < count; i++ ) {			if ( ( fill >= 16 || refill() >= 16 ) && ( preComp = DELTA[ current >> ( fill - 16 ) & 0xFFFF ] ) != 0 ) {				readBits += preComp >> 16;				fill -= preComp >> 16;				a[ i ] = preComp & 0xFFFF;				continue;			}			a[ i ]  = ( ( 1 << ( msb = readGamma() ) ) | readInt( msb ) ) - 1;		}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -