| 1 |
// Copyright 2001-2003, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.IOException; |
| 5 |
import java.io.OutputStream; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class to write bits to a Stream, allowing for byte synchronization. Signed, |
| 9 |
* Unsigned, Booleans and Floats can be written. |
| 10 |
* |
| 11 |
* @author Mark Donszelmann |
| 12 |
* @author Charles Loomis |
| 13 |
* @version $Id: BitOutputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 14 |
*/ |
| 15 |
public class BitOutputStream extends CompressableOutputStream implements |
| 16 |
FinishableOutputStream { |
| 17 |
|
| 18 |
private int bits; |
| 19 |
|
| 20 |
private int bitPos; |
| 21 |
|
| 22 |
/** |
| 23 |
* Create a Bit output stream from given stream |
| 24 |
* |
| 25 |
* @param out stream to write to |
| 26 |
*/ |
| 27 |
public BitOutputStream(OutputStream out) { |
| 28 |
super(out); |
| 29 |
|
| 30 |
bits = 0; |
| 31 |
bitPos = 0; |
| 32 |
} |
| 33 |
|
| 34 |
public void write(int b) throws IOException { |
| 35 |
// System.out.println(Integer.toHexString(b)); |
| 36 |
super.write(b); |
| 37 |
} |
| 38 |
|
| 39 |
public void finish() throws IOException { |
| 40 |
|
| 41 |
flushByte(); |
| 42 |
if (out instanceof FinishableOutputStream) { |
| 43 |
((FinishableOutputStream) out).finish(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public void close() throws IOException { |
| 48 |
|
| 49 |
finish(); |
| 50 |
super.close(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* A utility method to flush the next byte |
| 55 |
* |
| 56 |
* @throws IOException if write fails |
| 57 |
*/ |
| 58 |
protected void flushByte() throws IOException { |
| 59 |
|
| 60 |
if (bitPos == 0) |
| 61 |
return; |
| 62 |
|
| 63 |
write(bits); |
| 64 |
bits = 0; |
| 65 |
bitPos = 0; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* A utility to force the next write to be byte-aligned. |
| 70 |
* |
| 71 |
* @throws IOException if write fails |
| 72 |
*/ |
| 73 |
public void byteAlign() throws IOException { |
| 74 |
flushByte(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Write a bit to the output stream. A 1-bit is true; a 0-bit is false. |
| 79 |
* |
| 80 |
* @param bit value to write |
| 81 |
* @throws IOException if write fails |
| 82 |
*/ |
| 83 |
public void writeBitFlag(boolean bit) throws IOException { |
| 84 |
|
| 85 |
writeUBits((bit) ? 1 : 0, 1); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Write a signed value of n-bits to the output stream. |
| 90 |
* |
| 91 |
* @param value value to write |
| 92 |
* @param n number of bits to write |
| 93 |
* @throws IOException if write fails |
| 94 |
*/ |
| 95 |
public void writeSBits(long value, int n) throws IOException { |
| 96 |
|
| 97 |
long tmp = value & 0x7FFFFFFF; |
| 98 |
|
| 99 |
if (value < 0) { |
| 100 |
tmp |= (1L << (n - 1)); |
| 101 |
} |
| 102 |
|
| 103 |
writeUBits(tmp, n); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Write a float value of n-bits to the stream. |
| 108 |
* |
| 109 |
* @param value value to write |
| 110 |
* @param n number of bits to write |
| 111 |
* @throws IOException if write fails |
| 112 |
*/ |
| 113 |
public void writeFBits(float value, int n) throws IOException { |
| 114 |
|
| 115 |
if (n == 0) |
| 116 |
return; |
| 117 |
long tmp = (long) (value * 0x10000); |
| 118 |
writeSBits(tmp, n); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Write an unsigned value of n-bits to the output stream. |
| 123 |
* |
| 124 |
* @param value value to write |
| 125 |
* @param n number of bits to write |
| 126 |
* @throws IOException if write fails |
| 127 |
*/ |
| 128 |
public void writeUBits(long value, int n) throws IOException { |
| 129 |
|
| 130 |
if (n == 0) |
| 131 |
return; |
| 132 |
if (bitPos == 0) |
| 133 |
bitPos = 8; |
| 134 |
|
| 135 |
int bitNum = n; |
| 136 |
|
| 137 |
while (bitNum > 0) { |
| 138 |
while ((bitPos > 0) && (bitNum > 0)) { |
| 139 |
long or = (value & (1L << (bitNum - 1))); |
| 140 |
int shift = bitPos - bitNum; |
| 141 |
if (shift < 0) { |
| 142 |
or >>= -shift; |
| 143 |
} else { |
| 144 |
or <<= shift; |
| 145 |
} |
| 146 |
bits |= or; |
| 147 |
|
| 148 |
bitNum--; |
| 149 |
bitPos--; |
| 150 |
} |
| 151 |
|
| 152 |
if (bitPos == 0) { |
| 153 |
write(bits); |
| 154 |
bits = 0; |
| 155 |
if (bitNum > 0) |
| 156 |
bitPos = 8; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* calculates the minumum number of bits necessary to write number. |
| 163 |
* |
| 164 |
* @param number number |
| 165 |
* @return minimum number of bits to store number |
| 166 |
*/ |
| 167 |
public static int minBits(float number) { |
| 168 |
return minBits((int) number, true) + 16; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @param number value to calculate bits for |
| 173 |
* @return number of bits needed to store value |
| 174 |
*/ |
| 175 |
public static int minBits(long number) { |
| 176 |
return minBits(number, number < 0); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @param number value to calculate bits for |
| 181 |
* @param signed true if the value if signed (< 0) |
| 182 |
* @return number of bits needed to store value |
| 183 |
*/ |
| 184 |
public static int minBits(long number, boolean signed) { |
| 185 |
number = Math.abs(number); |
| 186 |
|
| 187 |
long x = 1; |
| 188 |
int i; |
| 189 |
|
| 190 |
for (i = 1; i <= 64; i++) { |
| 191 |
x <<= 1; |
| 192 |
if (x > number) |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
return i + ((signed) ? 1 : 0); |
| 197 |
} |
| 198 |
} |