forked from proxyee-down-org/proxyee-down
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
33 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
common/src/main/java/lee/study/down/io/LargeMappedByteBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package lee.study.down.io; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.nio.ByteBuffer; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileChannel.MapMode; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class LargeMappedByteBuffer implements Closeable { | ||
|
||
private List<MappedByteBuffer> bufferList; | ||
private long rawPosition; | ||
private long position; | ||
private long size; | ||
|
||
public LargeMappedByteBuffer(FileChannel fileChannel, MapMode mapMode, long position, long size) | ||
throws IOException { | ||
this.rawPosition = position; | ||
this.position = position; | ||
this.size = size; | ||
int count = (int) Math.ceil(size / (double) Integer.MAX_VALUE); | ||
this.bufferList = new LinkedList<>(); | ||
long calcPos = position; | ||
for (int i = 0; i < count; i++) { | ||
long calcSize = i + 1 == count ? size % Integer.MAX_VALUE : Integer.MAX_VALUE; | ||
bufferList.add(fileChannel.map(mapMode, calcPos, calcSize)); | ||
calcPos += calcSize; | ||
} | ||
} | ||
|
||
public final void put(ByteBuffer byteBuffer) throws IOException { | ||
try { | ||
int index = getIndex(); | ||
long length = byteBuffer.limit() - byteBuffer.position(); | ||
this.position += length; | ||
MappedByteBuffer mappedBuffer = bufferList.get(index); | ||
if (mappedBuffer.remaining() < length) { | ||
byte[] temp = new byte[mappedBuffer.remaining()]; | ||
byteBuffer.get(temp); | ||
bufferList.get(index).put(temp); | ||
bufferList.get(index + 1).put(byteBuffer); | ||
} else { | ||
bufferList.get(index).put(byteBuffer); | ||
} | ||
} catch (Exception e) { | ||
throw new IOException("LargeMappedByteBuffer put rawPosition-"+rawPosition+" size-"+size, e); | ||
} | ||
} | ||
|
||
private int getIndex() { | ||
return (int) ((this.position - this.rawPosition) / Integer.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try { | ||
Class<?> clazz = Class.forName("sun.nio.ch.FileChannelImpl"); | ||
Method m = clazz.getDeclaredMethod("unmap", MappedByteBuffer.class); | ||
m.setAccessible(true); | ||
for (MappedByteBuffer mappedBuffer : bufferList) { | ||
m.invoke(clazz, mappedBuffer); | ||
} | ||
} catch (Exception e) { | ||
throw new IOException("LargeMappedByteBuffer close", e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters