From ed35b361b63aa6587bb06b6f5eabd6d0f78bc122 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sat, 9 Apr 2016 21:51:09 +0900 Subject: [PATCH] Publish checkTrailer Signed-off-by: Hiroshi Miura --- .../main/java/org/dict/zip/DictZipInputStream.java | 12 ++++------ .../java/org/dict/zip/DictZipInputStreamTest.java | 28 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dictzip-lib/src/main/java/org/dict/zip/DictZipInputStream.java b/dictzip-lib/src/main/java/org/dict/zip/DictZipInputStream.java index d6c2498..23b4d24 100644 --- a/dictzip-lib/src/main/java/org/dict/zip/DictZipInputStream.java +++ b/dictzip-lib/src/main/java/org/dict/zip/DictZipInputStream.java @@ -32,8 +32,6 @@ import java.util.zip.CRC32; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; -import static org.dict.zip.DictZipFileUtils.readUInt; - /** * DictZipInputStream. @@ -248,7 +246,7 @@ public class DictZipInputStream extends InflaterInputStream { * Check gzip member trailer; CRC and length. * @throws IOException when CRC error or total length error. */ - private void checkTrailer() throws IOException { + public void checkTrailer() throws IOException { InputStream in = this.in; int num = inf.getRemaining(); if (num > 0) { @@ -256,13 +254,13 @@ public class DictZipInputStream extends InflaterInputStream { new ByteArrayInputStream(buf, len - num, num), in); } long val = crc.getValue(); - long crcValue = readUInt(in); + long crcValue = DictZipFileUtils.readUInt(in); if (crcValue != val) { throw new IOException(MessageFormat .format("Incorrect CRC: Computed CRC = %8x / From input %8x", val, crcValue)); } long total = inf.getTotalOut(); - long trailerTotal = readUInt(in); + long trailerTotal = DictZipFileUtils.readUInt(in); if (trailerTotal != total) { throw new IOException(MessageFormat .format("False number of uncompressed bytes: Computed size =%d / From input %d", @@ -279,8 +277,8 @@ public class DictZipInputStream extends InflaterInputStream { RandomAccessInputStream rain = (RandomAccessInputStream) in; compLength = rain.getLength(); rain.seek(compLength - 8); - crcVal = readUInt(rain); - totalLength = readUInt(rain); + crcVal = DictZipFileUtils.readUInt(rain); + totalLength = DictZipFileUtils.readUInt(rain); } else { throw new IOException("Illegal type of InputStream."); } diff --git a/dictzip-lib/src/test/java/org/dict/zip/DictZipInputStreamTest.java b/dictzip-lib/src/test/java/org/dict/zip/DictZipInputStreamTest.java index f2d8490..3bc432f 100644 --- a/dictzip-lib/src/test/java/org/dict/zip/DictZipInputStreamTest.java +++ b/dictzip-lib/src/test/java/org/dict/zip/DictZipInputStreamTest.java @@ -25,6 +25,7 @@ import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import java.io.EOFException; import java.io.IOException; import java.util.Arrays; @@ -130,7 +131,34 @@ public class DictZipInputStreamTest { din.readFully(buf, off, len); } + /** + * Test of readFully method, of class DictZipInputStream. + * @throws java.lang.Exception + */ + @Test + public void testReadFully_checkTrailer() throws Exception { + System.out.println("readFully and checkTrailer"); + getDZHeader(din); + byte[] buf = new byte[512]; + try { + din.readFully(buf); + } catch (RuntimeException e) { + throw e; + } catch (EOFException e) { + // continue + } + // read trailer + try { + din.readTrailer(); + } catch (RuntimeException e) { + throw e; + } + assertEquals(din.getCrc(), 0x024d1f37); + assertEquals(din.getLength(), 383783); + } + + /** * Test of readHeader method, of class DictZipInputStream. * @throws java.lang.Exception */ -- 2.11.0