gzip 输入流支持

pull/330/head
laoyuyu 7 years ago
parent ed369db0f7
commit 5b645ea494
  1. 26
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java
  2. 23
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  3. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  4. 2
      Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java
  5. 17
      Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java
  6. 21
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  7. 2
      Aria/src/main/java/com/arialyy/aria/util/Regular.java
  8. 1
      DEV_LOG.md
  9. 7
      app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java
  10. 4
      app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java

@ -15,19 +15,21 @@
*/ */
package com.arialyy.aria.core.download.downloader; package com.arialyy.aria.core.download.downloader;
import android.util.Log; import android.text.TextUtils;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.util.SSLContextUtil; import com.arialyy.aria.util.SSLContextUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.ProtocolException; import java.net.ProtocolException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Set; import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.net.ftp.FTPClient;
/** /**
* Created by lyy on 2017/1/18. * Created by lyy on 2017/1/18.
@ -35,6 +37,26 @@ import org.apache.commons.net.ftp.FTPClient;
*/ */
class ConnectionHelp { class ConnectionHelp {
/**
* 转换HttpUrlConnect的inputStream流
*
* @return {@link GZIPInputStream}{@link InflaterInputStream}
* @throws IOException
*/
static InputStream convertInputStream(HttpURLConnection connection) throws IOException {
String encoding = connection.getContentEncoding();
if (TextUtils.isEmpty(encoding)) {
return connection.getInputStream();
}
if (encoding.contains("gzip")) {
return new GZIPInputStream(connection.getInputStream());
} else if (encoding.contains("deflate")) {
return new InflaterInputStream(connection.getInputStream());
} else {
return connection.getInputStream();
}
}
/** /**
* 处理链接 * 处理链接
* *

@ -24,7 +24,9 @@ import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
@ -90,7 +92,7 @@ class HttpFileInfoThread implements Runnable {
} }
} }
int code = conn.getResponseCode(); int code = conn.getResponseCode();
boolean isComplete = false; boolean end = false;
if (TextUtils.isEmpty(mEntity.getMd5Code())) { if (TextUtils.isEmpty(mEntity.getMd5Code())) {
String md5Code = conn.getHeaderField("Content-MD5"); String md5Code = conn.getHeaderField("Content-MD5");
mEntity.setMd5Code(md5Code); mEntity.setMd5Code(md5Code);
@ -123,14 +125,25 @@ class HttpFileInfoThread implements Runnable {
} }
mEntity.setFileSize(len); mEntity.setFileSize(len);
mTaskEntity.setSupportBP(true); mTaskEntity.setSupportBP(true);
isComplete = true; end = true;
} else if (code == HttpURLConnection.HTTP_OK) { } else if (code == HttpURLConnection.HTTP_OK) {
if (!checkLen(len) && !isChunked) { if (conn.getHeaderField("Content-Type").equals("text/html")) {
BufferedReader reader =
new BufferedReader(new InputStreamReader(ConnectionHelp.convertInputStream(conn)));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
handle302Turn(conn, CommonUtil.getWindowReplaceUrl(sb.toString()));
return;
} else if (!checkLen(len) && !isChunked) {
return; return;
} }
mEntity.setFileSize(len); mEntity.setFileSize(len);
mTaskEntity.setSupportBP(false); mTaskEntity.setSupportBP(false);
isComplete = true; end = true;
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) { } else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404", true); failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404", true);
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP } else if (code == HttpURLConnection.HTTP_MOVED_TEMP
@ -143,7 +156,7 @@ class HttpFileInfoThread implements Runnable {
} else { } else {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true); failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true);
} }
if (isComplete) { if (end) {
mTaskEntity.setChunked(isChunked); mTaskEntity.setChunked(isChunked);
mTaskEntity.update(); mTaskEntity.update();
if (onFileInfoListener != null) { if (onFileInfoListener != null) {

@ -74,8 +74,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
conn.setConnectTimeout(STATE.CONNECT_TIME_OUT); conn.setConnectTimeout(STATE.CONNECT_TIME_OUT);
conn.setReadTimeout(STATE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数 conn.setReadTimeout(STATE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数
//is = conn.getInputStream(); is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
is = new BufferedInputStream(conn.getInputStream());
//创建可设置位置的文件 //创建可设置位置的文件
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
//设置每条线程写入文件的位置 //设置每条线程写入文件的位置

@ -394,7 +394,7 @@ class DelegateFind extends AbsDelegate {
//当设置了主键,而且主键的类型为integer时,查询RowID等于主键 //当设置了主键,而且主键的类型为integer时,查询RowID等于主键
entity.rowID = cursor.getInt( entity.rowID = cursor.getInt(
cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName)); cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName));
mDataCache.put(getCacheKey(entity), entity); //mDataCache.put(getCacheKey(entity), entity);
entitys.add(entity); entitys.add(entity);
} }
closeCursor(cursor); closeCursor(cursor);

@ -62,7 +62,7 @@ class DelegateUpdate extends AbsDelegate {
db = checkDb(db); db = checkDb(db);
Class<?> clazz = dbEntity.getClass(); Class<?> clazz = dbEntity.getClass();
List<Field> fields = CommonUtil.getAllFields(clazz); List<Field> fields = CommonUtil.getAllFields(clazz);
DbEntity cacheEntity = mDataCache.get(getCacheKey(dbEntity)); //DbEntity cacheEntity = mDataCache.get(getCacheKey(dbEntity));
if (fields != null && fields.size() > 0) { if (fields != null && fields.size() > 0) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
try { try {
@ -71,11 +71,14 @@ class DelegateUpdate extends AbsDelegate {
if (isIgnore(dbEntity, field)) { if (isIgnore(dbEntity, field)) {
continue; continue;
} }
if (cacheEntity != null //if (cacheEntity != null
&& field.get(dbEntity) == field.get(cacheEntity) // && field.get(dbEntity).equals(field.get(cacheEntity))
&& !field.getName().equals("state")) { //在LruCache中 state字段总是不能重新赋值... // && !field.getName().equals("state")) { //在LruCache中 state字段总是不能重新赋值...
continue; // Log.d(TAG, field.get(dbEntity) + "");
} // Log.d(TAG, field.get(cacheEntity) + "");
//
// continue;
//}
String value; String value;
Type type = field.getType(); Type type = field.getType();
if (type == Map.class && checkMap(field)) { if (type == Map.class && checkMap(field)) {
@ -98,7 +101,7 @@ class DelegateUpdate extends AbsDelegate {
ALog.d(TAG, "没有数据更新"); ALog.d(TAG, "没有数据更新");
} }
} }
mDataCache.put(getCacheKey(dbEntity), dbEntity); //mDataCache.put(getCacheKey(dbEntity), dbEntity);
close(db); close(db);
} }

@ -69,6 +69,27 @@ import java.util.regex.Pattern;
public class CommonUtil { public class CommonUtil {
private static final String TAG = "CommonUtil"; private static final String TAG = "CommonUtil";
/**
* 拦截window.location.replace数据
*
* @return 重定向url
*/
public static String getWindowReplaceUrl(String text) {
if (TextUtils.isEmpty(text)) {
ALog.e(TAG, "拦截数据为null");
return null;
}
String reg = Regular.REG_WINLOD_REPLACE;
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(text);
if (m.find()){
String s = m.group();
s = s.substring(9, s.length() - 2);
return s;
}
return null;
}
/** /**
* 获取sdcard app的缓存目录 * 获取sdcard app的缓存目录
* *

@ -33,5 +33,5 @@ public interface Regular {
/** /**
* 匹配window.location.replace * 匹配window.location.replace
*/ */
String REG_WINLOD_REPLACE = "\\.*replace(\\.*)\\.*"; String REG_WINLOD_REPLACE = "replace\\(\".*\"\\)";
} }

