Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
0001d069
R
retrofit
项目概览
wd1105040417
/
retrofit
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
retrofit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0001d069
编写于
10月 23, 2012
作者:
P
Patrick Forhan
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #67 from square/jw/floater
Remove `Fetcher`.
上级
e997c721
763186c8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
0 addition
and
306 deletion
+0
-306
http/src/main/java/retrofit/http/Fetcher.java
http/src/main/java/retrofit/http/Fetcher.java
+0
-132
http/src/main/java/retrofit/http/ProgressListener.java
http/src/main/java/retrofit/http/ProgressListener.java
+0
-17
http/src/test/java/retrofit/http/FetcherTest.java
http/src/test/java/retrofit/http/FetcherTest.java
+0
-157
未找到文件。
http/src/main/java/retrofit/http/Fetcher.java
已删除
100644 → 0
浏览文件 @
e997c721
// Copyright 2012 Square, Inc.
package
retrofit.http
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.methods.HttpGet
;
import
retrofit.io.ByteSink
;
import
javax.inject.Inject
;
import
javax.inject.Provider
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.lang.reflect.Type
;
import
java.text.DateFormat
;
import
java.util.Date
;
import
java.util.concurrent.Executor
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
static
retrofit
.
http
.
RestAdapter
.
DATE_FORMAT
;
/**
* Fetches URL contents to files.
*
* @author Bob Lee (bob@squareup.com)
*/
public
class
Fetcher
{
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
Fetcher
.
class
.
getName
());
// TODO: Support conditional get.
private
final
Provider
<
HttpClient
>
httpClientProvider
;
private
final
Executor
executor
;
private
final
MainThread
mainThread
;
@Inject
Fetcher
(
Provider
<
HttpClient
>
httpClientProvider
,
Executor
executor
,
MainThread
mainThread
)
{
this
.
httpClientProvider
=
httpClientProvider
;
this
.
executor
=
executor
;
this
.
mainThread
=
mainThread
;
}
/**
* Fetches the contents of a URL to a file.
*
* @param url to fetch
* @param destination for download
* @param callback to invoke in the UI thread once the download completes
* @param progressListener listens for progress, can be null
*/
public
void
fetch
(
final
String
url
,
final
ByteSink
.
Factory
destination
,
Callback
<
Void
>
callback
,
final
ProgressListener
progressListener
)
{
if
(
url
==
null
)
throw
new
NullPointerException
(
"url"
);
if
(
destination
==
null
)
throw
new
NullPointerException
(
"destination"
);
if
(
callback
==
null
)
throw
new
NullPointerException
(
"callback"
);
final
HttpGet
get
=
new
HttpGet
(
url
);
final
UiCallback
<
Void
>
uiCallback
=
new
UiCallback
<
Void
>(
callback
,
mainThread
);
executor
.
execute
(
new
Runnable
()
{
public
void
run
()
{
try
{
DownloadHandler
downloadHandler
=
new
DownloadHandler
(
url
,
destination
,
uiCallback
,
progressListener
,
mainThread
,
new
Date
(),
DATE_FORMAT
);
httpClientProvider
.
get
().
execute
(
get
,
downloadHandler
);
}
catch
(
IOException
e
)
{
LOGGER
.
log
(
Level
.
WARNING
,
"fetch exception"
,
e
);
uiCallback
.
networkError
();
}
catch
(
Throwable
t
)
{
uiCallback
.
unexpectedError
(
t
);
}
}
});
}
static
class
DownloadHandler
extends
CallbackResponseHandler
<
Void
>
{
/** Throttles how often we send progress updates. Specified in %. */
private
static
final
int
PROGRESS_GRANULARITY
=
5
;
private
final
ByteSink
.
Factory
destination
;
private
final
ProgressListener
progressListener
;
private
final
MainThread
mainThread
;
private
final
ProgressUpdate
progressUpdate
=
new
ProgressUpdate
();
DownloadHandler
(
String
url
,
ByteSink
.
Factory
destination
,
UiCallback
<
Void
>
callback
,
ProgressListener
progressListener
,
MainThread
mainThread
,
Date
start
,
ThreadLocal
<
DateFormat
>
dateFormat
)
{
super
(
callback
,
null
,
null
,
url
,
start
,
dateFormat
);
this
.
destination
=
destination
;
this
.
progressListener
=
progressListener
;
this
.
mainThread
=
mainThread
;
}
@Override
protected
Object
parse
(
HttpEntity
entity
,
Type
type
)
throws
ConversionException
{
try
{
// Save the result to the sink instead of returning it.
InputStream
in
=
entity
.
getContent
();
try
{
ByteSink
out
=
destination
.
newSink
();
final
int
total
=
(
int
)
entity
.
getContentLength
();
int
totalRead
=
0
;
try
{
byte
[]
buffer
=
new
byte
[
4096
];
int
read
;
while
((
read
=
in
.
read
(
buffer
))
!=
-
1
)
{
out
.
write
(
buffer
,
read
);
if
(
progressListener
!=
null
)
{
totalRead
+=
read
;
int
percent
=
totalRead
*
100
/
total
;
if
(
percent
-
progressUpdate
.
percent
>
PROGRESS_GRANULARITY
)
{
progressUpdate
.
percent
=
percent
;
mainThread
.
execute
(
progressUpdate
);
}
}
}
}
finally
{
out
.
close
();
}
}
finally
{
in
.
close
();
}
}
catch
(
IOException
e
)
{
throw
new
ConversionException
(
e
);
}
return
null
;
}
/** Invokes ProgressListener in UI thread. */
private
class
ProgressUpdate
implements
Runnable
{
private
volatile
int
percent
;
public
void
run
()
{
progressListener
.
hearProgress
(
percent
);
}
}
}
}
http/src/main/java/retrofit/http/ProgressListener.java
已删除
100644 → 0
浏览文件 @
e997c721
// Copyright 2010 Square, Inc.
package
retrofit.http
;
/**
* Listens for progress.
*
* @author Bob Lee (bob@squareup.com)
*/
public
interface
ProgressListener
{
/**
* Hears a progress update.
*
* @param percent 0-100
*/
void
hearProgress
(
int
percent
);
}
http/src/test/java/retrofit/http/FetcherTest.java
已删除
100644 → 0
浏览文件 @
e997c721
// Copyright 2010 Square, Inc.
package
retrofit.http
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.StatusLine
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.ResponseHandler
;
import
org.apache.http.client.methods.HttpUriRequest
;
import
org.junit.Test
;
import
retrofit.io.ByteSink
;
import
javax.inject.Provider
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Arrays
;
import
java.util.concurrent.Executor
;
import
static
org
.
easymock
.
EasyMock
.
anyInt
;
import
static
org
.
easymock
.
EasyMock
.
createMock
;
import
static
org
.
easymock
.
EasyMock
.
expect
;
import
static
org
.
easymock
.
EasyMock
.
expectLastCall
;
import
static
org
.
easymock
.
EasyMock
.
replay
;
import
static
org
.
easymock
.
EasyMock
.
verify
;
import
static
org
.
fest
.
assertions
.
Assertions
.
assertThat
;
/**
* Fetcher test cases.
*
* @author Bob Lee (bob@squareup.com)
*/
public
class
FetcherTest
{
private
static
final
String
URL
=
"http://crazybob.org/"
;
private
ByteSink
.
Factory
sinkFactory
=
createMock
(
ByteSink
.
Factory
.
class
);
private
StatusLine
statusLine
=
createMock
(
StatusLine
.
class
);
private
HttpEntity
entity
=
createMock
(
HttpEntity
.
class
);
private
HttpResponse
response
=
createMock
(
HttpResponse
.
class
);
@SuppressWarnings
(
"unchecked"
)
private
Callback
<
Void
>
callback
=
createMock
(
Callback
.
class
);
private
ProgressListener
progressListener
=
createMock
(
ProgressListener
.
class
);
@Test
public
void
testSuccessfulFetch
()
throws
Exception
{
DummyInputStream
in
=
new
DummyInputStream
();
final
SingletonHttpClient
httpClient
=
new
SingletonHttpClient
(
response
);
DummyExecutor
executor
=
new
DummyExecutor
();
DummyMainThread
uiThread
=
new
DummyMainThread
();
ArraySink
sink
=
new
ArraySink
();
expect
(
response
.
getStatusLine
()).
andReturn
(
statusLine
);
expect
(
statusLine
.
getStatusCode
()).
andReturn
(
200
);
expect
(
response
.
getEntity
()).
andReturn
(
entity
);
expect
(
entity
.
getContentLength
()).
andReturn
(
3L
);
expect
(
entity
.
getContent
()).
andReturn
(
in
);
expect
(
sinkFactory
.
newSink
()).
andReturn
(
sink
);
progressListener
.
hearProgress
(
anyInt
());
expectLastCall
().
atLeastOnce
();
callback
.
call
(
null
);
replayAll
();
Fetcher
fetcher
=
new
Fetcher
(
new
Provider
<
HttpClient
>()
{
public
HttpClient
get
()
{
return
httpClient
;
}
},
executor
,
uiThread
);
fetcher
.
fetch
(
URL
,
sinkFactory
,
callback
,
progressListener
);
verifyAll
();
assertThat
(
executor
.
calls
).
isEqualTo
(
1
);
assertThat
(
uiThread
.
calls
).
isGreaterThan
(
1
);
// result + progress updates
assertThat
(
httpClient
.
calls
).
isEqualTo
(
1
);
assertThat
(
sink
.
toArray
()).
isEqualTo
(
new
byte
[]
{
1
,
2
,
3
});
}
private
void
replayAll
()
{
replay
(
sinkFactory
,
statusLine
,
entity
,
response
,
callback
,
progressListener
);
}
private
void
verifyAll
()
{
verify
(
sinkFactory
,
statusLine
,
entity
,
response
,
callback
,
progressListener
);
}
static
class
ArraySink
implements
ByteSink
{
ByteArrayOutputStream
bout
=
new
ByteArrayOutputStream
();
boolean
closed
;
public
void
write
(
byte
[]
buffer
,
int
count
)
throws
IOException
{
bout
.
write
(
buffer
,
0
,
count
);
}
public
void
close
()
throws
IOException
{
closed
=
true
;
}
public
byte
[]
toArray
()
{
return
bout
.
toByteArray
();
}
}
/** Returns { 1, 2, 3 }, 1 byte at a time. */
static
class
DummyInputStream
extends
InputStream
{
int
count
;
@Override
public
int
read
(
byte
[]
b
)
throws
IOException
{
if
(
count
++
==
3
)
return
-
1
;
Arrays
.
fill
(
b
,
0
,
1
,
(
byte
)
count
);
return
1
;
}
@Override
public
int
read
()
throws
IOException
{
throw
new
UnsupportedOperationException
();
}
}
static
class
DummyExecutor
implements
Executor
{
int
calls
;
public
void
execute
(
Runnable
command
)
{
calls
++;
command
.
run
();
}
}
static
class
DummyMainThread
extends
DummyExecutor
implements
MainThread
{
public
void
executeDelayed
(
Runnable
r
,
long
delay
)
{
throw
new
UnsupportedOperationException
();
}
public
void
executeSynchronously
(
Runnable
r
)
{
throw
new
UnsupportedOperationException
();
}
@Override
public
void
executeOnMain
(
Runnable
r
)
{
throw
new
UnsupportedOperationException
();
}
public
void
cancel
(
Runnable
r
)
{
throw
new
UnsupportedOperationException
();
}
}
/** An HttpClient that returns one response. */
private
class
SingletonHttpClient
extends
DummyHttpClient
{
private
final
HttpResponse
response
;
public
SingletonHttpClient
(
HttpResponse
response
)
{
this
.
response
=
response
;
}
int
calls
;
public
<
T
>
T
execute
(
HttpUriRequest
request
,
ResponseHandler
<?
extends
T
>
responseHandler
)
throws
IOException
{
calls
++;
assertThat
(
request
.
getURI
().
toString
()).
isEqualTo
(
FetcherTest
.
URL
);
T
t
=
responseHandler
.
handleResponse
(
response
);
assertThat
(
t
).
isNull
();
return
t
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录