Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Merge branch 'v1.6.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Jan 6, 2018
2 parents 1c00ba1 + 25a4cf8 commit fe71df4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>lee.study</groupId>
<artifactId>proxyee-down</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>

<build>
<plugins>
Expand Down
42 changes: 22 additions & 20 deletions src/main/java/lee/study/down/ext/LargeMappedByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@
import java.nio.channels.FileChannel.MapMode;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.nio.ch.FileChannelImpl;

public class LargeMappedByteBuffer implements Closeable {

private static final Logger LOGGER = LoggerFactory.getLogger(LargeMappedByteBuffer.class);

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;
Expand All @@ -35,18 +32,22 @@ public LargeMappedByteBuffer(FileChannel fileChannel, MapMode mapMode, long posi
}
}

public final void put(ByteBuffer byteBuffer) {
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);
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);
}
}

Expand All @@ -57,13 +58,14 @@ private int getIndex() {
@Override
public void close() throws IOException {
try {
Method m = FileChannelImpl.class.getDeclaredMethod("unmap", MappedByteBuffer.class);
Class<?> clazz = Class.forName("sun.nio.ch.FileChannelImpl");
Method m = clazz.getDeclaredMethod("unmap", MappedByteBuffer.class);
m.setAccessible(true);
for (MappedByteBuffer mappedBuffer : bufferList) {
m.invoke(FileChannelImpl.class, mappedBuffer);
m.invoke(clazz, mappedBuffer);
}
} catch (Exception e) {
LOGGER.error("LargeMappedByteBuffer close:",e);
throw new IOException("LargeMappedByteBuffer close", e);
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/lee/study/down/hanndle/HttpDownInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
callback.onError(taskInfo, chunkInfo, cause);
if (cause instanceof IOException) {
HttpDownServer.LOGGER.debug(
"服务器响应异常重试:" + chunkInfo.getIndex() + "\t" + chunkInfo.getDownSize());
} else {
HttpDownServer.LOGGER.error("down onError:", cause);
}
}
Expand Down

0 comments on commit fe71df4

Please sign in to comment.