/*
* $ProjectName$
* $ProjectRevision$
* -----------------------------------------------------------
* $Id$
* -----------------------------------------------------------
*
* $Author$
*
* Description:
*
* Copyright 2002-2003 Tor-Einar Jarnbjo
* -----------------------------------------------------------
*
* Change History
* -----------------------------------------------------------
* $Log$
* Revision 1.1 2005/07/11 15:42:36 hcl
* Songdb java version, source. only 1.5 compatible
*
* Revision 1.1.1.1 2004/04/04 22:09:12 shred
* First Import
*
* Revision 1.3 2003/04/10 19:48:31 jarnbjo
* no message
*
* Revision 1.2 2003/03/16 01:11:39 jarnbjo
* no message
*
* Revision 1.1 2003/03/03 21:02:20 jarnbjo
* no message
*
*/
package de.jarnbjo.util.io;
import java.io.IOException;
/**
* Implementation of the BitInputStream
interface,
* using a byte array as data source.
*/
public class ByteArrayBitInputStream implements BitInputStream {
private byte[] source;
private byte currentByte;
private int endian;
private int byteIndex=0;
private int bitIndex=0;
public ByteArrayBitInputStream(byte[] source) {
this(source, LITTLE_ENDIAN);
}
public ByteArrayBitInputStream(byte[] source, int endian) {
this.endian=endian;
this.source=source;
currentByte=source[0];
bitIndex=(endian==LITTLE_ENDIAN)?0:7;
}
public boolean getBit() throws IOException {
if(endian==LITTLE_ENDIAN) {
if(bitIndex>7) {
bitIndex=0;
currentByte=source[++byteIndex];
}
return (currentByte&(1<<(bitIndex++)))!=0;
}
else {
if(bitIndex<0) {
bitIndex=7;
currentByte=source[++byteIndex];
}
return (currentByte&(1<<(bitIndex--)))!=0;
}
}
public int getInt(int bits) throws IOException {
if(bits>32) {
throw new IllegalArgumentException("Argument \"bits\" must be <= 32");
}
int res=0;
if(endian==LITTLE_ENDIAN) {
for(int i=0; i
not supported for little endian
* * @param order * @return the decoded integer value read from the stream * * @throws IOException if an I/O error occurs * @throws UnsupportedOperationException if the method is not supported by the implementation */ public int readSignedRice(int order) throws IOException { int msbs=-1, lsbs=0, res=0; if(endian==LITTLE_ENDIAN) { // little endian throw new UnsupportedOperationException("ByteArrayBitInputStream.readSignedRice() is only supported in big endian mode"); } else { // big endian byte cb=source[byteIndex]; do { msbs++; if(bitIndex<0) { bitIndex=7; byteIndex++; cb=source[byteIndex]; } } while((cb&(1<fills the array from offset
with len
* integers encoded as "signed rice" as described in
* the FLAC audio format specification
not supported for little endian
* * @param order * @param buffer * @param offset * @param len * @return the decoded integer value read from the stream * * @throws IOException if an I/O error occurs * @throws UnsupportedOperationException if the method is not supported by the implementation */ public void readSignedRice(int order, int[] buffer, int off, int len) throws IOException { if(endian==LITTLE_ENDIAN) { // little endian throw new UnsupportedOperationException("ByteArrayBitInputStream.readSignedRice() is only supported in big endian mode"); } else { // big endian for(int i=off; i