|
| 1 | +package org.janelia.saalfeldlab.n5.codec; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +import org.janelia.saalfeldlab.n5.DatasetAttributes; |
| 6 | +import org.janelia.saalfeldlab.n5.N5Exception; |
| 7 | + |
| 8 | +public class CodecParser { |
| 9 | + |
| 10 | + public DatasetCodecInfo[] datasetCodecInfos; |
| 11 | + public BlockCodecInfo blockCodecInfo; |
| 12 | + public DataCodecInfo[] dataCodecInfos; |
| 13 | + |
| 14 | + public CodecParser(CodecInfo[] codecs) { |
| 15 | + |
| 16 | + parse(codecs); |
| 17 | + } |
| 18 | + |
| 19 | + private void parse(CodecInfo[] codecs) { |
| 20 | + |
| 21 | + final ArrayList<DataCodecInfo> dataCodecList = new ArrayList<>(); |
| 22 | + final ArrayList<DatasetCodecInfo> datasetCodecList = new ArrayList<>(); |
| 23 | + |
| 24 | + boolean foundBlockCodec = false; |
| 25 | + |
| 26 | + int i = 0; |
| 27 | + int blockCodecIndex = -1; |
| 28 | + for (CodecInfo codec : codecs) { |
| 29 | + if (!foundBlockCodec) { |
| 30 | + |
| 31 | + if (codec instanceof BlockCodecInfo) { |
| 32 | + blockCodecInfo = (BlockCodecInfo)codec; |
| 33 | + foundBlockCodec = true; |
| 34 | + blockCodecIndex = i; |
| 35 | + } else if (codec instanceof DatasetCodecInfo) |
| 36 | + datasetCodecList.add((DatasetCodecInfo)codec); |
| 37 | + else |
| 38 | + throw new N5Exception("Codec at index " + i + " is a DataCodec, but came before a BlockCodec."); |
| 39 | + |
| 40 | + } else if (codec instanceof BlockCodecInfo) |
| 41 | + throw new N5Exception("Codec at index " + i + " is a BlockCodec, but came after a BlockCodec at position " + blockCodecIndex); |
| 42 | + else if (codec instanceof DatasetCodecInfo) |
| 43 | + throw new N5Exception("Codec at index " + i + " is a DatasetCodec, but came after a BlockCodec at position " + blockCodecIndex); |
| 44 | + else |
| 45 | + dataCodecList.add((DataCodecInfo)codec); |
| 46 | + |
| 47 | + i++; |
| 48 | + } |
| 49 | + |
| 50 | + datasetCodecInfos = datasetCodecList.stream().toArray(n -> new DatasetCodecInfo[n]); |
| 51 | + dataCodecInfos = dataCodecList.stream().toArray(n -> new DataCodecInfo[n]); |
| 52 | + } |
| 53 | + |
| 54 | + private static CodecInfo[] concatenateCodecs(DatasetAttributes attributes) { |
| 55 | + |
| 56 | + final CodecInfo[] codecs = new CodecInfo[attributes.getDataCodecInfos().length + 1]; |
| 57 | + codecs[0] = attributes.getBlockCodecInfo(); |
| 58 | + System.arraycopy(attributes.getDataCodecInfos(), 0, codecs, 1, attributes.getDataCodecInfos().length); |
| 59 | + return codecs; |
| 60 | + } |
| 61 | +} |
0 commit comments