View Javadoc
1   package nom.tam.fits.compression.algorithm.rice;
2   
3   /*
4    * #%L
5    * nom.tam FITS library
6    * %%
7    * Copyright (C) 1996 - 2024 nom-tam-fits
8    * %%
9    * This is free and unencumbered software released into the public domain.
10   *
11   * Anyone is free to copy, modify, publish, use, compile, sell, or
12   * distribute this software, either in source code form or as a compiled
13   * binary, for any purpose, commercial or non-commercial, and by any
14   * means.
15   *
16   * In jurisdictions that recognize copyright laws, the author or authors
17   * of this software dedicate any and all copyright interest in the
18   * software to the public domain. We make this dedication for the benefit
19   * of the public at large and to the detriment of our heirs and
20   * successors. We intend this dedication to be an overt act of
21   * relinquishment in perpetuity of all present and future rights to this
22   * software under copyright law.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30   * OTHER DEALINGS IN THE SOFTWARE.
31   * #L%
32   */
33  
34  import java.nio.ByteBuffer;
35  
36  /**
37   * (<i>for internal use</i>) A bit wise reader writer around a {@link ByteBuffer}.
38   * 
39   * @deprecated (<i>for internal use</i>) Its visibility may be reduced to the package level in the future.
40   * 
41   * @author     Ritchie
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      * write out int value to the next 4 bytes of the buffer
128      * 
129      * @param i integer to write
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 }