Skip to content

Commit

Permalink
Fix large TIFF files bug EasyinnovaSL#51
Browse files Browse the repository at this point in the history
  • Loading branch information
victormunoz committed Jun 13, 2017
1 parent 9097a5f commit ceb5787
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@ public class RandomAccessFileInputStream extends InputStream {
*/
private long limit = -1;

private String path;

/**
* Instantiates a new tiff file input stream.
*
* @param file the file
* @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();
}
Expand Down
62 changes: 55 additions & 7 deletions src/main/java/com/easyinnova/tiff/io/TiffInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* © 2015 Easy Innova, SL
* </p>
*
* @author Xavier Tarrés Bonet
* @author Victor Munoz
* @version 1.0
* @since 26/5/2015
*
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}

0 comments on commit ceb5787

Please sign in to comment.