Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cz.it4i.qcmp.io;
import cz.it4i.qcmp.fileformat.IFileHeader;
import cz.it4i.qcmp.fileformat.IQcmpFile;
import cz.it4i.qcmp.fileformat.QCMPFileHeaderV1;
import cz.it4i.qcmp.fileformat.QCMPFileHeaderV2;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class QcmpFileReader {
private static final int QCMP_HEADER_MAGIC_VALUE_SIZE = 8;
/**
* Read cache file from file.
*
* @param path File path.
* @return Cache file or null if reading fails.
*/
public static IQcmpFile readCacheFile(final String path) throws IOException {
try (final FileInputStream fis = new FileInputStream(path)) {
return readQcmpFile(fis);
}
}
/**
* Make DataInputStream from InputStream.
*
* @param inputStream Some input stream.
* @return DataInputStream.
*/
private static DataInputStream asDataInputStream(final InputStream inputStream) {
if (inputStream instanceof DataInputStream) {
return (DataInputStream) inputStream;
} else {
return new DataInputStream(inputStream);
}
}
/**
* Create correct Qcmp header version by analyzing the magic value.
*
* @param magicValue Magic value of the qcmp file.
* @return Correct version of QCMP header.
* @throws IOException when the magic value is unknown.
*/
private static IFileHeader getCorrectQcmpHeader(final String magicValue) throws IOException {
switch (magicValue) {
case QCMPFileHeaderV1.MAGIC_VALUE:
return new QCMPFileHeaderV1();
case QCMPFileHeaderV2.MAGIC_VALUE:
return new QCMPFileHeaderV2();
default:
throw new IOException("Invalid QCMP file. Unknown QCMP magic value: " + magicValue);
}
}
/**
* Read cache file by DataInputStream.
*
* @param inputStream Input stream.
* @return Cache file or null, if exception occurs.
*/
private static IQcmpFile readQcmpFile(final InputStream inputStream) throws IOException {
final DataInputStream dis = asDataInputStream(inputStream);
final byte[] magicValueBuffer = new byte[QCMP_HEADER_MAGIC_VALUE_SIZE];
RawDataIO.readFullBuffer(dis, magicValueBuffer);
final String magicValue = new String(magicValueBuffer);
final IFileHeader header = getCorrectQcmpHeader(magicValue);
header.readFromStream(dis);
assert ((header.getHeaderVersion() == 1 && header instanceof QCMPFileHeaderV1) ||
header.getHeaderVersion() == 2 && header instanceof QCMPFileHeaderV2);
// TODO(Moravec): Create the IQcmpFile impl. Think about it!
return null;
}
}