From 5b645ea494f0e6869dda4136852832deb19972d1 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 4 May 2018 20:14:45 +0800 Subject: [PATCH] =?UTF-8?q?gzip=20=E8=BE=93=E5=85=A5=E6=B5=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../download/downloader/ConnectionHelp.java | 26 +++++++++++++++++-- .../downloader/HttpFileInfoThread.java | 23 ++++++++++++---- .../download/downloader/HttpThreadTask.java | 3 +-- .../com/arialyy/aria/orm/DelegateFind.java | 2 +- .../com/arialyy/aria/orm/DelegateUpdate.java | 17 +++++++----- .../com/arialyy/aria/util/CommonUtil.java | 21 +++++++++++++++ .../java/com/arialyy/aria/util/Regular.java | 2 +- DEV_LOG.md | 1 + .../arialyy/simple/test/AnyRunActivity.java | 7 +++-- .../arialyy/simple/test/AnyRunnModule.java | 4 +-- 10 files changed, 82 insertions(+), 24 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index f7a42a4f..0803249a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -15,19 +15,21 @@ */ 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.util.SSLContextUtil; import java.io.IOException; +import java.io.InputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; import java.net.URLConnection; import java.util.Set; +import java.util.zip.GZIPInputStream; +import java.util.zip.InflaterInputStream; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; -import org.apache.commons.net.ftp.FTPClient; /** * Created by lyy on 2017/1/18. @@ -35,6 +37,26 @@ import org.apache.commons.net.ftp.FTPClient; */ 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(); + } + } + /** * 处理链接 * diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index ab205ef1..f2ff72b4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -24,7 +24,9 @@ import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLDecoder; @@ -90,7 +92,7 @@ class HttpFileInfoThread implements Runnable { } } int code = conn.getResponseCode(); - boolean isComplete = false; + boolean end = false; if (TextUtils.isEmpty(mEntity.getMd5Code())) { String md5Code = conn.getHeaderField("Content-MD5"); mEntity.setMd5Code(md5Code); @@ -123,14 +125,25 @@ class HttpFileInfoThread implements Runnable { } mEntity.setFileSize(len); mTaskEntity.setSupportBP(true); - isComplete = true; + end = true; } 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; } mEntity.setFileSize(len); mTaskEntity.setSupportBP(false); - isComplete = true; + end = true; } else if (code == HttpURLConnection.HTTP_NOT_FOUND) { failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404", true); } else if (code == HttpURLConnection.HTTP_MOVED_TEMP @@ -143,7 +156,7 @@ class HttpFileInfoThread implements Runnable { } else { failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true); } - if (isComplete) { + if (end) { mTaskEntity.setChunked(isChunked); mTaskEntity.update(); if (onFileInfoListener != null) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index ba0b8eca..333dfff7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -74,8 +74,7 @@ final class HttpThreadTask extends AbsThreadTask clazz = dbEntity.getClass(); List fields = CommonUtil.getAllFields(clazz); - DbEntity cacheEntity = mDataCache.get(getCacheKey(dbEntity)); + //DbEntity cacheEntity = mDataCache.get(getCacheKey(dbEntity)); if (fields != null && fields.size() > 0) { ContentValues values = new ContentValues(); try { @@ -71,11 +71,14 @@ class DelegateUpdate extends AbsDelegate { if (isIgnore(dbEntity, field)) { continue; } - if (cacheEntity != null - && field.get(dbEntity) == field.get(cacheEntity) - && !field.getName().equals("state")) { //在LruCache中 state字段总是不能重新赋值... - continue; - } + //if (cacheEntity != null + // && field.get(dbEntity).equals(field.get(cacheEntity)) + // && !field.getName().equals("state")) { //在LruCache中 state字段总是不能重新赋值... + // Log.d(TAG, field.get(dbEntity) + ""); + // Log.d(TAG, field.get(cacheEntity) + ""); + // + // continue; + //} String value; Type type = field.getType(); if (type == Map.class && checkMap(field)) { @@ -98,7 +101,7 @@ class DelegateUpdate extends AbsDelegate { ALog.d(TAG, "没有数据更新"); } } - mDataCache.put(getCacheKey(dbEntity), dbEntity); + //mDataCache.put(getCacheKey(dbEntity), dbEntity); close(db); } diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 0ecd9869..96f06349 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -69,6 +69,27 @@ import java.util.regex.Pattern; public class 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的缓存目录 * diff --git a/Aria/src/main/java/com/arialyy/aria/util/Regular.java b/Aria/src/main/java/com/arialyy/aria/util/Regular.java index 17df97b2..829b5dda 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/Regular.java +++ b/Aria/src/main/java/com/arialyy/aria/util/Regular.java @@ -33,5 +33,5 @@ public interface Regular { /** * 匹配window.location.replace */ - String REG_WINLOD_REPLACE = "\\.*replace(\\.*)\\.*"; + String REG_WINLOD_REPLACE = "replace\\(\".*\"\\)"; } diff --git a/DEV_LOG.md b/DEV_LOG.md index 16642f17..ef418fb5 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -4,6 +4,7 @@ - 重构Aria的ORM模型 - 可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html) - window.location.replace("http://xxxx")类型的重定向支持 + - 适配gzip、deflate 压缩类型的输入流 + v_3.3.16 - 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200 - 优化target代码结构,移除路径被占用的提示 diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index 308b8f88..715952a3 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -3,7 +3,6 @@ package com.arialyy.simple.test; import android.os.Bundle; import android.view.View; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; @@ -18,7 +17,9 @@ public class AnyRunActivity extends BaseActivity { AnyRunnModule module; String[] urls; 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() { return R.layout.activity_test; @@ -30,7 +31,6 @@ public class AnyRunActivity extends BaseActivity { mBar.setVisibility(View.GONE); module = new AnyRunnModule(this); urls = getResources().getStringArray(R.array.group_urls); - } public void onClick(View view) { @@ -41,7 +41,6 @@ public class AnyRunActivity extends BaseActivity { // module.start(urls[index]); // index++; //} - //module.start("https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864"); module.start(URL); break; case R.id.stop: diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index 98ba7ce3..4b583b98 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -67,9 +67,9 @@ public class AnyRunnModule { mUrl = url; Aria.download(this) .load(url) - .addHeader("Accept-Encoding", "gzip, deflate") + .addHeader("Accept-Encoding", "gzip") .setRequestMode(RequestEnum.GET) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg123.apk") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1234.apk") .resetState() .start(); }