Skip to content

Commit

Permalink
FEAT: Use memory mapped stream for files < 2M.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlwilson committed Jul 7, 2020
1 parent 4444f3b commit da6f2fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/com/easyinnova/tiff/io/MappedByteInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.BufferUnderflowException;
import java.nio.MappedByteBuffer;
Expand Down Expand Up @@ -66,6 +67,17 @@ public MappedByteInputStream(File file) throws FileNotFoundException {
}
}

public MappedByteInputStream(RandomAccessFile raf) throws FileNotFoundException {
path = "";
ch = raf.getChannel();
try {
mbsize = ch.size();
mb = ch.map(FileChannel.MapMode.READ_ONLY, 0L, mbsize);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public String getPath() {
return path;
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/easyinnova/tiff/io/TiffInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,26 @@ public TiffInputStream(File file) throws FileNotFoundException {
}

public TiffInputStream(RandomAccessFile raf) throws FileNotFoundException {
long mbsize = 0;
try {
mbsize = raf.getChannel().size();
} catch (Exception ex) {

}

internalFile = null;
internalFileBig = new RandomAccessFileInputStream(raf);
internalFileBig = null;

if (mbsize > 2147483647L) {
internalFileBig = new RandomAccessFileInputStream(raf);
} else {
internalFile = new MappedByteInputStream(raf);
}
byteOrder = ByteOrder.BIG_ENDIAN;
fileOffset = 0;
buffer = new PagedInputBuffer(this);
}


/**
* Gets the byte order.
Expand Down

0 comments on commit da6f2fb

Please sign in to comment.