Skip to content

Commit

Permalink
v2.0 init
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Jan 10, 2018
1 parent 25a4cf8 commit b4ce23e
Show file tree
Hide file tree
Showing 120 changed files with 1,680 additions and 1,656 deletions.
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pom.properties
hs_err_pid*

.DS_Store
view/node_modules/
view/dist/
*/view/node_modules/
*/view/dist/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand All @@ -33,5 +33,8 @@ yarn-error.log*
*.sln

# vue.js build
src/main/resources/view/
target/
*/src/main/resources/view/
*/target/
target/

.idea/libraries/
11 changes: 9 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions common/common.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: lee.study:proxyee:1.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-all:4.1.15.Final" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcpkix-jdk15on:1.58" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.58" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.16.18" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
</component>
</module>
15 changes: 15 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>proxyee-down</artifactId>
<groupId>lee.study</groupId>
<version>2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>proxyee-down-common</artifactId>
<packaging>jar</packaging>

</project>
37 changes: 37 additions & 0 deletions common/src/main/java/lee/study/down/constant/HttpDownStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lee.study.down.constant;

public class HttpDownStatus {

/**
* 等待下载
*/
public final static int WAIT = 0;
/**
* 正常发起的连接
*/
public final static int CONNECTING_NORMAL = 1;
/**
* 失败后发起的连接
*/
public final static int CONNECTING_FAIL = 2;
/**
* 继续下载发起的链接
*/
public final static int CONNECTING_CONTINUE = 3;
/**
* 下载中
*/
public final static int RUNNING = 4;
/**
* 暂停下载
*/
public final static int PAUSE = 5;
/**
* 下载失败
*/
public final static int FAIL = 6;
/**
* 下载完成
*/
public final static int DONE = 7;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lee.study.down.ext;
package lee.study.down.io;

import java.io.File;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lee.study.down.ext;
package lee.study.down.io;

import java.io.Closeable;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package lee.study.down.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.netty.channel.Channel;
import java.io.Serializable;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import lee.study.down.ext.LargeMappedByteBuffer;
import lee.study.down.io.LargeMappedByteBuffer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChunkInfo implements Serializable {

private static final long serialVersionUID = 231649750985696846L;
Expand All @@ -22,16 +22,11 @@ public class ChunkInfo implements Serializable {
private long totalSize;
private long startTime = 0;
private long lastTime = 0;
private long usedTime = 0;
private long pauseTime = 0;
private int status = 0; //0.待下载 //1.下载中 2.下载完成 3.下载失败 4.下载暂停
private int status = 0;

@JsonIgnore
private transient volatile Channel channel;
@JsonIgnore
private transient volatile FileChannel fileChannel;
@JsonIgnore
private transient volatile LargeMappedByteBuffer mappedBuffer;

public ChunkInfo() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,32 @@ public class TaskInfo implements Serializable {
private String id;
private String filePath;
private String fileName;
private int connections = 1;
private int connections;
private long totalSize;
private boolean supportRange;
private long downSize;
private long startTime;
private long lastTime;
private long startTime = 0;
private long lastTime = 0;
private long usedTime = 0;
private long pauseTime = 0;
private int status; //0.待下载 1.下载中 2.下载完成 3.下载失败 4.下载暂停
private int status;
private List<ChunkInfo> chunkInfoList;

public String buildTaskFilePath() {
return getFilePath() + File.separator + getFileName();
}

public String buildTaskRecordFilePath() {
return getFilePath() + File.separator + "." + getFileName() + ".inf";
}

public void reset() {
startTime = lastTime = usedTime = pauseTime = 0;
chunkInfoList.forEach((chunkInfo) -> {
chunkInfo.setStartTime(0);
chunkInfo.setLastTime(0);
chunkInfo.setUsedTime(0);
chunkInfo.setPauseTime(0);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.netty.buffer.ByteBuf;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -86,11 +85,16 @@ public static byte[] objToBts(Serializable object) throws IOException {
}
}

public static void serialize(Serializable object, String path) throws IOException {
try (
RandomAccessFile raf = new RandomAccessFile(path, "rw")
) {
raf.setLength(0);
public static void serialize(Serializable object, String path, boolean isHidden)
throws IOException {
if (FileUtil.exists(path)) {
try (
RandomAccessFile raf = new RandomAccessFile(path, "rw")
) {
raf.setLength(0);
}
} else {
FileUtil.createFile(path, isHidden);
}
try (
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(path))
Expand All @@ -99,6 +103,11 @@ public static void serialize(Serializable object, String path) throws IOExceptio
}
}

public static void serialize(Serializable object, String path)
throws IOException {
serialize(object, path, true);
}

public static Object deserialize(String path) throws IOException, ClassNotFoundException {
try (
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))
Expand Down Expand Up @@ -178,31 +187,6 @@ public static String readJsContent(InputStream inputStream) {
return sb.toString();
}

public static void insertContent(String path, long offset, byte[] content) throws IOException {
RandomAccessFile sourceRaf = new RandomAccessFile(path, "rw");
File tempFile = FileUtil.createFile(path + ".tmp");
RandomAccessFile tempRaf = new RandomAccessFile(tempFile, "rw");
long fileSize = sourceRaf.length();
FileChannel sourceChannel = sourceRaf.getChannel();
FileChannel targetChannel = tempRaf.getChannel();
long remaining = fileSize - offset;
long position = offset;
while (remaining > 0) {
long transferred = sourceChannel.transferTo(position, remaining, targetChannel);
remaining -= transferred;
position += transferred;
}
sourceChannel.truncate(offset);
sourceRaf.seek(offset);
sourceRaf.write(content);
long newOffset = sourceRaf.getFilePointer();
targetChannel.position(0);
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
sourceChannel.close();
targetChannel.close();
FileUtil.deleteIfExists(tempFile);
}

/**
* 查找buffer中一段字节数的位置
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,40 @@

public class FileUtil {

/**
* 文件或目录是否存在
*/
public static boolean exists(String path) {
return new File(path).exists();
}

/**
* 删除文件或文件夹
*/
public static void deleteIfExists(File file) {
public static void deleteIfExists(File file) throws IOException {
if (file.exists()) {
if (file.isFile()) {
file.delete();
if (!file.delete()) {
throw new IOException("Delete file failure,path:" + file.getAbsolutePath());
}
} else {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File temp : files) {
deleteIfExists(temp);
}
}
file.delete();
if (!file.delete()) {
throw new IOException("Delete file failure,path:" + file.getAbsolutePath());
}
}
}
}

/**
* 删除文件或文件夹
*/
public static void deleteIfExists(String path) {
public static void deleteIfExists(String path) throws IOException {
deleteIfExists(new File(path));
}

Expand Down
Loading

0 comments on commit b4ce23e

Please sign in to comment.