|
| 1 | +package org.json.zip; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | + |
| 6 | +/* |
| 7 | + Copyright (c) 2013 JSON.org |
| 8 | +
|
| 9 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + of this software and associated documentation files (the "Software"), to deal |
| 11 | + in the Software without restriction, including without limitation the rights |
| 12 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + copies of the Software, and to permit persons to whom the Software is |
| 14 | + furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | + The above copyright notice and this permission notice shall be included in all |
| 17 | + copies or substantial portions of the Software. |
| 18 | +
|
| 19 | + The Software shall be used for Good, not Evil. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | + SOFTWARE. |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * This is a big endian bit reader. It reads its bits from an InputStream. |
| 32 | + * |
| 33 | + * @version 2013-04-18 |
| 34 | + * |
| 35 | + */ |
| 36 | +public class BitInputStream implements BitReader { |
| 37 | + /** |
| 38 | + * 2^n - 1 |
| 39 | + */ |
| 40 | + static final int[] mask = { 0, 1, 3, 7, 15, 31, 63, 127, 255 }; |
| 41 | + |
| 42 | + /** |
| 43 | + * The number of bits remaining in the current byte. |
| 44 | + */ |
| 45 | + private int available = 0; |
| 46 | + |
| 47 | + /** |
| 48 | + * Up to a byte's worth of unread bits. |
| 49 | + */ |
| 50 | + private int unread = 0; |
| 51 | + |
| 52 | + /** |
| 53 | + * The source of the bits. |
| 54 | + */ |
| 55 | + private InputStream in; |
| 56 | + |
| 57 | + /** |
| 58 | + * The number of bits read so far. This is used in padding. |
| 59 | + */ |
| 60 | + private long nrBits = 0; |
| 61 | + |
| 62 | + /** |
| 63 | + * Make a BitReader from an InputStream. The BitReader will take bytes from |
| 64 | + * the InputStream and unpack them into bits. |
| 65 | + * |
| 66 | + * @param in |
| 67 | + * An InputStream. |
| 68 | + */ |
| 69 | + public BitInputStream(InputStream in) { |
| 70 | + this.in = in; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Make a BitReader. The first byte is passed in explicitly, the remaining |
| 75 | + * bytes are obtained from the InputStream. This makes it possible to look |
| 76 | + * at the first byte of a stream before deciding that it should be read as |
| 77 | + * bits. |
| 78 | + * |
| 79 | + * @param in |
| 80 | + * An InputStream |
| 81 | + * @param firstByte |
| 82 | + * The first byte, which was probably read from in. |
| 83 | + */ |
| 84 | + public BitInputStream(InputStream in, int firstByte) { |
| 85 | + this.in = in; |
| 86 | + this.unread = firstByte; |
| 87 | + this.available = 8; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Read one bit. |
| 92 | + * |
| 93 | + * @return true if it is a 1 bit. |
| 94 | + */ |
| 95 | + public boolean bit() throws IOException { |
| 96 | + return read(1) != 0; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the number of bits that have been read from this BitInputStream. |
| 101 | + * This includes pad bits that have been skipped, but might not include |
| 102 | + * bytes that have been read from the underlying InputStream that have not |
| 103 | + * yet been delivered as bits. |
| 104 | + * |
| 105 | + * @return The number of bits read so far. |
| 106 | + */ |
| 107 | + public long nrBits() { |
| 108 | + return this.nrBits; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Check that the rest of the block has been padded with zeroes. |
| 113 | + * |
| 114 | + * @param factor |
| 115 | + * The size of the block to pad. This will typically be 8, 16, |
| 116 | + * 32, 64, 128, 256, etc. |
| 117 | + * @return true if the block was zero padded, or false if the the padding |
| 118 | + * contains any one bits. |
| 119 | + * @throws IOException |
| 120 | + */ |
| 121 | + public boolean pad(int factor) throws IOException { |
| 122 | + int padding = factor - (int) (this.nrBits % factor); |
| 123 | + boolean result = true; |
| 124 | + |
| 125 | + for (int i = 0; i < padding; i += 1) { |
| 126 | + if (bit()) { |
| 127 | + result = false; |
| 128 | + } |
| 129 | + } |
| 130 | + return result; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Read some bits. |
| 135 | + * |
| 136 | + * @param width |
| 137 | + * The number of bits to read. (0..32) |
| 138 | + * @throws IOException |
| 139 | + * @return the bits |
| 140 | + */ |
| 141 | + public int read(int width) throws IOException { |
| 142 | + if (width == 0) { |
| 143 | + return 0; |
| 144 | + } |
| 145 | + if (width < 0 || width > 32) { |
| 146 | + throw new IOException("Bad read width."); |
| 147 | + } |
| 148 | + int result = 0; |
| 149 | + while (width > 0) { |
| 150 | + if (this.available == 0) { |
| 151 | + this.unread = this.in.read(); |
| 152 | + if (this.unread < 0) { |
| 153 | + throw new IOException("Attempt to read past end."); |
| 154 | + } |
| 155 | + this.available = 8; |
| 156 | + } |
| 157 | + int take = width; |
| 158 | + if (take > this.available) { |
| 159 | + take = this.available; |
| 160 | + } |
| 161 | + result |= ((this.unread >>> (this.available - take)) & mask[take]) |
| 162 | + << (width - take); |
| 163 | + this.nrBits += take; |
| 164 | + this.available -= take; |
| 165 | + width -= take; |
| 166 | + } |
| 167 | + return result; |
| 168 | + } |
| 169 | +} |
0 commit comments