1 package nom.tam.fits.compression.algorithm.rice;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 import java.nio.ByteBuffer;
35
36
37
38
39
40
41
42
43 @Deprecated
44 @SuppressWarnings("javadoc")
45 public class BitBuffer {
46
47 private static final int BITS_OF_4_BYTES = 32;
48
49 private static final int BYTE_MASK = 0xFF;
50
51 private static final long INTEGER_MASK = 0xFFFFFFFFL;
52
53 private static final int BITS_OF_1_BYTE = 8;
54
55 private static final int BITS_OF_2_BYTES = 16;
56
57 private static final int BITS_OF_3_BYTES = 24;
58
59 private static final int BYTE_1_OF_INT = 0x000000FF;
60
61 private static final int BYTE_2_OF_INT = 0x0000FF00;
62
63 private static final int BYTE_3_OF_INT = 0x00FF0000;
64
65 private static final int BYTE_4_OF_INT = 0xFF000000;
66
67 private final ByteBuffer buffer;
68
69 private long position;
70
71 @Deprecated
72 public BitBuffer(ByteBuffer writeBuffer) {
73 buffer = writeBuffer;
74 }
75
76 @Deprecated
77 public int bitbuffer() {
78 return buffer.get((int) (position / BITS_OF_1_BYTE));
79 }
80
81 @Deprecated
82 void close() {
83 if (position % BITS_OF_1_BYTE != 0) {
84 putByte((byte) 0, (int) (BITS_OF_1_BYTE - position % BITS_OF_1_BYTE));
85 }
86 buffer.position((int) (position / BITS_OF_1_BYTE));
87 }
88
89 @Deprecated
90 public int missingBitsInCurrentByte() {
91 return (int) (BITS_OF_1_BYTE - position % BITS_OF_1_BYTE);
92 }
93
94 @Deprecated
95 public void movePosition(int i) {
96 position += i;
97 }
98
99 @Deprecated
100 public void putByte(byte byteToAdd) {
101 final int bytePosition = (int) (position / BITS_OF_1_BYTE);
102 final int positionInByte = (int) (position % BITS_OF_1_BYTE);
103 final byte old = (byte) (buffer.get(bytePosition) & (byte) ~(BYTE_MASK >>> positionInByte));
104 final int byteAsInt = byteToAdd & BYTE_MASK;
105 buffer.put(bytePosition, (byte) (old | (byte) (byteAsInt >>> positionInByte)));
106 if (positionInByte > 0) {
107 buffer.put(bytePosition + 1, (byte) (byteAsInt << BITS_OF_1_BYTE - positionInByte));
108 }
109 position += BITS_OF_1_BYTE;
110 }
111
112 @Deprecated
113 public void putByte(byte byteToAdd, int bits) {
114 final int bytePosition = (int) (position / BITS_OF_1_BYTE);
115 final int positionInByte = (int) (position % BITS_OF_1_BYTE);
116 final byte old = buffer.get(bytePosition);
117 final int byteAsInt = BYTE_MASK & (byteToAdd & BYTE_MASK >>> BITS_OF_1_BYTE - bits) << BITS_OF_1_BYTE - bits;
118 buffer.put(bytePosition, (byte) (BYTE_MASK &
119 (old & BYTE_MASK << BITS_OF_1_BYTE - positionInByte | byteAsInt >>> positionInByte)));
120 if (BITS_OF_1_BYTE - positionInByte < bits) {
121 buffer.put(bytePosition + 1, (byte) (BYTE_MASK & byteAsInt << BITS_OF_1_BYTE - positionInByte));
122 }
123 position += bits;
124 }
125
126
127
128
129
130
131 @Deprecated
132 public void putInt(int i) {
133 putByte((byte) ((i & BYTE_4_OF_INT) >>> BITS_OF_3_BYTES));
134 putByte((byte) ((i & BYTE_3_OF_INT) >>> BITS_OF_2_BYTES));
135 putByte((byte) ((i & BYTE_2_OF_INT) >>> BITS_OF_1_BYTE));
136 putByte((byte) (i & BYTE_1_OF_INT));
137 }
138
139 @Deprecated
140 public void putInt(int i, int bits) {
141 if (bits == 0) {
142 return;
143 }
144 do {
145 if (bits >= BITS_OF_1_BYTE) {
146 putByte((byte) ((i & BYTE_MASK << bits - BITS_OF_1_BYTE) >>> bits - BITS_OF_1_BYTE & BYTE_MASK));
147 bits -= BITS_OF_1_BYTE;
148 } else {
149 putByte((byte) (i & BYTE_MASK >> -(bits - BITS_OF_1_BYTE)), bits);
150 bits = 0;
151 }
152 } while (bits > 0);
153 }
154
155 @Deprecated
156 public void putLong(long l, int bits) {
157 if (bits == 0) {
158 return;
159 }
160 do {
161 if (bits >= BITS_OF_4_BYTES) {
162 putInt((int) ((l & INTEGER_MASK << bits - BITS_OF_4_BYTES) >>> bits - BITS_OF_4_BYTES));
163 bits -= BITS_OF_4_BYTES;
164 } else {
165 putInt((int) (l & INTEGER_MASK >> -(bits - BITS_OF_4_BYTES)), bits);
166 bits = 0;
167 }
168 } while (bits > 0);
169 }
170
171 }