Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public boolean equals(final Object other) {
}

@Override
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
final InputStream inflater = new BZip2CompressorInputStream(readData.inputStream());
return ReadData.from(inflater, decodedLength);
public ReadData decode(final ReadData readData) throws IOException {

return ReadData.from(new BZip2CompressorInputStream(readData.inputStream()));
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/janelia/saalfeldlab/n5/Compression.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,13 @@ default String getType() {
*
* @param readData
* data to decode
* @param decodedLength
* length of the decoded data (-1 if unknown)
*
* @return decoded ReadData
*
* @throws IOException
* if any I/O error occurs
*/
ReadData decode(ReadData readData, int decodedLength) throws IOException;
ReadData decode(ReadData readData) throws IOException;

/**
* Encode the given {@code readData}.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/DataBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ static int getNumElements(final int[] size) {
*/
interface DataBlockFactory<T> {


/**
* Factory for {@link DataBlock DataBlocks}.
*
* @param blockSize
* the block size
* @param gridPosition
* the grid position
* @param data
* the number of elements (not necessarily one element per block
* element)
* @return the data block
*/
DataBlock<T> createDataBlock(int[] blockSize, long[] gridPosition, T data);

}
}
76 changes: 24 additions & 52 deletions src/main/java/org/janelia/saalfeldlab/n5/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand All @@ -34,10 +34,6 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.janelia.saalfeldlab.n5.codec.N5Codecs;
import org.janelia.saalfeldlab.n5.codec.DataBlockCodec;
import org.janelia.saalfeldlab.n5.codec.N5Codecs.DataBlockCodecFactory;

/**
* Enumerates available data types.
*
Expand All @@ -47,101 +43,90 @@ public enum DataType {

UINT8(
"uint8",
(blockSize, gridPosition, numElements) -> new ByteArrayDataBlock(
blockSize,
gridPosition,
new byte[numElements]),
N5Codecs.BYTE),
(blockSize, gridPosition, numElements) -> {
ByteArrayDataBlock dataBlock = new ByteArrayDataBlock(blockSize, gridPosition, new byte[numElements]);


return new ByteArrayDataBlock(
blockSize,
gridPosition,
new byte[numElements]);
}),
UINT16(
"uint16",
(blockSize, gridPosition, numElements) -> new ShortArrayDataBlock(
blockSize,
gridPosition,
new short[numElements]),
N5Codecs.SHORT),
new short[numElements])),
UINT32(
"uint32",
(blockSize, gridPosition, numElements) -> new IntArrayDataBlock(
blockSize,
gridPosition,
new int[numElements]),
N5Codecs.INT),
new int[numElements])),
UINT64(
"uint64",
(blockSize, gridPosition, numElements) -> new LongArrayDataBlock(
blockSize,
gridPosition,
new long[numElements]),
N5Codecs.LONG),
new long[numElements])),
INT8(
"int8",
(blockSize, gridPosition, numElements) -> new ByteArrayDataBlock(
blockSize,
gridPosition,
new byte[numElements]),
N5Codecs.BYTE),
new byte[numElements])),
INT16(
"int16",
(blockSize, gridPosition, numElements) -> new ShortArrayDataBlock(
blockSize,
gridPosition,
new short[numElements]),
N5Codecs.SHORT),
new short[numElements])),
INT32(
"int32",
(blockSize, gridPosition, numElements) -> new IntArrayDataBlock(
blockSize,
gridPosition,
new int[numElements]),
N5Codecs.INT),
new int[numElements])),
INT64(
"int64",
(blockSize, gridPosition, numElements) -> new LongArrayDataBlock(
blockSize,
gridPosition,
new long[numElements]),
N5Codecs.LONG),
new long[numElements])),
FLOAT32(
"float32",
(blockSize, gridPosition, numElements) -> new FloatArrayDataBlock(
blockSize,
gridPosition,
new float[numElements]),
N5Codecs.FLOAT),
new float[numElements])),
FLOAT64(
"float64",
(blockSize, gridPosition, numElements) -> new DoubleArrayDataBlock(
blockSize,
gridPosition,
new double[numElements]),
N5Codecs.DOUBLE),
new double[numElements])),
STRING(
"string",
(blockSize, gridPosition, numElements) -> new StringDataBlock(
blockSize,
gridPosition,
new String[numElements]),
N5Codecs.STRING),
new String[numElements])),
OBJECT(
"object",
(blockSize, gridPosition, numElements) -> new ByteArrayDataBlock(
blockSize,
gridPosition,
new byte[numElements]),
N5Codecs.OBJECT);

new byte[numElements]));

private final String label;

private final DataBlockFactory dataBlockFactory;

private final DataBlockCodecFactory<?> dataBlockCodecFactory;

DataType(final String label, final DataBlockFactory dataBlockFactory, final DataBlockCodecFactory<?> dataBlockCodecFactory) {
DataType(final String label, final DataBlockFactory dataBlockFactory) {

this.label = label;
this.dataBlockFactory = dataBlockFactory;
this.dataBlockCodecFactory = dataBlockCodecFactory;
}

@Override
Expand Down Expand Up @@ -190,19 +175,6 @@ public DataBlock<?> createDataBlock(final int[] blockSize, final long[] gridPosi
return dataBlockFactory.createDataBlock(blockSize, gridPosition, DataBlock.getNumElements(blockSize));
}

/**
* Get the default {@link DataBlockCodec}, with the specified {@code
* compression}, for {@link DataBlock DataBlocks} of this {@code DataType}.
* The default codec is used for de/serializing blocks to N5 format.
*
* @param compression
*
* @return the default {@code DataBlockCodec}
*/
public DataBlockCodec<?> createDataBlockCodec(final Compression compression) {
return dataBlockCodecFactory.createDataBlockCodec(compression);
}

private interface DataBlockFactory {

DataBlock<?> createDataBlock(final int[] blockSize, final long[] gridPosition, final int numElements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;

import org.janelia.saalfeldlab.n5.codec.DataBlockCodec;
import org.janelia.saalfeldlab.n5.codec.N5Codecs;

/**
* Mandatory dataset attributes:
Expand Down Expand Up @@ -58,16 +60,16 @@ public class DatasetAttributes implements Serializable {
private final long[] dimensions;
private final int[] blockSize;
private final DataType dataType;
private final Compression compression;
private final DataBlockCodec<?> dataBlockCodec;
private final Compression compression;

public DatasetAttributes(
final long[] dimensions,
final int[] blockSize,
final DataType dataType,
final Compression compression) {

this(dimensions, blockSize, dataType, compression, dataType.createDataBlockCodec(compression));
this(dimensions, blockSize, dataType, compression, N5Codecs.createDataBlockCodec(dataType, compression));
}

protected DatasetAttributes(
Expand Down Expand Up @@ -118,7 +120,7 @@ public DataType getDataType() {
* @return the {@code DataBlockCodec} for this dataset
*/
public <T> DataBlockCodec<T> getDataBlockCodec() {
return (DataBlockCodec<T>) dataBlockCodec;
return (DataBlockCodec<T>)dataBlockCodec;
}

public HashMap<String, Object> asMap() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/janelia/saalfeldlab/n5/GzipCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ private InputStream decode(final InputStream in) throws IOException {
}

@Override
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
final InputStream inflater = decode(readData.inputStream());
return ReadData.from(inflater, decodedLength);
public ReadData decode(final ReadData readData) throws IOException {

return ReadData.from(decode(readData.inputStream()));
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/janelia/saalfeldlab/n5/Lz4Compression.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public boolean equals(final Object other) {
}

@Override
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
final InputStream inflater = new LZ4BlockInputStream(readData.inputStream());
return ReadData.from(inflater, decodedLength);
public ReadData decode(final ReadData readData) throws IOException {

return ReadData.from(new LZ4BlockInputStream(readData.inputStream()));
}

@Override
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/janelia/saalfeldlab/n5/RawCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public class RawCompression implements Compression {

@Override
public boolean equals(final Object other) {
if (other == null || other.getClass() != RawCompression.class)
return false;
else
return true;
return other != null && other.getClass() == RawCompression.class;
}

@Override
Expand All @@ -47,7 +44,7 @@ public ReadData encode(final ReadData readData) {
}

@Override
public ReadData decode(final ReadData readData, int decodedLength) {
public ReadData decode(final ReadData readData) {
return readData;
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/janelia/saalfeldlab/n5/XzCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public boolean equals(final Object other) {
}

@Override
public ReadData decode(final ReadData readData, final int decodedLength) throws IOException {
final InputStream inflater = new XZCompressorInputStream(readData.inputStream());
return ReadData.from(inflater, decodedLength);
public ReadData decode(final ReadData readData) throws IOException {

return ReadData.from(new XZCompressorInputStream(readData.inputStream()));
}

@Override
Expand Down
Loading