|
26 | 26 | package org.janelia.saalfeldlab.n5.blosc; |
27 | 27 |
|
28 | 28 | import java.io.IOException; |
29 | | -import java.io.InputStream; |
30 | | -import java.io.OutputStream; |
31 | 29 | import java.nio.ByteBuffer; |
32 | | - |
33 | | -import org.apache.commons.compress.utils.IOUtils; |
34 | 30 | import org.blosc.BufferSizes; |
35 | 31 | import org.blosc.JBlosc; |
36 | 32 | import org.janelia.saalfeldlab.n5.Compression; |
37 | 33 | import org.janelia.saalfeldlab.n5.Compression.CompressionType; |
38 | | -import org.janelia.saalfeldlab.n5.DataBlock; |
39 | | -import org.janelia.saalfeldlab.n5.DefaultBlockReader; |
40 | | -import org.janelia.saalfeldlab.n5.DefaultBlockWriter; |
| 34 | +import org.janelia.saalfeldlab.n5.readdata.ReadData; |
41 | 35 |
|
42 | 36 | /** |
43 | 37 | * Compression using JBlosc (https://github.com/Blosc/JBlosc) compressors. |
44 | 38 | * |
45 | 39 | * @author Stephan Saalfeld <saalfelds@janelia.hhmi.org> |
46 | 40 | */ |
47 | 41 | @CompressionType("blosc") |
48 | | -public class BloscCompression implements DefaultBlockReader, DefaultBlockWriter, Compression { |
| 42 | +public class BloscCompression implements Compression { |
49 | 43 |
|
50 | 44 | public static final int NOSHUFFLE = 0; |
51 | 45 | public static final int SHUFFLE = 1; |
@@ -138,68 +132,33 @@ public BloscCompression(final BloscCompression template) { |
138 | 132 | this.nthreads = template.nthreads; |
139 | 133 | } |
140 | 134 |
|
141 | | - @Override |
142 | | - public <T, B extends DataBlock<T>> void read( |
143 | | - final B dataBlock, |
144 | | - final InputStream in) throws IOException { |
145 | | - |
146 | | - final ByteBuffer src = ByteBuffer.wrap(IOUtils.toByteArray(in)); |
147 | | - final boolean isByte = dataBlock.getData() instanceof byte[]; |
| 135 | + private byte[] decode(final byte[] data, final byte[] dstBuffer) { |
| 136 | + final ByteBuffer src = ByteBuffer.wrap(data); |
148 | 137 | final ByteBuffer dst; |
149 | | - if (isByte) |
150 | | - dst = dataBlock.toByteBuffer(); |
151 | | - else { |
| 138 | + if (dstBuffer != null) { |
| 139 | + dst = ByteBuffer.wrap(dstBuffer); |
| 140 | + } else { |
152 | 141 | final BufferSizes sizes = blosc.cbufferSizes(src); |
153 | 142 | final int dstSize = (int)sizes.getNbytes(); |
154 | | - dst = ByteBuffer.allocateDirect(dstSize); |
| 143 | + dst = ByteBuffer.allocate(dstSize); |
155 | 144 | } |
156 | 145 | JBlosc.decompressCtx(src, dst, dst.capacity(), nthreads); |
157 | | - dataBlock.readData(dst); |
| 146 | + return dst.array(); |
158 | 147 | } |
159 | 148 |
|
160 | 149 | @Override |
161 | | - public <T> void write( |
162 | | - final DataBlock<T> dataBlock, |
163 | | - final OutputStream out) throws IOException { |
| 150 | + public ReadData decode(final ReadData readData) throws IOException { |
| 151 | + return ReadData.from(decode(readData.allBytes(), null)); |
| 152 | + } |
164 | 153 |
|
165 | | - final ByteBuffer src = dataBlock.toByteBuffer(); |
166 | | - final ByteBuffer dst = ByteBuffer.allocate(src.limit() + JBlosc.OVERHEAD); |
| 154 | + @Override |
| 155 | + public ReadData encode(final ReadData readData) throws IOException { |
| 156 | + final byte[] serialized = readData.allBytes(); |
| 157 | + final ByteBuffer src = ByteBuffer.wrap(serialized); |
| 158 | + final ByteBuffer dst = ByteBuffer.allocate(serialized.length + JBlosc.OVERHEAD); |
167 | 159 | JBlosc.compressCtx(clevel, shuffle, 1, src, src.limit(), dst, dst.limit(), cname, blocksize, nthreads); |
168 | 160 | final BufferSizes sizes = blosc.cbufferSizes(dst); |
169 | 161 | final int dstSize = (int)sizes.getCbytes(); |
170 | | - out.write(dst.array(), 0, dstSize); |
171 | | - out.flush(); |
172 | | - } |
173 | | - |
174 | | - @Override |
175 | | - public BloscCompression getReader() { |
176 | | - |
177 | | - return this; |
178 | | - } |
179 | | - |
180 | | - @Override |
181 | | - public BloscCompression getWriter() { |
182 | | - |
183 | | - return this; |
184 | | - } |
185 | | - |
186 | | - /** |
187 | | - * Not used in this implementation of {@link DefaultBlockWriter} as |
188 | | - * {@link JBlosc} decompresses from and into {@link ByteBuffer}. |
189 | | - */ |
190 | | - @Override |
191 | | - public OutputStream getOutputStream(final OutputStream out) throws IOException { |
192 | | - |
193 | | - return null; |
194 | | - } |
195 | | - |
196 | | - /** |
197 | | - * Not used in this implementation of {@link DefaultBlockReader} as |
198 | | - * {@link JBlosc} compresses from and into {@link ByteBuffer}. |
199 | | - */ |
200 | | - @Override |
201 | | - public InputStream getInputStream(final InputStream in) throws IOException { |
202 | | - |
203 | | - return null; |
| 162 | + return ReadData.from(dst.array(), 0, dstSize); |
204 | 163 | } |
205 | 164 | } |
0 commit comments