Skip to content

Commit 44cd4a4

Browse files
committed
同步
by cp profeta
1 parent 3f8c303 commit 44cd4a4

File tree

4 files changed

+320
-1
lines changed

4 files changed

+320
-1
lines changed

CommonClass/src/org/cp/javadate/WorkDay.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class WorkDay {
2323
private int initSort = 9;// 初始化 工作 序列号 jingjing
2424
// private int initSort = 1;// 初始化 工作 序列号 lulu
2525

26-
private String target = "2019-10-29";// 目标日期
26+
private String target = "2020-02-20";// 目标日期
2727

2828
private int showNextDay = 120;//显示目标及接下来的7天的工作安排
2929

@@ -34,6 +34,7 @@ public class WorkDay {
3434
specialDay.put(LocalDate.parse("2019-12-24"), "\uD83C\uDF4E");//苹果
3535
specialDay.put(LocalDate.parse("2019-12-25"), "\uD83C\uDF84");//圣诞树
3636
specialDay.put(LocalDate.parse("2020-01-01"), "\uD83E\uDD5A");//鸡蛋
37+
specialDay.put(LocalDate.parse("2020-01-21"), "\uD83D\uDCAF");//100
3738
specialDay.put(LocalDate.parse("2020-01-24"), "\uD83C\uDF19");//月亮
3839
specialDay.put(LocalDate.parse("2020-01-25"), "☀");//太阳
3940
specialDay.put(LocalDate.parse("2020-02-14"), "❤");//爱心
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.cp.constructerReference;
2+
3+
import org.junit.Test;
4+
5+
import java.util.function.Function;
6+
import java.util.function.Supplier;
7+
8+
/**
9+
* 数组的构造器引用
10+
* create by CP on 2019/11/5 0005.
11+
*/
12+
public class ArrayRefTest {
13+
14+
@Test
15+
public void test1() {
16+
Supplier<String[]> supplier = () -> new String[1];
17+
System.out.println(supplier.get());
18+
19+
//没有无参构造器, 编译不通过
20+
// Supplier<String[]> supplier1 = String[]::new;
21+
}
22+
23+
@Test
24+
public void test2() {
25+
Function<Integer, String[]> function = capacity -> new String[capacity];
26+
System.out.println(function.apply(1));
27+
}
28+
29+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.profeta.cp.springbootquick.Entity;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* create by CP on 2019/11/29 0029.
9+
*/
10+
public class M3U8 {
11+
private String basepath;
12+
private List<Ts> tsList = new ArrayList<>();
13+
private long startTime;// 开始时间
14+
private long endTime;// 结束时间
15+
private long startDownloadTime;// 开始下载时间
16+
private long endDownloadTime;// 结束下载时间
17+
18+
public String getBasepath() {
19+
return basepath;
20+
}
21+
22+
public void setBasepath(String basepath) {
23+
this.basepath = basepath;
24+
}
25+
26+
public List<Ts> getTsList() {
27+
return tsList;
28+
}
29+
30+
public void setTsList(List<Ts> tsList) {
31+
this.tsList = tsList;
32+
}
33+
34+
public void addTs(Ts ts) {
35+
this.tsList.add(ts);
36+
}
37+
38+
public long getStartDownloadTime() {
39+
return startDownloadTime;
40+
}
41+
42+
public void setStartDownloadTime(long startDownloadTime) {
43+
this.startDownloadTime = startDownloadTime;
44+
}
45+
46+
public long getEndDownloadTime() {
47+
return endDownloadTime;
48+
}
49+
50+
public void setEndDownloadTime(long endDownloadTime) {
51+
this.endDownloadTime = endDownloadTime;
52+
}
53+
54+
/**
55+
* 获取开始时间
56+
*
57+
* @return
58+
*/
59+
public long getStartTime() {
60+
if (tsList.size() > 0) {
61+
Collections.sort(tsList);
62+
startTime = tsList.get(0).getLongDate();
63+
return startTime;
64+
}
65+
return 0;
66+
}
67+
68+
/**
69+
* 获取结束时间(加上了最后一段时间的持续时间)
70+
*
71+
* @return
72+
*/
73+
public long getEndTime() {
74+
if (tsList.size() > 0) {
75+
Ts m3U8Ts = tsList.get(tsList.size() - 1);
76+
endTime = m3U8Ts.getLongDate() + (long) (m3U8Ts.getSeconds() * 1000);
77+
return endTime;
78+
}
79+
return 0;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
StringBuilder sb = new StringBuilder();
85+
sb.append("basepath: " + basepath);
86+
for (Ts ts : tsList) {
87+
sb.append("\nts_file_name = " + ts);
88+
}
89+
sb.append("\n\nstartTime = " + startTime);
90+
sb.append("\n\nendTime = " + endTime);
91+
sb.append("\n\nstartDownloadTime = " + startDownloadTime);
92+
sb.append("\n\nendDownloadTime = " + endDownloadTime);
93+
return sb.toString();
94+
}
95+
public static class Ts implements Comparable<Ts> {
96+
private String file;
97+
private float seconds;
98+
99+
public Ts(String file, float seconds) {
100+
this.file = file;
101+
this.seconds = seconds;
102+
}
103+
104+
public String getFile() {
105+
return file;
106+
}
107+
108+
public void setFile(String file) {
109+
this.file = file;
110+
}
111+
112+
public float getSeconds() {
113+
return seconds;
114+
}
115+
116+
public void setSeconds(float seconds) {
117+
this.seconds = seconds;
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return file + " (" + seconds + "sec)";
123+
}
124+
125+
/**
126+
* 获取时间
127+
*/
128+
public long getLongDate() {
129+
try {
130+
return Long.parseLong(file.substring(0, file.lastIndexOf(".")));
131+
} catch (Exception e) {
132+
return 0;
133+
}
134+
}
135+
136+
@Override
137+
public int compareTo(Ts o) {
138+
return file.compareTo(o.file);
139+
}
140+
}
141+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.profeta.cp.springbootquick;
2+
3+
import com.profeta.cp.springbootquick.Entity.M3U8;
4+
5+
import java.io.*;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
import java.nio.channels.FileChannel;
9+
10+
/**
11+
* create by CP on 2019/11/29 0029.
12+
*/
13+
public class M3U8Test {
14+
public static String TEMP_DIR = "D:/temp";
15+
public static int connTimeout = 30 * 60 * 1000;
16+
public static int readTimeout = 30 * 60 * 1000;
17+
// public static String s1 = "https://tbm-auth.alicdn.com/e99361edd833010b/SW1B47blwBUniE5xbZf/1rvaBXimya5X0cUZ8M0_235188466048___sd.m3u8?auth_key=1574992603-0-0-016fa6a1de20e59a0bb6e9650c71b583";
18+
public static String s1 = "http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8";
19+
20+
public static void main(String[] args) {
21+
File tfile = new File(TEMP_DIR);
22+
if (!tfile.exists()) {
23+
tfile.mkdirs();
24+
}
25+
26+
M3U8 m3u8ByURL = getM3U8ByURL(s1);
27+
String basePath = m3u8ByURL.getBasepath();
28+
m3u8ByURL.getTsList().stream().parallel().forEach(m3U8Ts -> {
29+
File file = new File(TEMP_DIR + File.separator + m3U8Ts.getFile());
30+
if (!file.exists()) {// 下载过的就不管了
31+
FileOutputStream fos = null;
32+
InputStream inputStream = null;
33+
try {
34+
URL url = new URL(basePath + m3U8Ts.getFile());
35+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
36+
conn.setConnectTimeout(connTimeout);
37+
conn.setReadTimeout(readTimeout);
38+
if (conn.getResponseCode() == 200) {
39+
inputStream = conn.getInputStream();
40+
fos = new FileOutputStream(file);// 会自动创建文件
41+
int len = 0;
42+
byte[] buf = new byte[1024];
43+
while ((len = inputStream.read(buf)) != -1) {
44+
fos.write(buf, 0, len);// 写入流中
45+
}
46+
}
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
} finally {// 关流
50+
try {
51+
if (inputStream != null) {
52+
inputStream.close();
53+
}
54+
if (fos != null) {
55+
fos.close();
56+
}
57+
} catch (IOException e) {e.printStackTrace();}
58+
}
59+
}
60+
});
61+
System.out.println("文件下载完毕!");
62+
mergeFiles(tfile.listFiles(), "test.ts");
63+
}
64+
public static M3U8 getM3U8ByURL(String m3u8URL) {
65+
try {
66+
HttpURLConnection conn = (HttpURLConnection) new URL(m3u8URL).openConnection();
67+
if (conn.getResponseCode() == 200) {
68+
String realUrl = conn.getURL().toString();
69+
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
70+
String basepath = realUrl.substring(0, realUrl.lastIndexOf("/") + 1);
71+
M3U8 ret = new M3U8();
72+
ret.setBasepath(basepath);
73+
74+
String line;
75+
float seconds = 0;
76+
int mIndex;
77+
while ((line = reader.readLine()) != null) {
78+
if (line.startsWith("#")) {
79+
if (line.startsWith("#EXTINF:")) {
80+
line = line.substring(8);
81+
if ((mIndex = line.indexOf(",")) != -1) {
82+
line = line.substring(0, mIndex + 1);
83+
}
84+
try {
85+
seconds = Float.parseFloat(line);
86+
} catch (Exception e) {
87+
seconds = 0;
88+
}
89+
}
90+
continue;
91+
}
92+
if (line.endsWith("m3u8")) {
93+
return getM3U8ByURL(basepath + line);
94+
}
95+
ret.addTs(new M3U8.Ts(line, seconds));
96+
seconds = 0;
97+
}
98+
reader.close();
99+
100+
return ret;
101+
}
102+
} catch (IOException e) {
103+
// TODO Auto-generated catch block
104+
e.printStackTrace();
105+
}
106+
return null;
107+
}
108+
public static boolean mergeFiles(File[] fpaths, String resultPath) {
109+
if (fpaths == null || fpaths.length < 1) {
110+
return false;
111+
}
112+
113+
if (fpaths.length == 1) {
114+
return fpaths[0].renameTo(new File(resultPath));
115+
}
116+
for (int i = 0; i < fpaths.length; i++) {
117+
if (!fpaths[i].exists() || !fpaths[i].isFile()) {
118+
return false;
119+
}
120+
}
121+
File resultFile = new File(resultPath);
122+
123+
try {
124+
FileOutputStream fs = new FileOutputStream(resultFile, true);
125+
FileChannel resultFileChannel = fs.getChannel();
126+
FileInputStream tfs;
127+
for (int i = 0; i < fpaths.length; i++) {
128+
tfs = new FileInputStream(fpaths[i]);
129+
FileChannel blk = tfs.getChannel();
130+
resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size());
131+
tfs.close();
132+
blk.close();
133+
}
134+
fs.close();
135+
resultFileChannel.close();
136+
} catch (Exception e) {
137+
e.printStackTrace();
138+
return false;
139+
}
140+
141+
// for (int i = 0; i < fpaths.length; i ++) {
142+
// fpaths[i].delete();
143+
// }
144+
145+
return true;
146+
}
147+
148+
}

0 commit comments

Comments
 (0)