From ceb578750af4210547818bce1096fa74f29509a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Mu=C3=B1oz?= Date: Tue, 13 Jun 2017 10:17:50 +0200 Subject: [PATCH] Fix large TIFF files bug #51 --- .../tiff/io/MappedByteInputStream.java | 2 +- .../tiff/io/RandomAccessFileInputStream.java | 7 +++ .../easyinnova/tiff/io/TiffInputStream.java | 62 ++++++++++++++++--- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/easyinnova/tiff/io/MappedByteInputStream.java b/src/main/java/com/easyinnova/tiff/io/MappedByteInputStream.java index 7f795af..769c27f 100644 --- a/src/main/java/com/easyinnova/tiff/io/MappedByteInputStream.java +++ b/src/main/java/com/easyinnova/tiff/io/MappedByteInputStream.java @@ -11,7 +11,7 @@ import java.nio.channels.FileChannel; /** - * Created by easy on 09/08/2016. + * Created by Victor Munoz on 09/08/2016. */ public class MappedByteInputStream extends InputStream { diff --git a/src/main/java/com/easyinnova/tiff/io/RandomAccessFileInputStream.java b/src/main/java/com/easyinnova/tiff/io/RandomAccessFileInputStream.java index 7452ec9..c3e22fc 100644 --- a/src/main/java/com/easyinnova/tiff/io/RandomAccessFileInputStream.java +++ b/src/main/java/com/easyinnova/tiff/io/RandomAccessFileInputStream.java @@ -49,6 +49,8 @@ public class RandomAccessFileInputStream extends InputStream { */ private long limit = -1; + private String path; + /** * Instantiates a new tiff file input stream. * @@ -56,9 +58,14 @@ public class RandomAccessFileInputStream extends InputStream { * @throws FileNotFoundException the file not found exception */ public RandomAccessFileInputStream(File file) throws FileNotFoundException { + path = file.getPath(); randomAccessFile = new RandomAccessFile(file, "r"); } + public String getPath() { + return path; + } + @Override public int read() throws IOException { return randomAccessFile.read(); } diff --git a/src/main/java/com/easyinnova/tiff/io/TiffInputStream.java b/src/main/java/com/easyinnova/tiff/io/TiffInputStream.java index 60546a3..7902414 100644 --- a/src/main/java/com/easyinnova/tiff/io/TiffInputStream.java +++ b/src/main/java/com/easyinnova/tiff/io/TiffInputStream.java @@ -24,7 +24,7 @@ * © 2015 Easy Innova, SL *

* - * @author Xavier Tarrés Bonet + * @author Victor Munoz * @version 1.0 * @since 26/5/2015 * @@ -47,13 +47,14 @@ import java.io.EOFException; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * The Class TiffInputStream. */ -public class TiffInputStream extends MappedByteInputStream implements TiffDataIntput { +public class TiffInputStream implements TiffDataIntput { /** The Byte order. */ private ByteOrder byteOrder; @@ -64,13 +65,33 @@ public class TiffInputStream extends MappedByteInputStream implements TiffDataIn /** The buffer. */ private PagedInputBuffer buffer; + /** The file stream */ + private MappedByteInputStream internalFile; + private RandomAccessFileInputStream internalFileBig; + /** * Instantiates a new data byte order input stream. * @param file file * @throws FileNotFoundException sdf */ public TiffInputStream(File file) throws FileNotFoundException { - super(file); + FileInputStream f = new FileInputStream(file); + long mbsize = 0; + try { + mbsize = f.getChannel().size(); + } catch (Exception ex) { + + } + + internalFile = null; + internalFileBig = null; + + if (mbsize > 2147483647L) { + internalFileBig = new RandomAccessFileInputStream(file); + } else { + internalFile = new MappedByteInputStream(file); + } + byteOrder = ByteOrder.BIG_ENDIAN; fileOffset = 0; buffer = new PagedInputBuffer(this); @@ -85,11 +106,35 @@ public ByteOrder getByteOrder() { return byteOrder; } - public String getFilePath() { - return getPath(); + public String getFilePath() { + if (internalFile != null) + return internalFile.getPath(); + else + return internalFileBig.getPath(); } - /** + public int read() throws IOException { + if (internalFile != null) + return internalFile.read(); + else + return internalFileBig.read(); + } + + public void seek(long pos) throws IOException { + if (internalFile != null) + internalFile.seek(pos); + else + internalFileBig.seek(pos); + } + + public void close() throws IOException { + if (internalFile != null) + internalFile.close(); + else + internalFileBig.close(); + } + + /** * Gets the stream. * * @return the stream @@ -503,6 +548,9 @@ public Double readDouble() throws IOException { * @return the file size. */ public long size() { - return super.size(); + if (internalFile != null) + return internalFile.size(); + else + return internalFileBig.size(); } }