Skip to content
Snippets Groups Projects
Commit 44a92b69 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Create FileTypeInspector which determine file type based on magic value.

parent 76346d87
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,11 @@ import cz.it4i.qcmp.cli.CustomFunctionBase; ...@@ -7,10 +7,11 @@ import cz.it4i.qcmp.cli.CustomFunctionBase;
import cz.it4i.qcmp.cli.functions.DebugFunction; import cz.it4i.qcmp.cli.functions.DebugFunction;
import cz.it4i.qcmp.compression.ImageCompressor; import cz.it4i.qcmp.compression.ImageCompressor;
import cz.it4i.qcmp.compression.ImageDecompressor; import cz.it4i.qcmp.compression.ImageDecompressor;
import cz.it4i.qcmp.fileformat.FileExtensions;
import cz.it4i.qcmp.fileformat.IQvcFile; import cz.it4i.qcmp.fileformat.IQvcFile;
import cz.it4i.qcmp.io.FileTypeInspector;
import cz.it4i.qcmp.io.QuantizationCacheManager; import cz.it4i.qcmp.io.QuantizationCacheManager;
import cz.it4i.qcmp.io.QvcFileReader; import cz.it4i.qcmp.io.QvcFileReader;
import cz.it4i.qcmp.utilities.ColorConsole;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import java.io.IOException; import java.io.IOException;
...@@ -108,47 +109,69 @@ public class DataCompressor { ...@@ -108,47 +109,69 @@ public class DataCompressor {
} }
break; break;
case InspectFile: { case InspectFile: {
if (parsedOptions.getInputDataInfo().getFilePath().endsWith(FileExtensions.CACHE_FILE_EXT)) { final FileTypeInspector.FileType fileType = FileTypeInspector.inspectFile(parsedOptions.getInputDataInfo().getFilePath());
QuantizationCacheManager.inspectCacheFile(parsedOptions.getInputDataInfo().getFilePath(), switch (fileType) {
parsedOptions.isVerbose()); case Qcmp: {
} else { final ImageDecompressor decompressor = new ImageDecompressor(parsedOptions);
final ImageDecompressor decompressor = new ImageDecompressor(parsedOptions); try {
try { System.out.println(decompressor.inspectCompressedFile());
System.out.println(decompressor.inspectCompressedFile()); } catch (final IOException e) {
} catch (final IOException e) { System.err.println("Errors occurred during inspecting file.");
System.err.println("Errors occurred during inspecting file."); System.err.println(e.getMessage());
System.err.println(e.getMessage()); e.printStackTrace();
e.printStackTrace(); }
} }
break;
case Qvc: {
QuantizationCacheManager.inspectCacheFile(parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.isVerbose());
}
break;
case InvalidPath:
exitWithInvalidFilePath();
break;
case Unknown:
exitWithUnknownFile();
break;
} }
} }
break; break;
case Convert: { case Convert: {
final boolean inPlace = parsedOptions.getOutputFilePath() == null; final FileTypeInspector.FileType fileType = FileTypeInspector.inspectFile(parsedOptions.getInputDataInfo().getFilePath());
// TODO(Moravec): Maybe replace with generic reader which can determine file type based on magic value and not an extension. switch (fileType) {
if (parsedOptions.getInputDataInfo().getFilePath().endsWith(FileExtensions.CACHE_FILE_EXT)) { case Qcmp: {
IQvcFile cacheFile = null; System.err.println("Qcmp file conversion isn't supported yet");
try {
cacheFile = QvcFileReader.readCacheFile(parsedOptions.getInputDataInfo().getFilePath());
} catch (final IOException e) {
System.err.println("Unable to read QVC file. Error: " + e.getMessage());
exitApplication(1);
}
try {
assert (cacheFile != null);
cacheFile.convertToNewerVersion(inPlace, parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.getOutputFilePath());
} catch (final IOException e) {
System.err.println("Unable to convert specified QVC file. Error: " + e.getMessage());
exitApplication(1);
} }
break;
case Qvc: {
IQvcFile cacheFile = null;
try {
cacheFile = QvcFileReader.readCacheFile(parsedOptions.getInputDataInfo().getFilePath());
} catch (final IOException e) {
System.err.println("Unable to read QVC file. Error: " + e.getMessage());
exitApplication(1);
}
final boolean inPlace = parsedOptions.getOutputFilePath() == null;
try {
assert (cacheFile != null);
cacheFile.convertToNewerVersion(inPlace, parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.getOutputFilePath());
} catch (final IOException e) {
System.err.println("Unable to convert specified QVC file. Error: " + e.getMessage());
exitApplication(1);
}
if (parsedOptions.isVerbose()) { if (parsedOptions.isVerbose()) {
System.err.println("Qvc file is converted."); System.err.println("Qvc file is converted.");
}
} }
break;
} else { case InvalidPath:
System.err.println("Qcmp file conversion isn't supported yet"); exitWithInvalidFilePath();
break;
case Unknown:
exitWithUnknownFile();
break;
} }
} }
break; break;
...@@ -156,6 +179,21 @@ public class DataCompressor { ...@@ -156,6 +179,21 @@ public class DataCompressor {
ScifioWrapper.dispose(); ScifioWrapper.dispose();
} }
private static void exitWithUnknownFile() {
ColorConsole.fprintf(ColorConsole.Target.stderr,
ColorConsole.Color.Red,
"Provided file is of unknown type. Only QCMP and QVC files are supported.\n");
exitApplication(1);
}
private static void exitWithInvalidFilePath() {
ColorConsole.fprintf(ColorConsole.Target.stderr,
ColorConsole.Color.Red,
"File specified by provided path doesn't exist.\n");
exitApplication(1);
}
private static void exitApplication(final int exitCode) { private static void exitApplication(final int exitCode) {
ScifioWrapper.dispose(); ScifioWrapper.dispose();
System.exit(exitCode); System.exit(exitCode);
......
...@@ -15,7 +15,7 @@ public class QCMPFileHeader implements IFileHeader, Cloneable { ...@@ -15,7 +15,7 @@ public class QCMPFileHeader implements IFileHeader, Cloneable {
//region Constants //region Constants
private static final int VERSION = 1; private static final int VERSION = 1;
private static final int BASE_QCMP_HEADER_SIZE = 23; private static final int BASE_QCMP_HEADER_SIZE = 23;
private static final String MAGIC_VALUE = "QCMPFILE"; public static final String MAGIC_VALUE = "QCMPFILE";
//endregion //endregion
//region Header fields //region Header fields
......
package cz.it4i.qcmp.io;
import cz.it4i.qcmp.fileformat.QCMPFileHeader;
import cz.it4i.qcmp.fileformat.QvcHeaderV1;
import cz.it4i.qcmp.fileformat.QvcHeaderV2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileTypeInspector {
public enum FileType {
/**
* Compressed image hyperstack.
*/
Qcmp,
/**
* Codebook cache file.
*/
Qvc,
/**
* Invalid file path.
*/
InvalidPath,
/**
* No known magic value was matched.
*/
Unknown
}
/**
* Inspect file specified by path and return its type.
*
* @param filePath Path to the file.
* @return File type.
*/
public static FileType inspectFile(final String filePath) {
// QCMPFileHeader.MAGIC_VALUE // 8 bytes
// QvcHeaderV1.MAGIC_VALUE // 9 bytes
// QvcHeaderV2.MAGIC_VALUE // 9 bytes
try (final FileInputStream stream = new FileInputStream(filePath)) {
final byte[] buf1 = new byte[QCMPFileHeader.MAGIC_VALUE.length()];
RawDataIO.readFullBuffer(stream, buf1);
if (new String(buf1).equals(QCMPFileHeader.MAGIC_VALUE)) {
return FileType.Qcmp;
}
final byte[] buf2 = new byte[QvcHeaderV1.MAGIC_VALUE.length()];
System.arraycopy(buf1, 0, buf2, 0, buf1.length);
final int read = stream.read(buf2, buf1.length, 1);
if (read != 1)
return FileType.Unknown;
final String magicValue = new String(buf2);
if (magicValue.equals(QvcHeaderV1.MAGIC_VALUE) || magicValue.equals(QvcHeaderV2.MAGIC_VALUE))
return FileType.Qvc;
} catch (final FileNotFoundException e) {
return FileType.InvalidPath;
} catch (final IOException e) {
return FileType.Unknown;
}
return FileType.Unknown;
}
}
...@@ -48,10 +48,10 @@ public final class ColorConsole { ...@@ -48,10 +48,10 @@ public final class ColorConsole {
switch (target) { switch (target) {
case stdout: case stdout:
System.out.println(getColor(color) + string + ANSI_RESET); System.out.print(getColor(color) + string + ANSI_RESET);
break; break;
case stderr: case stderr:
System.err.println(getColor(color) + string + ANSI_RESET); System.err.print(getColor(color) + string + ANSI_RESET);
break; break;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment