Skip to content

Commit b09b1ee

Browse files
committed
Explicitly make the ByteBuffer content readonly
This does not change any behaviour, but it would prevent bugs in the future if something tries to write to the buffer. If fileMode is MEMORY_MAPPED, this would already fail, so this extra protection only really applies to the MEMORY mode.
1 parent a7eaec6 commit b09b1ee

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/main/java/com/maxmind/db/BufferHolder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ final class BufferHolder {
2121
final FileChannel channel = file.getChannel()
2222
) {
2323
if (mode == FileMode.MEMORY) {
24-
this.buffer = ByteBuffer.wrap(new byte[(int) channel.size()]);
25-
if (channel.read(this.buffer) != this.buffer.capacity()) {
24+
final ByteBuffer buf = ByteBuffer.wrap(new byte[(int) channel.size()]);
25+
if (channel.read(buf) != buf.capacity()) {
2626
throw new IOException("Unable to read "
2727
+ database.getName()
2828
+ " into memory. Unexpected end of stream.");
2929
}
30+
this.buffer = buf.asReadOnlyBuffer();
3031
} else {
31-
this.buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
32+
this.buffer = channel.map(MapMode.READ_ONLY, 0, channel.size()).asReadOnlyBuffer();
3233
}
3334
}
3435
}
@@ -50,7 +51,7 @@ final class BufferHolder {
5051
while (-1 != (br = stream.read(bytes))) {
5152
baos.write(bytes, 0, br);
5253
}
53-
this.buffer = ByteBuffer.wrap(baos.toByteArray());
54+
this.buffer = ByteBuffer.wrap(baos.toByteArray()).asReadOnlyBuffer();
5455
}
5556

5657
/*

0 commit comments

Comments
 (0)