Skip to content

Commit

Permalink
前端文件文件选择器加上校验,调大bufferSize提升文件读写速度,重新支持32位系统
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Jan 15, 2018
1 parent 9a09b1d commit 98b7ef3
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 165 deletions.
72 changes: 0 additions & 72 deletions common/src/main/java/lee/study/down/io/LargeMappedByteBuffer.java

This file was deleted.

7 changes: 0 additions & 7 deletions common/src/main/java/lee/study/down/model/ChunkInfo.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package lee.study.down.model;

import io.netty.channel.Channel;
import java.io.Serializable;
import java.nio.channels.FileChannel;
import lee.study.down.io.LargeMappedByteBuffer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -24,8 +21,4 @@ public class ChunkInfo implements Serializable {
private long lastTime = 0;
private long pauseTime = 0;
private int status = 0;

private transient volatile Channel channel;
private transient volatile FileChannel fileChannel;
private transient volatile LargeMappedByteBuffer mappedBuffer;
}
15 changes: 14 additions & 1 deletion common/src/main/java/lee/study/down/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.file.Files;
import java.util.Stack;

Expand Down Expand Up @@ -145,6 +147,17 @@ public static File createDirSmart(String path) throws IOException {
return file;
}

public static void unmap(MappedByteBuffer mappedBuffer) throws IOException {
try {
Class<?> clazz = Class.forName("sun.nio.ch.FileChannelImpl");
Method m = clazz.getDeclaredMethod("unmap", MappedByteBuffer.class);
m.setAccessible(true);
m.invoke(clazz, mappedBuffer);
} catch (Exception e) {
throw new IOException("LargeMappedByteBuffer close", e);
}
}

