From ecd82724000bd2465be8600d785133c97ea5af40 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Fri, 21 Aug 2020 11:37:27 +0100 Subject: [PATCH] JAL-3615 code tidy, improve unit test --- src/jalview/io/FileParse.java | 13 ++++++------- test/jalview/io/FileIOTester.java | 8 ++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index d986dc5..2ff0d27 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -230,19 +230,18 @@ public class FileParse return false; } input.mark(4); - byte[] bytes = new byte[2]; // input.readNBytes(2); + + // get first 2 bytes or return false + byte[] bytes = new byte[2]; int read = input.read(bytes); input.reset(); if (read != bytes.length) { return false; } - if (bytes.length == 2) - { - int header = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); - return (GZIPInputStream.GZIP_MAGIC == header); - } - return false; + + int header = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); + return (GZIPInputStream.GZIP_MAGIC == header); } /** diff --git a/test/jalview/io/FileIOTester.java b/test/jalview/io/FileIOTester.java index 04d01b0..e37ae3f 100644 --- a/test/jalview/io/FileIOTester.java +++ b/test/jalview/io/FileIOTester.java @@ -24,10 +24,12 @@ import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.StringBufferInputStream; import org.testng.AssertJUnit; import org.testng.annotations.AfterClass; @@ -147,6 +149,12 @@ public class FileIOTester */ is = new BufferedInputStream(is, 16); assertTrue(FileParse.isGzipStream(is)); + + /* + * check recognition of non-gzipped input + */ + assertFalse(FileParse.isGzipStream(new BufferedInputStream( + new ByteArrayInputStream("NOT A GZIP".getBytes())))); } @Test(groups = { "Functional" }) -- 1.7.10.2