Skip to content

Commit e6c7a8f

Browse files
authored
Minor updates to MemoryMappedFileBuffer (#1700)
* remove unecessary stream creation * prevent potential buffer overflow
1 parent 9fe9042 commit e6c7a8f

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/main/java/htsjdk/samtools/MemoryMappedFileBuffer.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.ByteOrder;
99
import java.nio.MappedByteBuffer;
1010
import java.nio.channels.FileChannel;
11+
import java.nio.file.StandardOpenOption;
1112

1213
/**
1314
* Traditional implementation of BAM index file access using memory mapped files.
@@ -16,14 +17,9 @@ class MemoryMappedFileBuffer implements IndexFileBuffer {
1617
private MappedByteBuffer mFileBuffer;
1718

1819
MemoryMappedFileBuffer(final File file) {
19-
try {
20-
// Open the file stream.
21-
final FileInputStream fileStream = new FileInputStream(file);
22-
final FileChannel fileChannel = fileStream.getChannel();
20+
try(final FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);) {
2321
mFileBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0L, fileChannel.size());
2422
mFileBuffer.order(ByteOrder.LITTLE_ENDIAN);
25-
fileChannel.close();
26-
fileStream.close();
2723
} catch (final IOException exc) {
2824
throw new RuntimeIOException(exc.getMessage(), exc);
2925
}
@@ -51,7 +47,7 @@ public void skipBytes(final int count) {
5147

5248
@Override
5349
public void seek(final long position) {
54-
mFileBuffer.position((int)position);
50+
mFileBuffer.position(Math.toIntExact(position));
5551
}
5652

5753
@Override

0 commit comments

Comments
 (0)