|
1 | 1 | package com.maxmind.db; |
2 | 2 |
|
| 3 | +import com.maxmind.db.Reader.FileMode; |
3 | 4 | import java.io.ByteArrayOutputStream; |
4 | 5 | import java.io.File; |
5 | 6 | import java.io.IOException; |
|
9 | 10 | import java.nio.channels.FileChannel; |
10 | 11 | import java.nio.channels.FileChannel.MapMode; |
11 | 12 |
|
12 | | -import com.maxmind.db.Reader.FileMode; |
13 | | - |
14 | 13 | final class BufferHolder { |
15 | 14 | // DO NOT PASS OUTSIDE THIS CLASS. Doing so will remove thread safety. |
16 | 15 | private final ByteBuffer buffer; |
17 | 16 |
|
18 | 17 | BufferHolder(File database, FileMode mode) throws IOException { |
19 | 18 | try ( |
20 | | - final RandomAccessFile file = new RandomAccessFile(database, "r"); |
21 | | - final FileChannel channel = file.getChannel() |
| 19 | + final RandomAccessFile file = new RandomAccessFile(database, "r"); |
| 20 | + final FileChannel channel = file.getChannel() |
22 | 21 | ) { |
23 | 22 | if (mode == FileMode.MEMORY) { |
24 | 23 | final ByteBuffer buf = ByteBuffer.wrap(new byte[(int) channel.size()]); |
25 | 24 | if (channel.read(buf) != buf.capacity()) { |
26 | 25 | throw new IOException("Unable to read " |
27 | | - + database.getName() |
28 | | - + " into memory. Unexpected end of stream."); |
| 26 | + + database.getName() |
| 27 | + + " into memory. Unexpected end of stream."); |
29 | 28 | } |
30 | 29 | this.buffer = buf.asReadOnlyBuffer(); |
31 | 30 | } else { |
@@ -61,19 +60,21 @@ final class BufferHolder { |
61 | 60 | ByteBuffer get() { |
62 | 61 | // The Java API docs for buffer state: |
63 | 62 | // |
64 | | - // Buffers are not safe for use by multiple concurrent threads. If a buffer is to be used by more than |
65 | | - // one thread then access to the buffer should be controlled by appropriate synchronization. |
| 63 | + // Buffers are not safe for use by multiple concurrent threads. If a buffer is to be |
| 64 | + // used by more than one thread then access to the buffer should be controlled by |
| 65 | + // appropriate synchronization. |
66 | 66 | // |
67 | | - // As such, you may think that this should be synchronized. This used to be the case, but we had several |
68 | | - // complaints about the synchronization causing contention, e.g.: |
| 67 | + // As such, you may think that this should be synchronized. This used to be the case, but |
| 68 | + // we had several complaints about the synchronization causing contention, e.g.: |
69 | 69 | // |
70 | 70 | // * https://github.com/maxmind/MaxMind-DB-Reader-java/issues/65 |
71 | 71 | // * https://github.com/maxmind/MaxMind-DB-Reader-java/pull/69 |
72 | 72 | // |
73 | | - // Given that we are not modifying the original ByteBuffer in any way and all currently known and most |
74 | | - // reasonably imaginable implementations of duplicate() only do read operations on the original buffer object, |
75 | | - // the risk of not synchronizing this call seems relatively low and worth taking for the performance benefit |
76 | | - // when lookups are being done from many threads. |
| 73 | + // Given that we are not modifying the original ByteBuffer in any way and all currently |
| 74 | + // known and most reasonably imaginable implementations of duplicate() only do read |
| 75 | + // operations on the original buffer object, the risk of not synchronizing this call seems |
| 76 | + // relatively low and worth taking for the performance benefit when lookups are being done |
| 77 | + // from many threads. |
77 | 78 | return this.buffer.duplicate(); |
78 | 79 | } |
79 | 80 | } |
0 commit comments