public static void main(String[] args) throws Exception {
String path = "F:\\百度云合并下载研究\\test.txt";
RandomAccessFile raf2 = new RandomAccessFile(path, "rw");
Expand All @@ -154,7 +167,7 @@ public static void main(String[] args) throws Exception {
raf3.setLength(0);
raf3.close();
/*FileChannel fileChannel = new RandomAccessFile(path, "rw").getChannel();
MappedByteBuffer byteBuffer1 = fileChannel.map(MapMode.READ_WRITE,0,1000);
MappedByteBuf byteBuffer1 = fileChannel.map(MapMode.READ_WRITE,0,1000);
byteBuffer1.put(new byte[]{1,2,3,4,5});
byte[] bytes = new byte[5];
byteBuffer1.flip();
Expand Down
67 changes: 53 additions & 14 deletions core/src/main/java/lee/study/down/HttpDownBootstrap.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package lee.study.down;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultLastHttpContent;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.proxy.ProxyHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.resolver.NoopAddressResolverGroup;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import lee.study.down.constant.HttpDownStatus;
import lee.study.down.dispatch.HttpDownCallback;
import lee.study.down.handle.HttpDownInitializer;
Expand All @@ -20,24 +26,29 @@
import lee.study.down.model.HttpRequestInfo;
import lee.study.down.model.TaskInfo;
import lee.study.down.util.FileUtil;
import lee.study.down.util.HttpDownUtil;
import lee.study.proxyee.proxy.ProxyConfig;
import lee.study.proxyee.util.ProtoUtil.RequestProto;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Data
@AllArgsConstructor
@AllArgsConstructor()
public class HttpDownBootstrap {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpDownBootstrap.class);
public static final String ATTR_CHANNEL = "channel";
public static final String ATTR_FILE_CHANNEL = "fileChannel";
//tcp bufferSize最大为128K
public static final int BUFFER_SIZE = 1024 * 128;
private static final RecvByteBufAllocator RECV_BYTE_BUF_ALLOCATOR = new AdaptiveRecvByteBufAllocator(
64, BUFFER_SIZE, BUFFER_SIZE);

private HttpDownInfo httpDownInfo;
private SslContext clientSslContext;
private NioEventLoopGroup clientLoopGroup;
private HttpDownCallback callback;
private final Map<Integer, Map<String, Object>> attr = new HashMap<>();

public void startDown() throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
Expand Down Expand Up @@ -66,6 +77,8 @@ public void startChunkDown(ChunkInfo chunkInfo, int updateStatus) {
LOGGER.debug("开始下载:" + chunkInfo.getIndex() + "\t" + chunkInfo.getDownSize());
Bootstrap bootstrap = new Bootstrap()
.channel(NioSocketChannel.class)
.option(ChannelOption.RCVBUF_ALLOCATOR, RECV_BYTE_BUF_ALLOCATOR)
.option(ChannelOption.SO_RCVBUF, BUFFER_SIZE)
.group(clientLoopGroup)
.handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
if (httpDownInfo.getProxyConfig() != null) {
Expand Down Expand Up @@ -108,7 +121,7 @@ public void retryChunkDown(ChunkInfo chunkInfo, int updateStatus)
throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
synchronized (chunkInfo) {
closeChunk(chunkInfo);
close(chunkInfo);
//已经下载完成
if (chunkInfo.getDownSize() == chunkInfo.getTotalSize()) {
chunkInfo.setStatus(HttpDownStatus.DONE);
Expand Down Expand Up @@ -138,22 +151,22 @@ public void pauseDown() throws Exception {
taskInfo.setStatus(HttpDownStatus.PAUSE);
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) {
synchronized (chunkInfo) {
closeChunk(chunkInfo);
close(chunkInfo);
if (chunkInfo.getStatus() != HttpDownStatus.DONE) {
chunkInfo.setStatus(HttpDownStatus.PAUSE);
}
}
}
}
callback.onPause(getHttpDownInfo());
callback.onPause(httpDownInfo);
}

/**
* 继续下载
*/
public void continueDown()
throws Exception {
TaskInfo taskInfo = getHttpDownInfo().getTaskInfo();
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
synchronized (taskInfo) {
//如果文件被删除重新开始下载
if (!FileUtil.exists(taskInfo.buildTaskFilePath())) {
Expand All @@ -176,15 +189,23 @@ public void continueDown()
}
}
}
callback.onContinue(getHttpDownInfo());
callback.onContinue(httpDownInfo);
}

public void closeChunk(ChunkInfo chunkInfo) {
public void close(ChunkInfo chunkInfo) {
try {
HttpDownUtil.safeClose(chunkInfo.getChannel(), chunkInfo.getFileChannel(),
chunkInfo.getMappedBuffer());
FileChannel fileChannel = (FileChannel) getAttr(chunkInfo, ATTR_FILE_CHANNEL);
if (fileChannel != null) {
//关闭旧的下载文件连接
fileChannel.close();
}
Channel channel = (Channel) getAttr(chunkInfo, ATTR_CHANNEL);
if (channel != null) {
//关闭旧的下载连接
channel.close();
}
} catch (Exception e) {
LOGGER.error("closeChunk error", e);
LOGGER.error("close error", e);
}
}

Expand All @@ -193,9 +214,27 @@ public void close() {
synchronized (taskInfo) {
for (ChunkInfo chunkInfo : httpDownInfo.getTaskInfo().getChunkInfoList()) {
synchronized (chunkInfo) {
closeChunk(chunkInfo);
close(chunkInfo);
}
}
}
}

public void setAttr(ChunkInfo chunkInfo, String key, Object object) {
Map<String, Object> map = attr.get(chunkInfo.getIndex());
if (map == null) {
map = new HashMap<>();
attr.put(chunkInfo.getIndex(), map);
}
map.put(key, object);
}

public Object getAttr(ChunkInfo chunkInfo, String key) {
Map<String, Object> map = attr.get(chunkInfo.getIndex());
if (map == null) {
return null;
} else {
return map.get(key);
}
}
}
Loading

0 comments on commit 98b7ef3

Please sign in to comment.