Skip to content

Commit

Permalink
Minor updates to MemoryMappedFileBuffer
Browse files Browse the repository at this point in the history
* remove unecessary stream creation
* prevent potential buffer overflow
  • Loading branch information
lbergelson committed Jan 23, 2024
1 parent b16416c commit abc1f80
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/main/java/htsjdk/samtools/MemoryMappedFileBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;

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

MemoryMappedFileBuffer(final File file) {
try {
// Open the file stream.
final FileInputStream fileStream = new FileInputStream(file);
final FileChannel fileChannel = fileStream.getChannel();
try(final FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);) {
mFileBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0L, fileChannel.size());
mFileBuffer.order(ByteOrder.LITTLE_ENDIAN);
fileChannel.close();
fileStream.close();
} catch (final IOException exc) {
throw new RuntimeIOException(exc.getMessage(), exc);
}
Expand Down Expand Up @@ -51,7 +47,7 @@ public void skipBytes(final int count) {

@Override
public void seek(final long position) {
mFileBuffer.position((int)position);
mFileBuffer.position(Math.toIntExact(position));
}

@Override
Expand Down

0 comments on commit abc1f80

Please sign in to comment.