Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
e93b0a9c
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e93b0a9c
编写于
9月 09, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Close streams in OkHttp and HttpUrl fetchers.
上级
2bd356be
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
14 deletion
+60
-14
integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp/OkHttpStreamFetcher.java
...umptech/glide/integration/okhttp/OkHttpStreamFetcher.java
+15
-2
library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherTest.java
...java/com/bumptech/glide/load/data/HttpUrlFetcherTest.java
+27
-4
library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
...ain/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
+18
-8
未找到文件。
integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp/OkHttpStreamFetcher.java
浏览文件 @
e93b0a9c
...
...
@@ -6,6 +6,7 @@ import com.bumptech.glide.load.model.GlideUrl;
import
com.squareup.okhttp.OkHttpClient
;
import
com.squareup.okhttp.Request
;
import
java.io.IOException
;
import
java.io.InputStream
;
/**
...
...
@@ -15,6 +16,7 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private
final
OkHttpClient
client
;
private
final
GlideUrl
url
;
private
volatile
Request
request
;
private
InputStream
stream
;
public
OkHttpStreamFetcher
(
OkHttpClient
client
,
GlideUrl
url
)
{
this
.
client
=
client
;
...
...
@@ -27,12 +29,23 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
.
url
(
url
.
toString
())
.
build
();
return
client
.
newCall
(
request
).
execute
().
body
().
byteStream
();
stream
=
client
.
newCall
(
request
)
.
execute
()
.
body
()
.
byteStream
();
return
stream
;
}
@Override
public
void
cleanup
()
{
// Do nothing.
if
(
stream
==
null
)
{
return
;
}
try
{
stream
.
close
();
}
catch
(
IOException
e
)
{
// Ignored
}
}
@Override
...
...
library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherTest.java
浏览文件 @
e93b0a9c
...
...
@@ -2,9 +2,9 @@ package com.bumptech.glide.load.data;
import
com.bumptech.glide.Priority
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.InOrder
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
...
...
@@ -15,6 +15,7 @@ import java.net.URL;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
inOrder
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
verify
;
...
...
@@ -24,16 +25,20 @@ public class HttpUrlFetcherTest {
private
HttpURLConnection
urlConnection
;
private
HttpUrlFetcher
fetcher
;
private
GlideUrl
glideUrl
;
private
InputStream
stream
;
@Before
public
void
setUp
()
throws
IOException
{
urlConnection
=
mock
(
HttpURLConnection
.
class
);
URL
url
=
new
URL
(
"http://www.google.com"
);
HttpUrlFetcher
.
HttpUrlConnectionFactory
factory
=
mock
(
HttpUrlFetcher
.
HttpUrlConnectionFactory
.
class
);
when
(
factory
.
build
(
eq
(
url
))).
thenReturn
(
urlConnection
);
HttpUrlFetcher
.
HttpUrlConnectionFactory
connectionFactory
=
mock
(
HttpUrlFetcher
.
HttpUrlConnectionFactory
.
class
);
when
(
connectionFactory
.
build
(
eq
(
url
))).
thenReturn
(
urlConnection
);
glideUrl
=
mock
(
GlideUrl
.
class
);
when
(
glideUrl
.
toURL
()).
thenReturn
(
url
);
fetcher
=
new
HttpUrlFetcher
(
glideUrl
,
factory
);
fetcher
=
new
HttpUrlFetcher
(
glideUrl
,
connectionFactory
);
stream
=
mock
(
InputStream
.
class
);
when
(
urlConnection
.
getInputStream
()).
thenReturn
(
stream
);
when
(
urlConnection
.
getResponseCode
()).
thenReturn
(
200
);
}
...
...
@@ -90,4 +95,22 @@ public class HttpUrlFetcherTest {
verify
(
urlConnection
,
never
()).
disconnect
();
}
@Test
public
void
testClosesStreamInCleanupIfNotNull
()
throws
Exception
{
fetcher
.
loadData
(
Priority
.
HIGH
);
fetcher
.
cleanup
();
verify
(
stream
).
close
();
}
@Test
public
void
testClosesStreamBeforeDisconnectingConnection
()
throws
Exception
{
fetcher
.
loadData
(
Priority
.
NORMAL
);
fetcher
.
cleanup
();
InOrder
order
=
inOrder
(
stream
,
urlConnection
);
order
.
verify
(
stream
).
close
();
order
.
verify
(
urlConnection
).
disconnect
();
}
}
\ No newline at end of file
library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
浏览文件 @
e93b0a9c
...
...
@@ -13,20 +13,23 @@ import java.net.URL;
*/
public
class
HttpUrlFetcher
implements
DataFetcher
<
InputStream
>
{
private
static
final
int
MAXIMUM_REDIRECTS
=
5
;
private
static
final
HttpUrlConnectionFactory
DEFAULT_FACTORY
=
new
DefaultHttpUrlConnectionFactory
();
private
static
final
HttpUrlConnectionFactory
DEFAULT_
CONNECTION_
FACTORY
=
new
DefaultHttpUrlConnectionFactory
();
private
final
HttpUrlConnectionFactory
factory
;
private
final
GlideUrl
glideUrl
;
private
final
HttpUrlConnectionFactory
connectionFactory
;
private
HttpURLConnection
urlConnection
;
private
InputStream
stream
;
private
volatile
boolean
isCancelled
;
public
HttpUrlFetcher
(
GlideUrl
glideUrl
)
{
this
(
glideUrl
,
DEFAULT_FACTORY
);
this
(
glideUrl
,
DEFAULT_
CONNECTION_
FACTORY
);
}
HttpUrlFetcher
(
GlideUrl
glideUrl
,
HttpUrlConnectionFactory
factory
)
{
// Visible for testing.
HttpUrlFetcher
(
GlideUrl
glideUrl
,
HttpUrlConnectionFactory
connectionFactory
)
{
this
.
glideUrl
=
glideUrl
;
this
.
factory
=
f
actory
;
this
.
connectionFactory
=
connectionF
actory
;
}
@Override
...
...
@@ -40,7 +43,7 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
}
else
if
(
url
.
equals
(
lastUrl
))
{
throw
new
IOException
(
"In re-direct loop"
);
}
urlConnection
=
f
actory
.
build
(
url
);
urlConnection
=
connectionF
actory
.
build
(
url
);
urlConnection
.
setConnectTimeout
(
2500
);
urlConnection
.
setReadTimeout
(
2500
);
urlConnection
.
setUseCaches
(
false
);
...
...
@@ -53,7 +56,8 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
}
final
int
statusCode
=
urlConnection
.
getResponseCode
();
if
(
statusCode
/
100
==
2
)
{
return
urlConnection
.
getInputStream
();
stream
=
urlConnection
.
getInputStream
();
return
stream
;
}
else
if
(
statusCode
/
100
==
3
)
{
String
redirectUrlString
=
urlConnection
.
getHeaderField
(
"Location"
);
URL
redirectUrl
=
new
URL
(
redirectUrlString
);
...
...
@@ -68,6 +72,13 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
@Override
public
void
cleanup
()
{
if
(
stream
!=
null
)
{
try
{
stream
.
close
();
}
catch
(
IOException
e
)
{
// Ignore
}
}
if
(
urlConnection
!=
null
)
{
urlConnection
.
disconnect
();
}
...
...
@@ -95,5 +106,4 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
return
(
HttpURLConnection
)
url
.
openConnection
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录