@ -4,6 +4,7 @@
- 重构Aria的ORM模型 - 重构Aria的ORM模型
- 可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html) - 可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html)
- window.location.replace("http://xxxx")类型的重定向支持 - window.location.replace("http://xxxx")类型的重定向支持
- 适配gzip、deflate 压缩类型的输入流
+ v_3.3.16 + v_3.3.16
- 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200 - 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200
- 优化target代码结构,移除路径被占用的提示 - 优化target代码结构,移除路径被占用的提示

@ -3,7 +3,6 @@ package com.arialyy.simple.test;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
@ -18,7 +17,9 @@ public class AnyRunActivity extends BaseActivity<ActivityTestBinding> {
AnyRunnModule module; AnyRunnModule module;
String[] urls; String[] urls;
int index = 0; int index = 0;
String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk";
String URL =
"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864";
@Override protected int setLayoutId() { @Override protected int setLayoutId() {
return R.layout.activity_test; return R.layout.activity_test;
@ -30,7 +31,6 @@ public class AnyRunActivity extends BaseActivity<ActivityTestBinding> {
mBar.setVisibility(View.GONE); mBar.setVisibility(View.GONE);
module = new AnyRunnModule(this); module = new AnyRunnModule(this);
urls = getResources().getStringArray(R.array.group_urls); urls = getResources().getStringArray(R.array.group_urls);
} }
public void onClick(View view) { public void onClick(View view) {
@ -41,7 +41,6 @@ public class AnyRunActivity extends BaseActivity<ActivityTestBinding> {
// module.start(urls[index]); // module.start(urls[index]);
// index++; // index++;
//} //}
//module.start("https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864");
module.start(URL); module.start(URL);
break; break;
case R.id.stop: case R.id.stop:

@ -67,9 +67,9 @@ public class AnyRunnModule {
mUrl = url; mUrl = url;
Aria.download(this) Aria.download(this)
.load(url) .load(url)
.addHeader("Accept-Encoding", "gzip, deflate") .addHeader("Accept-Encoding", "gzip")
.setRequestMode(RequestEnum.GET) .setRequestMode(RequestEnum.GET)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg123.apk") .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1234.apk")
.resetState() .resetState()
.start(); .start();
} }

Loading…
Cancel
Save