final static int MAX_PATTERN_LENGTH = 44;
final byte[][] magic;
-
+
FileMagic(long magic) {
this.magic = new byte[1][8];
LittleEndian.putLong(this.magic[0], 0, magic);
FileMagic(byte[]... magic) {
this.magic = magic;
}
-
+
FileMagic(String... magic) {
this.magic = new byte[magic.length][];
int i=0;
// read as many bytes as possible, up to the required number of bytes
byte[] data = new byte[MAX_PATTERN_LENGTH];
int read = IOUtils.readFully(fis, data, 0, MAX_PATTERN_LENGTH);
+ if(read == -1) {
+ return FileMagic.UNKNOWN;
+ }
// only use the bytes that could be read
data = Arrays.copyOf(data, read);
@Test
public void testShortFile() throws IOException {
// having a file shorter than 8 bytes previously caused an exception
- byte[] data = new byte[] { -1, -40, -1, -32, 0 };
+ fetchMagicFromData(new byte[] { -1, -40, -1, -32, 0 });
+ fetchMagicFromData(new byte[] { -1, -40, -1, -32 });
+ fetchMagicFromData(new byte[] { -1, -40, -1 });
+ fetchMagicFromData(new byte[] { -1, -40 });
+ fetchMagicFromData(new byte[] { -1 });
+ fetchMagicFromData(new byte[0]);
+ }
+ private void fetchMagicFromData(byte[] data) throws IOException {
File file = TempFile.createTempFile("TestFileMagic", ".bin");
try {
try (FileOutputStream fos = new FileOutputStream(file)) {