|
1 | | -package com.ebremer.beakgraph.HDTish; |
2 | | - |
3 | | -import java.io.ByteArrayOutputStream; |
4 | | -import java.io.IOException; |
5 | | -import java.nio.ByteBuffer; |
6 | | -import java.nio.file.Path; |
7 | | -import java.util.HashMap; |
8 | | -import java.util.Map; |
9 | | - |
10 | | -interface ByteWriter { |
11 | | - void writeByte(byte b) throws IOException; |
12 | | -} |
13 | | - |
14 | | -public class BitPackedWriter implements HDF5Buffer, AutoCloseable { |
15 | | - private final ByteWriter byteWriter; |
16 | | - private final long width; |
17 | | - private long bitBuffer = 0; |
18 | | - private int bitCount = 0; |
19 | | - private ByteArrayOutputStream os; |
20 | | - private Path path; |
21 | | - private long entries = 0; |
22 | | - |
23 | | - private BitPackedWriter(Path path, ByteWriter byteWriter, long width, ByteArrayOutputStream os) { |
24 | | - this.path = path; |
25 | | - this.byteWriter = byteWriter; |
26 | | - this.width = width; |
27 | | - this.os = os; |
28 | | - } |
29 | | - |
30 | | - @Override |
31 | | - public Map<String, Object> getProperties() { |
32 | | - Map<String,Object> meta = new HashMap<>(); |
33 | | - meta.put("width", width); |
34 | | - meta.put("numEntries", entries); |
35 | | - return meta; |
36 | | - } |
37 | | - |
38 | | - @Override |
39 | | - public Path getName() { |
40 | | - return path; |
41 | | - } |
42 | | - |
43 | | - @Override |
44 | | - public byte[] getBuffer() { |
45 | | - return os.toByteArray(); |
46 | | - } |
47 | | - |
48 | | - // Write the least significant n bits of the integer to the buffer |
49 | | - public void writeInteger(int value) throws IOException { |
50 | | - //if (width < 0 || width > 32) { |
51 | | - // throw new IllegalArgumentException("n must be between 0 and 32 ---> "+value); |
52 | | - // } |
53 | | - entries++; |
54 | | - // Extract the least significant n bits |
55 | | - long bits = value & ((1L << width) - 1); |
56 | | - // Shift existing bits left and append new bits |
57 | | - bitBuffer = (bitBuffer << width) | bits; |
58 | | - bitCount += width; |
59 | | - // Write full bytes when possible |
60 | | - while (bitCount >= 8) { |
61 | | - int shift = bitCount - 8; |
62 | | - byte b = (byte) (bitBuffer >>> shift); |
63 | | - byteWriter.writeByte(b); |
64 | | - // Keep the remaining bits |
65 | | - bitBuffer = bitBuffer & ((1L << shift) - 1); |
66 | | - bitCount -= 8; |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - public void writeLong(long value) throws IOException { |
71 | | - //if (width < 0 || width > 64) { |
72 | | - // throw new IllegalArgumentException("width must be between 0 and 64"); |
73 | | - // } |
74 | | - entries++; |
75 | | - long bits = value & ((1L << width) - 1); |
76 | | - bitBuffer = (bitBuffer << width) | bits; |
77 | | - bitCount += width; |
78 | | - while (bitCount >= 8) { |
79 | | - int shift = bitCount - 8; |
80 | | - byte b = (byte) (bitBuffer >>> shift); |
81 | | - byteWriter.writeByte(b); |
82 | | - bitBuffer = bitBuffer & ((1L << shift) - 1); |
83 | | - bitCount -= 8; |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - @Override |
88 | | - public void close() throws IOException { |
89 | | - if (bitCount > 0) { |
90 | | - // Pad the remaining bits with zeros on the right |
91 | | - byte b = (byte) (bitBuffer << (8 - bitCount)); |
92 | | - byteWriter.writeByte(b); |
93 | | - } |
94 | | - os.flush(); |
95 | | - os.close(); |
96 | | - } |
97 | | - |
98 | | - public static BitPackedWriter forBuffer(Path path, long width) throws IOException { |
99 | | - if (width < 0 || width > 64) { |
100 | | - throw new IllegalArgumentException("n must be between 0 and 64 ---> "+width); |
101 | | - } |
102 | | - System.out.println(path+" ---> "+width); |
103 | | - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
104 | | - ByteWriter fileWriter = new ByteWriter() { |
105 | | - @Override |
106 | | - public void writeByte(byte b) throws IOException { |
107 | | - buffer.write(b); |
108 | | - } |
109 | | - }; |
110 | | - return new BitPackedWriter(path, fileWriter, width, buffer); |
111 | | - } |
112 | | - |
113 | | - /* |
114 | | - public static BitPackedWriter forFile(Path file, int width) throws IOException { |
115 | | - BufferedOutputStream fos = new BufferedOutputStream( new FileOutputStream(file.toFile())); |
116 | | - ByteWriter fileWriter = new ByteWriter() { |
117 | | - @Override |
118 | | - public void writeByte(byte b) throws IOException { |
119 | | - fos.write(b); |
120 | | - } |
121 | | - }; |
122 | | - return new BitPackedWriter(fileWriter, width, fos); |
123 | | - }*/ |
124 | | - |
125 | | - public static String toBinaryString(ByteBuffer buffer, String delimiter) { |
126 | | - buffer.mark(); |
127 | | - StringBuilder binary = new StringBuilder(); |
128 | | - while (buffer.hasRemaining()) { |
129 | | - byte b = buffer.get(); |
130 | | - String binaryByte = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
131 | | - binary.append(binaryByte); |
132 | | - if (buffer.hasRemaining()) { |
133 | | - binary.append(delimiter); |
134 | | - } |
135 | | - } |
136 | | - buffer.reset(); |
137 | | - return binary.toString(); |
138 | | - } |
139 | | - |
140 | | - /* |
141 | | - public static BitPackedWriter forByteBuffer(ByteBuffer buffer, int width) { |
142 | | - ByteWriter bufferWriter = new ByteWriter() { |
143 | | - @Override |
144 | | - public void writeByte(byte b) throws IOException { |
145 | | - if (!buffer.hasRemaining()) { |
146 | | - throw new IOException("ByteBuffer capacity exceeded"); |
147 | | - } |
148 | | - buffer.put(b); |
149 | | - } |
150 | | - }; |
151 | | - return new BitPackedWriter(bufferWriter, width, fos); |
152 | | - }*/ |
153 | | - |
154 | | - /* |
155 | | - public static void main(String[] args) throws IOException { |
156 | | - BitPackedWriter fileWriter = BitPackedWriter.forFile(new File("output.dat"), 3); |
157 | | - fileWriter.writeInteger(5); // 101 |
158 | | - fileWriter.writeInteger(3); // 011 |
159 | | - fileWriter.writeInteger(7); // 111 |
160 | | - fileWriter.writeInteger(2); // 111 |
161 | | - fileWriter.writeInteger(1); // 111 |
162 | | - fileWriter.writeInteger(1); // 111 |
163 | | - fileWriter.writeInteger(4); // 111 |
164 | | - fileWriter.close(); |
165 | | -
|
166 | | - |
167 | | - ByteBuffer buffer = ByteBuffer.allocate(10); |
168 | | - BitPackedWriter bufferWriter = BitPackedWriter.forByteBuffer(buffer,3); |
169 | | - bufferWriter.writeInteger(5); |
170 | | - bufferWriter.writeInteger(3); |
171 | | - bufferWriter.writeInteger(7); |
172 | | - bufferWriter.close(); |
173 | | -
|
174 | | - buffer.rewind(); |
175 | | - System.out.println(toBinaryString(buffer, " ")); |
176 | | - }*/ |
177 | | -} |
| 1 | +package com.ebremer.beakgraph.HDTish; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +interface ByteWriter { |
| 11 | + void writeByte(byte b) throws IOException; |
| 12 | +} |
| 13 | + |
| 14 | +public class BitPackedWriter implements HDF5Buffer, AutoCloseable { |
| 15 | + private final ByteWriter byteWriter; |
| 16 | + private final long width; |
| 17 | + private long bitBuffer = 0; |
| 18 | + private int bitCount = 0; |
| 19 | + private ByteArrayOutputStream os; |
| 20 | + private Path path; |
| 21 | + private long entries = 0; |
| 22 | + |
| 23 | + private BitPackedWriter(Path path, ByteWriter byteWriter, long width, ByteArrayOutputStream os) { |
| 24 | + this.path = path; |
| 25 | + this.byteWriter = byteWriter; |
| 26 | + this.width = width; |
| 27 | + this.os = os; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Map<String, Object> getProperties() { |
| 32 | + Map<String,Object> meta = new HashMap<>(); |
| 33 | + meta.put("width", width); |
| 34 | + meta.put("numEntries", entries); |
| 35 | + return meta; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Path getName() { |
| 40 | + return path; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public byte[] getBuffer() { |
| 45 | + return os.toByteArray(); |
| 46 | + } |
| 47 | + |
| 48 | + // Write the least significant n bits of the integer to the buffer |
| 49 | + public void writeInteger(int value) throws IOException { |
| 50 | + //if (width < 0 || width > 32) { |
| 51 | + // throw new IllegalArgumentException("n must be between 0 and 32 ---> "+value); |
| 52 | + // } |
| 53 | + entries++; |
| 54 | + // Extract the least significant n bits |
| 55 | + long bits = value & ((1L << width) - 1); |
| 56 | + // Shift existing bits left and append new bits |
| 57 | + bitBuffer = (bitBuffer << width) | bits; |
| 58 | + bitCount += width; |
| 59 | + // Write full bytes when possible |
| 60 | + while (bitCount >= 8) { |
| 61 | + int shift = bitCount - 8; |
| 62 | + byte b = (byte) (bitBuffer >>> shift); |
| 63 | + byteWriter.writeByte(b); |
| 64 | + // Keep the remaining bits |
| 65 | + bitBuffer = bitBuffer & ((1L << shift) - 1); |
| 66 | + bitCount -= 8; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void writeLong(long value) throws IOException { |
| 71 | + //if (width < 0 || width > 64) { |
| 72 | + // throw new IllegalArgumentException("width must be between 0 and 64"); |
| 73 | + // } |
| 74 | + entries++; |
| 75 | + long bits = value & ((1L << width) - 1); |
| 76 | + bitBuffer = (bitBuffer << width) | bits; |
| 77 | + bitCount += width; |
| 78 | + while (bitCount >= 8) { |
| 79 | + int shift = bitCount - 8; |
| 80 | + byte b = (byte) (bitBuffer >>> shift); |
| 81 | + byteWriter.writeByte(b); |
| 82 | + bitBuffer = bitBuffer & ((1L << shift) - 1); |
| 83 | + bitCount -= 8; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void close() throws IOException { |
| 89 | + if (bitCount > 0) { |
| 90 | + // Pad the remaining bits with zeros on the right |
| 91 | + byte b = (byte) (bitBuffer << (8 - bitCount)); |
| 92 | + byteWriter.writeByte(b); |
| 93 | + } |
| 94 | + os.flush(); |
| 95 | + os.close(); |
| 96 | + } |
| 97 | + |
| 98 | + public static BitPackedWriter forBuffer(Path path, long width) throws IOException { |
| 99 | + if (width < 0 || width > 64) { |
| 100 | + throw new IllegalArgumentException("n must be between 0 and 64 ---> "+width); |
| 101 | + } |
| 102 | + System.out.println(path+" ---> "+width); |
| 103 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 104 | + ByteWriter fileWriter = new ByteWriter() { |
| 105 | + @Override |
| 106 | + public void writeByte(byte b) throws IOException { |
| 107 | + buffer.write(b); |
| 108 | + } |
| 109 | + }; |
| 110 | + return new BitPackedWriter(path, fileWriter, width, buffer); |
| 111 | + } |
| 112 | + |
| 113 | + /* |
| 114 | + public static BitPackedWriter forFile(Path file, int width) throws IOException { |
| 115 | + BufferedOutputStream fos = new BufferedOutputStream( new FileOutputStream(file.toFile())); |
| 116 | + ByteWriter fileWriter = new ByteWriter() { |
| 117 | + @Override |
| 118 | + public void writeByte(byte b) throws IOException { |
| 119 | + fos.write(b); |
| 120 | + } |
| 121 | + }; |
| 122 | + return new BitPackedWriter(fileWriter, width, fos); |
| 123 | + }*/ |
| 124 | + |
| 125 | + public static String toBinaryString(ByteBuffer buffer, String delimiter) { |
| 126 | + buffer.mark(); |
| 127 | + StringBuilder binary = new StringBuilder(); |
| 128 | + while (buffer.hasRemaining()) { |
| 129 | + byte b = buffer.get(); |
| 130 | + String binaryByte = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 131 | + binary.append(binaryByte); |
| 132 | + if (buffer.hasRemaining()) { |
| 133 | + binary.append(delimiter); |
| 134 | + } |
| 135 | + } |
| 136 | + buffer.reset(); |
| 137 | + return binary.toString(); |
| 138 | + } |
| 139 | + |
| 140 | + /* |
| 141 | + public static BitPackedWriter forByteBuffer(ByteBuffer buffer, int width) { |
| 142 | + ByteWriter bufferWriter = new ByteWriter() { |
| 143 | + @Override |
| 144 | + public void writeByte(byte b) throws IOException { |
| 145 | + if (!buffer.hasRemaining()) { |
| 146 | + throw new IOException("ByteBuffer capacity exceeded"); |
| 147 | + } |
| 148 | + buffer.put(b); |
| 149 | + } |
| 150 | + }; |
| 151 | + return new BitPackedWriter(bufferWriter, width, fos); |
| 152 | + }*/ |
| 153 | + |
| 154 | + /* |
| 155 | + public static void main(String[] args) throws IOException { |
| 156 | + BitPackedWriter fileWriter = BitPackedWriter.forFile(new File("output.dat"), 3); |
| 157 | + fileWriter.writeInteger(5); // 101 |
| 158 | + fileWriter.writeInteger(3); // 011 |
| 159 | + fileWriter.writeInteger(7); // 111 |
| 160 | + fileWriter.writeInteger(2); // 111 |
| 161 | + fileWriter.writeInteger(1); // 111 |
| 162 | + fileWriter.writeInteger(1); // 111 |
| 163 | + fileWriter.writeInteger(4); // 111 |
| 164 | + fileWriter.close(); |
| 165 | +
|
| 166 | + |
| 167 | + ByteBuffer buffer = ByteBuffer.allocate(10); |
| 168 | + BitPackedWriter bufferWriter = BitPackedWriter.forByteBuffer(buffer,3); |
| 169 | + bufferWriter.writeInteger(5); |
| 170 | + bufferWriter.writeInteger(3); |
| 171 | + bufferWriter.writeInteger(7); |
| 172 | + bufferWriter.close(); |
| 173 | +
|
| 174 | + buffer.rewind(); |
| 175 | + System.out.println(toBinaryString(buffer, " ")); |
| 176 | + }*/ |
| 177 | +} |
0 commit comments