-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathJBBPBitOutputStream.java
470 lines (435 loc) · 14.2 KB
/
JBBPBitOutputStream.java
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Copyright 2017 Igor Maznitsa.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.igormaznitsa.jbbp.io;
import static java.util.Objects.requireNonNull;
import com.igormaznitsa.jbbp.utils.JBBPUtils;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* The Filter allows to write bit by bit into an output stream and count the written byte number. The Class is not a thread-safe one.
*
* @since 1.0
*/
public class JBBPBitOutputStream extends FilterOutputStream implements JBBPCountableBitStream {
/**
* Contains bit mode for bit operations.
*/
private final JBBPBitOrder bitOrderMode;
/**
* Internal bit buffer.
*/
private int bitBuffer;
/**
* Number of bits buffered by the bit buffer.
*/
private int bitBufferCount;
/**
* The byte counter of written bytes.
*/
private long byteCounter;
/**
* A Constructor. The Default LSB0 bit mode will be used for a bit writing operations.
*
* @param out the output stream to be filtered.
*/
public JBBPBitOutputStream(final OutputStream out) {
this(out, JBBPBitOrder.LSB0);
}
/**
* A Constructor.
*
* @param out an output stream to be filtered.
* @param bitOrderMode a bit writing mode to used for writing operations.
* @see JBBPBitOrder#LSB0
* @see JBBPBitOrder#MSB0
*/
public JBBPBitOutputStream(final OutputStream out, final JBBPBitOrder bitOrderMode) {
super(out);
this.bitOrderMode = requireNonNull(bitOrderMode, "Bit order mode must not be null");
}
/**
* Get the bit mode for writing operations.
*
* @return the bit order for reading operations.
* @see JBBPBitOrder#LSB0
* @see JBBPBitOrder#MSB0
*/
@Override
public JBBPBitOrder getBitOrder() {
return this.bitOrderMode;
}
/**
* Write a signed short value into the output stream.
*
* @param value a value to be written. Only two bytes will be written.
* @param byteOrder the byte order of the value bytes to be used for writing.
* @throws IOException it will be thrown for transport errors
* @see JBBPByteOrder#BIG_ENDIAN
* @see JBBPByteOrder#LITTLE_ENDIAN
*/
public void writeShort(final int value, final JBBPByteOrder byteOrder) throws IOException {
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
this.write(value >>> 8);
this.write(value);
} else {
this.write(value);
this.write(value >>> 8);
}
}
/**
* Write an integer value into the output stream.
*
* @param value a value to be written into the output stream.
* @param byteOrder the byte order of the value bytes to be used for writing.
* @throws IOException it will be thrown for transport errors
* @see JBBPByteOrder#BIG_ENDIAN
* @see JBBPByteOrder#LITTLE_ENDIAN
*/
public void writeInt(final int value, final JBBPByteOrder byteOrder) throws IOException {
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
this.writeShort(value >>> 16, byteOrder);
this.writeShort(value, byteOrder);
} else {
this.writeShort(value, byteOrder);
this.writeShort(value >>> 16, byteOrder);
}
}
/**
* Write an unsigned integer value into the output stream.
*
* @param value a value to be written into the output stream.
* @param byteOrder the byte order of the value bytes to be used for writing.
* @throws IOException it will be thrown for transport errors
* @see JBBPByteOrder#BIG_ENDIAN
* @see JBBPByteOrder#LITTLE_ENDIAN
* @since 2.0.4
*/
public void writeUInt(final long value, final JBBPByteOrder byteOrder) throws IOException {
final int v = (int) value;
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
this.writeShort(v >>> 16, byteOrder);
this.writeShort(v, byteOrder);
} else {
this.writeShort(v, byteOrder);
this.writeShort(v >>> 16, byteOrder);
}
}
/**
* Write a float value into the output stream.
*
* @param value a value to be written into the output stream.
* @param byteOrder the byte order of the value bytes to be used for writing.
* @throws IOException it will be thrown for transport errors
* @see JBBPByteOrder#BIG_ENDIAN
* @see JBBPByteOrder#LITTLE_ENDIAN
* @since 1.4.0
*/
public void writeFloat(final float value, final JBBPByteOrder byteOrder) throws IOException {
final int intValue = Float.floatToIntBits(value);
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
this.writeShort(intValue >>> 16, byteOrder);
this.writeShort(intValue, byteOrder);
} else {
this.writeShort(intValue, byteOrder);
this.writeShort(intValue >>> 16, byteOrder);
}
}
/**
* Write a long value into the output stream.
*
* @param value a value to be written into the output stream.
* @param byteOrder the byte order of the value bytes to be used for writing.
* @throws IOException it will be thrown for transport errors
* @see JBBPByteOrder#BIG_ENDIAN
* @see JBBPByteOrder#LITTLE_ENDIAN
*/
public void writeLong(final long value, final JBBPByteOrder byteOrder) throws IOException {
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
this.writeInt((int) (value >>> 32), byteOrder);
this.writeInt((int) value, byteOrder);
} else {
this.writeInt((int) value, byteOrder);
this.writeInt((int) (value >>> 32), byteOrder);
}
}
/**
* Write a double value into the output stream.
*
* @param value a value to be written into the output stream.
* @param byteOrder the byte order of the value bytes to be used for writing.
* @throws IOException it will be thrown for transport errors
* @see JBBPByteOrder#BIG_ENDIAN
* @see JBBPByteOrder#LITTLE_ENDIAN
* @since 1.4.0
*/
public void writeDouble(final double value, final JBBPByteOrder byteOrder) throws IOException {
final long longValue = Double.doubleToLongBits(value);
if (byteOrder == JBBPByteOrder.BIG_ENDIAN) {
this.writeInt((int) (longValue >>> 32), byteOrder);
this.writeInt((int) longValue, byteOrder);
} else {
this.writeInt((int) longValue, byteOrder);
this.writeInt((int) (longValue >>> 32), byteOrder);
}
}
/**
* Get number of bytes written into the output stream.
*
* @return the long value contains number of bytes written into the stream
*/
@Override
public long getCounter() {
return this.byteCounter;
}
/**
* Get the internal bit buffer value.
*
* @return the internal bit buffer value
*/
@Override
public int getBitBuffer() {
return this.bitBuffer;
}
/**
* Get the number of bits cached in the internal bit buffer.
*
* @return the number of cached bits in the bit buffer
*/
@Override
public int getBufferedBitsNumber() {
return this.bitBufferCount;
}
/**
* Flush the bit buffer into the output stream
*
* @throws IOException it will be thrown for transport errors
*/
private void flushBitBuffer() throws IOException {
if (this.bitBufferCount > 0) {
this.bitBufferCount = 0;
writeByte(this.bitBuffer);
}
}
@Override
public void flush() throws IOException {
flushBitBuffer();
this.out.flush();
}
@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
if (this.bitOrderMode == JBBPBitOrder.MSB0 || this.bitBufferCount != 0) {
int i = off;
int cnt = len;
while (cnt > 0) {
this.write(b[i++]);
cnt--;
}
} else {
out.write(b, off, len);
this.byteCounter += len;
}
}
@Override
public void write(final byte[] b) throws IOException {
this.write(b, 0, b.length);
}
/**
* Write bits into the output stream.
*
* @param value the value which bits will be written in the output stream
* @param bitNumber number of bits from the value to be written, must be in 1..8
* @throws IOException it will be thrown for transport errors
* @throws IllegalArgumentException it will be thrown for wrong bit number
*/
public void writeBits(final int value, final JBBPBitNumber bitNumber) throws IOException {
if (this.bitBufferCount == 0 && bitNumber == JBBPBitNumber.BITS_8) {
this.write(value);
} else {
int mask;
int accumulator = value;
int i = bitNumber.getBitNumber();
if (this.bitOrderMode == JBBPBitOrder.MSB0_DIRECT) {
final int initialMask = 0x80;
mask = initialMask >> this.bitBufferCount;
final int accumulatorMask = 1 << (bitNumber.getBitNumber() - 1);
while (i > 0) {
this.bitBuffer = this.bitBuffer | ((accumulator & accumulatorMask) == 0 ? 0 : mask);
accumulator <<= 1;
mask >>= 1;
i--;
this.bitBufferCount++;
if (this.bitBufferCount == 8) {
this.bitBufferCount = 0;
writeByte(this.bitBuffer);
mask = initialMask;
this.bitBuffer = 0;
}
}
} else {
final int initialMask = 1;
mask = initialMask << this.bitBufferCount;
while (i > 0) {
this.bitBuffer = this.bitBuffer | ((accumulator & 1) == 0 ? 0 : mask);
accumulator >>= 1;
mask = mask << 1;
i--;
this.bitBufferCount++;
if (this.bitBufferCount == 8) {
this.bitBufferCount = 0;
writeByte(this.bitBuffer);
mask = initialMask;
this.bitBuffer = 0;
}
}
}
}
}
/**
* Write padding bytes to align the stream counter for the border.
*
* @param alignByteNumber the alignment border
* @throws IOException it will be thrown for transport errors
*/
public void align(final long alignByteNumber) throws IOException {
if (this.bitBufferCount > 0) {
this.writeBits(0, JBBPBitNumber.decode(8 - this.bitBufferCount));
}
if (alignByteNumber > 0) {
long padding = (alignByteNumber - (this.byteCounter % alignByteNumber)) % alignByteNumber;
while (padding > 0) {
this.out.write(0);
this.byteCounter++;
padding--;
}
}
}
/**
* Internal method to write a byte into wrapped stream.
*
* @param value a byte value to be written
* @throws IOException it will be thrown for transport problems
*/
private void writeByte(int value) throws IOException {
if (this.bitOrderMode == JBBPBitOrder.MSB0) {
value = JBBPUtils.reverseBitsInByte((byte) value) & 0xFF;
}
this.out.write(value);
this.byteCounter++;
}
@Override
public void close() throws IOException {
this.flush();
this.out.close();
}
@Override
public void write(final int value) throws IOException {
if (this.bitBufferCount == 0) {
this.writeByte(value);
} else {
this.writeBits(value, JBBPBitNumber.BITS_8);
}
}
/**
* Write number of items from byte array into stream
*
* @param array array, must not be null
* @param length number of items to be written, if -1 then whole array
* @param byteOrder order of bytes, if LITTLE_ENDIAN then array will be reversed
* @throws IOException it will be thrown if any transport error
* @see JBBPByteOrder#LITTLE_ENDIAN
* @since 1.3.0
*/
public void writeBytes(final byte[] array, final int length, final JBBPByteOrder byteOrder)
throws IOException {
if (byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
int i = length < 0 ? array.length - 1 : length - 1;
while (i >= 0) {
this.write(array[i--]);
}
} else {
this.write(array, 0, length < 0 ? array.length : length);
}
}
/**
* Reset the byte counter for the stream. The internal bit buffer will be reset also.
*/
@Override
public void resetCounter() {
this.bitBuffer = 0;
this.bitBufferCount = 0;
this.byteCounter = 0L;
}
/**
* Write string in UTF8 format into stream.
* <b>the byte order in saved char data will be BIG_ENDIAN</b>
* Format: PREFIX(FF=null | 0=empty | 0x8packedLength) LENGTH[packedLength] DATA_ARRAY[LENGTH]
*
* @param value string to be written, can be null
* @param order order of bytes in written data (it doesn't affect encoded UTF8 array)
* @throws IOException i twill be thrown if transport error
* @since 1.4.0
*/
public void writeString(final String value, final JBBPByteOrder order) throws IOException {
if (value == null) {
this.write(0xFF);
} else if (value.isEmpty()) {
this.write(0);
} else {
final byte[] array = JBBPUtils.strToUtf8(value);
final int len = array.length;
if (len < 0x80) {
this.write(len);
} else if ((len & 0xFFFFFF00) == 0) {
this.write(0x81);
this.write(len);
} else if ((len & 0xFFFF0000) == 0) {
this.write(0x82);
this.writeShort(len, order);
} else if ((len & 0xFF000000) == 0) {
this.write(0x83);
if (order == JBBPByteOrder.BIG_ENDIAN) {
this.write(len >>> 16);
this.write(len >>> 8);
this.write(len);
} else {
this.write(len);
this.write(len >>> 8);
this.write(len >>> 16);
}
} else {
this.write(0x84);
this.writeInt(len, order);
}
this.write(array);
}
}
/**
* Write array of strings in stream in UTF8 format
* <b>the byte order in saved char data will be BIG_ENDIAN</b>
*
* @param value array to be written, must not be null but can contain null values
* @param order byte order to write char data, must not be null
* @throws IOException it will be thrown for transport errors
* @see #writeString(String, JBBPByteOrder)
* @since 1.4.0
*/
public void writeStringArray(final String[] value, final JBBPByteOrder order) throws IOException {
for (final String s : value) {
this.writeString(s, order);
}
}
}