/
FileReceiver.java
268 lines (218 loc) · 7.74 KB
/
FileReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package io.github.mayubao.kuaichuan.core;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import io.github.mayubao.kuaichuan.core.entity.FileInfo;
import io.github.mayubao.kuaichuan.core.utils.FileUtils;
import io.github.mayubao.kuaichuan.core.utils.MLog;
import io.github.mayubao.kuaichuan.core.utils.TimeUtils;
/**
* Created by mayubao on 2016/11/10.
* Contact me 345269374@qq.com
*/
public class FileReceiver extends BaseTransfer implements Runnable {
private static final String TAG = FileReceiver.class.getSimpleName();
/**
* Socket的输入输出流
*/
private Socket mSocket;
private InputStream mInputStream;
/**
* 传送文件的信息
*/
private FileInfo mFileInfo;
/**
* 控制线程暂停 恢复
*/
private final Object LOCK = new Object();
boolean mIsPaused = false;
/**
* 文件接收的监听
*/
OnReceiveListener mOnReceiveListener;
public FileReceiver(Socket mSocket) {
this.mSocket = mSocket;
}
public void setOnReceiveListener(OnReceiveListener mOnReceiveListener) {
this.mOnReceiveListener = mOnReceiveListener;
}
@Override
public void run() {
//初始化
try {
if(mOnReceiveListener != null) mOnReceiveListener.onStart();
init();
} catch (Exception e) {
e.printStackTrace();
MLog.i(TAG, "FileReceiver init() --->>> occur expection");
if(mOnReceiveListener != null) mOnReceiveListener.onFailure(e, mFileInfo);
}
//解析头部
try {
parseHeader();
} catch (Exception e) {
e.printStackTrace();
MLog.i(TAG, "FileReceiver parseHeader() --->>> occur expection");
if(mOnReceiveListener != null) mOnReceiveListener.onFailure(e, mFileInfo);
}
//解析主体
try {
parseBody();
} catch (Exception e) {
e.printStackTrace();
MLog.i(TAG, "FileReceiver parseBody() --->>> occur expection");
if(mOnReceiveListener != null) mOnReceiveListener.onFailure(e, mFileInfo);
}
//结束
try {
finish();
} catch (Exception e) {
e.printStackTrace();
MLog.i(TAG, "FileReceiver finish() --->>> occur expection");
if(mOnReceiveListener != null) mOnReceiveListener.onFailure(e, mFileInfo);
}
}
@Override
public void init() throws Exception{
if(this.mSocket != null){
this.mInputStream = mSocket.getInputStream();
}
}
@Override
public void parseHeader() throws IOException {
MLog.i(TAG, "parseHeader######>>>start");
//Are you sure can read the 1024 byte accurately?
//读取header部分
byte[] headerBytes = new byte[BYTE_SIZE_HEADER];
int headTotal = 0;
int readByte = -1;
//开始读取header
while((readByte = mInputStream.read()) != -1){
headerBytes[headTotal] = (byte) readByte;
headTotal ++;
if(headTotal == headerBytes.length){
break;
}
}
MLog.i(TAG, "FileReceiver receive header size------>>>" + headTotal);
MLog.i(TAG, "FileReceiver receive header------>>>" + new String(headerBytes, UTF_8));
//读取缩略图部分
byte[] screenshotBytes = new byte[BYTE_SIZE_SCREENSHOT];
int screenshotTotal = 0;
int sreadByte = -1;
//开始读取缩略图
while((sreadByte = mInputStream.read()) != -1){
screenshotBytes[screenshotTotal] = (byte) sreadByte;
screenshotTotal ++;
if(screenshotTotal == screenshotBytes.length){
break;
}
}
Bitmap bitmap = BitmapFactory.decodeByteArray(screenshotBytes, 0, screenshotBytes.length);
if(mOnReceiveListener != null)mOnReceiveListener.onGetScreenshot(bitmap);
MLog.i(TAG, "FileReceiver receive screenshot size------>>>" + screenshotTotal);
// MLog.i(TAG, "FileReceiver receive screenshot------>>>" + new String(headerBytes, UTF_8));
//解析header
String jsonStr = new String(headerBytes, UTF_8);
String[] strArray = jsonStr.split(SPERATOR);
jsonStr = strArray[1].trim();
mFileInfo = FileInfo.toObject(jsonStr);
mFileInfo.setBitmap(bitmap);
if(mOnReceiveListener != null) mOnReceiveListener.onGetFileInfo(mFileInfo);
// String fileName = getFileName(fileInfo.getFilePath());
// int fileSize = fileInfo.getSize();
MLog.i(TAG, "parseHeader######>>>end");
}
@Override
public void parseBody() throws Exception {
MLog.i(TAG, "parseBody######>>>start");
//写入文件
long fileSize = mFileInfo.getSize();
OutputStream bos = new FileOutputStream(FileUtils.gerateLocalFile(mFileInfo.getFilePath()));
//记录文件开始写入时间
long startTime = System.currentTimeMillis();
byte[] bytes = new byte[BYTE_SIZE_DATA];
long total = 0;
int len = 0;
long sTime = System.currentTimeMillis();
long eTime = 0;
while((len=mInputStream.read(bytes)) != -1){
synchronized(LOCK) {
if (mIsPaused) {
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
bos.write(bytes, 0, len);
total = total + len;
eTime = System.currentTimeMillis();
if(eTime - sTime > 200) { //大于500ms 才进行一次监听
sTime = eTime;
if(mOnReceiveListener != null) mOnReceiveListener.onProgress(total, fileSize);
}
}
// bos.write(bytes, 0, len);
// total = total + len;
// if(mOnReceiveListener != null) mOnReceiveListener.onProgress(total, fileSize);
}
//记录文件结束写入时间
long endTime = System.currentTimeMillis();
MLog.i(TAG, "FileReceiver body receive######>>>" + (TimeUtils.formatTime(endTime - startTime)));
MLog.i(TAG, "FileReceiver body receive######>>>" + total);
MLog.i(TAG, "parseBody######>>>end");
if(mOnReceiveListener != null) mOnReceiveListener.onSuccess(mFileInfo);
}
@Override
public void finish() {
//TODO 实现一些资源的关闭
if(mInputStream != null){
try {
mInputStream.close();
} catch (IOException e) {
}
}
if(mSocket != null && mSocket.isConnected()){
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
MLog.i(TAG, "FileReceiver close socket######>>>");
}
/**
* 停止线程下载
*/
public void pause() {
synchronized(LOCK) {
mIsPaused = true;
LOCK.notifyAll();
}
}
/**
* 重新开始线程下载
*/
public void resume() {
synchronized(LOCK) {
mIsPaused = false;
LOCK.notifyAll();
}
}
/**
* 文件接收的监听
*/
public interface OnReceiveListener{
void onStart();
void onGetFileInfo(FileInfo fileInfo);
void onGetScreenshot(Bitmap bitmap);
void onProgress(long progress, long total);
void onSuccess(FileInfo fileInfo);
void onFailure(Throwable t, FileInfo fileInfo);
}
}