99import java .nio .channels .FileChannel .MapMode ;
1010import java .util .LinkedList ;
1111import java .util .List ;
12- import org .slf4j .Logger ;
13- import org .slf4j .LoggerFactory ;
14- import sun .nio .ch .FileChannelImpl ;
1512
1613public class LargeMappedByteBuffer implements Closeable {
1714
18- private static final Logger LOGGER = LoggerFactory .getLogger (LargeMappedByteBuffer .class );
19-
2015 private List <MappedByteBuffer > bufferList ;
2116 private long rawPosition ;
2217 private long position ;
18+ private long size ;
2319
2420 public LargeMappedByteBuffer (FileChannel fileChannel , MapMode mapMode , long position , long size )
2521 throws IOException {
2622 this .rawPosition = position ;
2723 this .position = position ;
24+ this .size = size ;
2825 int count = (int ) Math .ceil (size / (double ) Integer .MAX_VALUE );
2926 this .bufferList = new LinkedList <>();
3027 long calcPos = position ;
@@ -35,18 +32,22 @@ public LargeMappedByteBuffer(FileChannel fileChannel, MapMode mapMode, long posi
3532 }
3633 }
3734
38- public final void put (ByteBuffer byteBuffer ) {
39- int index = getIndex ();
40- long length = byteBuffer .limit () - byteBuffer .position ();
41- this .position += length ;
42- MappedByteBuffer mappedBuffer = bufferList .get (index );
43- if (mappedBuffer .remaining () < length ) {
44- byte [] temp = new byte [mappedBuffer .remaining ()];
45- byteBuffer .get (temp );
46- bufferList .get (index ).put (temp );
47- bufferList .get (index + 1 ).put (byteBuffer );
48- } else {
49- bufferList .get (index ).put (byteBuffer );
35+ public final void put (ByteBuffer byteBuffer ) throws IOException {
36+ try {
37+ int index = getIndex ();
38+ long length = byteBuffer .limit () - byteBuffer .position ();
39+ this .position += length ;
40+ MappedByteBuffer mappedBuffer = bufferList .get (index );
41+ if (mappedBuffer .remaining () < length ) {
42+ byte [] temp = new byte [mappedBuffer .remaining ()];
43+ byteBuffer .get (temp );
44+ bufferList .get (index ).put (temp );
45+ bufferList .get (index + 1 ).put (byteBuffer );
46+ } else {
47+ bufferList .get (index ).put (byteBuffer );
48+ }
49+ } catch (Exception e ) {
50+ throw new IOException ("LargeMappedByteBuffer put rawPosition-" +rawPosition +" size-" +size , e );
5051 }
5152 }
5253
@@ -57,13 +58,14 @@ private int getIndex() {
5758 @ Override
5859 public void close () throws IOException {
5960 try {
60- Method m = FileChannelImpl .class .getDeclaredMethod ("unmap" , MappedByteBuffer .class );
61+ Class <?> clazz = Class .forName ("sun.nio.ch.FileChannelImpl" );
62+ Method m = clazz .getDeclaredMethod ("unmap" , MappedByteBuffer .class );
6163 m .setAccessible (true );
6264 for (MappedByteBuffer mappedBuffer : bufferList ) {
63- m .invoke (FileChannelImpl . class , mappedBuffer );
65+ m .invoke (clazz , mappedBuffer );
6466 }
6567 } catch (Exception e ) {
66- LOGGER . error ("LargeMappedByteBuffer close:" , e );
68+ throw new IOException ("LargeMappedByteBuffer close" , e );
6769 }
6870 }
6971
0 commit comments