From 8b9e7a6c121c92eaa6e7a53ffba07414ea88f91e Mon Sep 17 00:00:00 2001 From: Vojtech Moravec <vojtech.moravec.st@vsb.cz> Date: Fri, 11 Sep 2020 13:51:59 +0200 Subject: [PATCH] Implement ICacheFile reading/writing from/to stream. Also there was a bug when reading ICacheFile from stream, while providing the header. We didn't set the header of the read file so it was null and caused some NREs. --- .../cache/QuantizationCacheManager.java | 68 +++++++++++++++++-- .../java/azgracompress/cache/SQCacheFile.java | 1 + .../java/azgracompress/cache/VQCacheFile.java | 1 + 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/main/java/azgracompress/cache/QuantizationCacheManager.java b/src/main/java/azgracompress/cache/QuantizationCacheManager.java index 77a3427..5679ccf 100644 --- a/src/main/java/azgracompress/cache/QuantizationCacheManager.java +++ b/src/main/java/azgracompress/cache/QuantizationCacheManager.java @@ -1,5 +1,6 @@ package azgracompress.cache; +import azgracompress.compression.CompressionOptions; import azgracompress.data.V3i; import azgracompress.fileformat.QuantizationType; import azgracompress.quantization.scalar.SQCodebook; @@ -133,6 +134,7 @@ public class QuantizationCacheManager { return fileName; } + /** * Save VQ codebook to cache. * @@ -206,7 +208,7 @@ public class QuantizationCacheManager { * @param codebookSize Codebook size. * @return SQ cache file. */ - private SQCacheFile loadSQCacheFile(final String imageFile, final int codebookSize) { + public SQCacheFile loadSQCacheFile(final String imageFile, final int codebookSize) { final File path = getCacheFilePathForSQ(imageFile, codebookSize); try { return (SQCacheFile) readCacheFile(path, new SQCacheFile()); @@ -225,9 +227,9 @@ public class QuantizationCacheManager { * @param vDim Quantization vector dimension. * @return VQ cache file. */ - private VQCacheFile loadVQCacheFile(final String trainFile, - final int codebookSize, - final V3i vDim) { + public VQCacheFile loadVQCacheFile(final String trainFile, + final int codebookSize, + final V3i vDim) { final File path = getCacheFilePathForVQ(trainFile, codebookSize, vDim); try { return (VQCacheFile) readCacheFile(path, new VQCacheFile()); @@ -282,9 +284,36 @@ public class QuantizationCacheManager { return null; } - public static ICacheFile readCacheFile(final String path) { - try (FileInputStream fis = new FileInputStream(path); - DataInputStream dis = new DataInputStream(fis)) { + public ICacheFile loadCacheFile(final CompressionOptions compressionParams) { + String path; + int codebookSize = (int) Math.pow(2, compressionParams.getBitsPerCodebookIndex()); + switch (compressionParams.getQuantizationType()) { + + case Scalar: + path = getCacheFilePathForSQ(compressionParams.getInputDataInfo().getCacheFileName(), + codebookSize).getAbsolutePath(); + break; + case Vector1D: + case Vector2D: + case Vector3D: + path = getCacheFilePathForVQ(compressionParams.getInputDataInfo().getCacheFileName(), + codebookSize, + compressionParams.getQuantizationVector()).getAbsolutePath(); + break; + default: + return null; + } + return readCacheFile(path); + } + + /** + * Read cache file by DataInputStream. + * + * @param inputStream Input stream. + * @return Cache file or null, if exception occurs. + */ + private static ICacheFile readCacheFileImpl(final InputStream inputStream) { + try (DataInputStream dis = new DataInputStream(inputStream)) { CacheFileHeader header = new CacheFileHeader(); header.readFromStream(dis); @@ -297,6 +326,31 @@ public class QuantizationCacheManager { } } + /** + * Read cache file from input stream. + * + * @param inputStream Input data stream. + * @return Cache file or null if reading fails. + */ + public static ICacheFile readCacheFile(final InputStream inputStream) { + return readCacheFileImpl(inputStream); + } + + + /** + * Read cache file from file. + * + * @param path File path. + * @return Cache file or null if reading fails. + */ + public static ICacheFile readCacheFile(final String path) { + try (FileInputStream fis = new FileInputStream(path)) { + return readCacheFileImpl(fis); + } catch (IOException e) { + return null; + } + } + /** * Inspect cache file specified by the path. * diff --git a/src/main/java/azgracompress/cache/SQCacheFile.java b/src/main/java/azgracompress/cache/SQCacheFile.java index f7ffe5a..b6707ad 100644 --- a/src/main/java/azgracompress/cache/SQCacheFile.java +++ b/src/main/java/azgracompress/cache/SQCacheFile.java @@ -39,6 +39,7 @@ public class SQCacheFile implements ICacheFile { } public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException { + this.header = header; final int codebookSize = header.getCodebookSize(); final int[] centroids = new int[codebookSize]; final long[] frequencies = new long[codebookSize]; diff --git a/src/main/java/azgracompress/cache/VQCacheFile.java b/src/main/java/azgracompress/cache/VQCacheFile.java index 986a22a..9a0df5e 100644 --- a/src/main/java/azgracompress/cache/VQCacheFile.java +++ b/src/main/java/azgracompress/cache/VQCacheFile.java @@ -43,6 +43,7 @@ public class VQCacheFile implements ICacheFile { @Override public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException { + this.header = header; final int codebookSize = header.getCodebookSize(); final int entrySize = header.getVectorSizeX() * header.getVectorSizeY() * header.getVectorSizeZ(); -- GitLab