Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
db439021
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,体验更适合开发者的 AI 搜索 >>
提交
db439021
编写于
10月 29, 2012
作者:
J
Jason Holmes
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #88 from square/jw/files-flies
Inline usage of Files helper class.
上级
f9217726
cba0d3f9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
11 addition
and
140 deletion
+11
-140
io/src/main/java/retrofit/io/Files.java
io/src/main/java/retrofit/io/Files.java
+0
-91
io/src/main/java/retrofit/io/TypedFile.java
io/src/main/java/retrofit/io/TypedFile.java
+11
-1
io/src/test/java/retrofit/io/FilesTest.java
io/src/test/java/retrofit/io/FilesTest.java
+0
-48
未找到文件。
io/src/main/java/retrofit/io/Files.java
已删除
100644 → 0
浏览文件 @
f9217726
// Copyright 2010 Square, Inc.
package
retrofit.io
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
/**
* File utilities.
*
* @author Bob Lee (bob@squareup.com)
*/
public
class
Files
{
/**
* Copies input stream to given file. Closes input stream when finished.
*/
public
static
void
copy
(
InputStream
in
,
File
file
)
throws
IOException
{
byte
[]
buffer
=
new
byte
[
4096
];
try
{
FileOutputStream
out
=
new
FileOutputStream
(
file
);
try
{
int
read
;
while
((
read
=
in
.
read
(
buffer
))
>
-
1
)
out
.
write
(
buffer
,
0
,
read
);
out
.
getFD
().
sync
();
}
finally
{
out
.
close
();
}
}
finally
{
in
.
close
();
}
}
/**
* Copies file to given output stream.
*/
public
static
void
copy
(
File
file
,
OutputStream
out
)
throws
IOException
{
byte
[]
buffer
=
new
byte
[
4096
];
FileInputStream
in
=
new
FileInputStream
(
file
);
try
{
int
read
;
while
((
read
=
in
.
read
(
buffer
))
>=
0
)
out
.
write
(
buffer
,
0
,
read
);
}
finally
{
in
.
close
();
}
}
/**
* Create the indicated directory, if it doesn't already exist.
*
* @throws IllegalStateException if there is an error creating the directory.
* @throws IllegalArgumentException if param represents a file instead
* of a directory.
*/
public
static
void
makeDirectory
(
File
directory
)
{
if
(!
directory
.
exists
())
{
if
(!
directory
.
mkdirs
())
{
throw
new
IllegalStateException
(
"Error creating "
+
directory
+
"."
);
}
}
else
{
if
(!
directory
.
isDirectory
())
{
throw
new
IllegalArgumentException
(
"File "
+
directory
+
" is not a directory"
);
}
}
}
/**
* Build a File object from the given parts, appending each path part to
* the preceding part.
*/
public
static
File
build
(
File
baseFile
,
String
...
parts
)
{
File
file
=
baseFile
;
for
(
String
part
:
parts
)
file
=
new
File
(
file
,
part
);
return
file
;
}
/**
* Delete the given file, returning <code>true</code> if the file is gone
* (that is, if the delete succeeds, or was never there in the first place).
* A return value of <code>false</code> indicates that the delete failed.
*/
public
static
boolean
delete
(
File
file
)
{
if
(
file
==
null
)
{
throw
new
IllegalArgumentException
(
"Cannot delete a null file."
);
}
return
!
file
.
exists
()
||
file
.
delete
();
}
}
\ No newline at end of file
io/src/main/java/retrofit/io/TypedFile.java
浏览文件 @
db439021
...
...
@@ -2,6 +2,7 @@
package
retrofit.io
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
...
...
@@ -32,7 +33,16 @@ public class TypedFile extends AbstractTypedBytes {
}
public
void
writeTo
(
OutputStream
out
)
throws
IOException
{
Files
.
copy
(
file
,
out
);
byte
[]
buffer
=
new
byte
[
4096
];
FileInputStream
in
=
new
FileInputStream
(
file
);
try
{
int
read
;
while
((
read
=
in
.
read
(
buffer
))
!=
-
1
)
{
out
.
write
(
buffer
,
0
,
read
);
}
}
finally
{
in
.
close
();
}
}
/**
...
...
io/src/test/java/retrofit/io/FilesTest.java
已删除
100644 → 0
浏览文件 @
f9217726
// Copyright 2011 Square, Inc.
package
retrofit.io
;
import
org.junit.Test
;
import
java.io.File
;
import
java.io.FileWriter
;
import
java.io.PrintWriter
;
import
static
org
.
fest
.
assertions
.
api
.
Assertions
.
assertThat
;
import
static
org
.
fest
.
assertions
.
api
.
Fail
.
fail
;
/**
* @author Paul Hawke (psh@squareup.com)
*/
public
class
FilesTest
{
@Test
public
void
testDelete
()
throws
Exception
{
File
tmpFile
=
File
.
createTempFile
(
"prefix"
,
".tmp"
);
PrintWriter
pw
=
new
PrintWriter
(
new
FileWriter
(
tmpFile
));
pw
.
println
(
"content"
);
pw
.
close
();
assertThat
(
tmpFile
).
exists
();
assertThat
(
Files
.
delete
(
tmpFile
)).
isTrue
();
assertThat
(
tmpFile
).
doesNotExist
();
}
@Test
public
void
testDeleteFileThatDoesntExist
()
throws
Exception
{
File
tmpFile
=
File
.
createTempFile
(
"foobar"
,
".tmp"
);
assertThat
(
tmpFile
.
delete
()).
as
(
"unable to delete temporary file"
).
isTrue
();
assertThat
(
tmpFile
).
doesNotExist
();
assertThat
(
Files
.
delete
(
tmpFile
)).
isTrue
();
assertThat
(
tmpFile
).
doesNotExist
();
}
@Test
public
void
testDeleteNullFile
()
throws
Exception
{
try
{
Files
.
delete
(
null
);
fail
(
"Expected an IAE"
);
}
catch
(
IllegalArgumentException
e
)
{
// expect this
}
catch
(
Throwable
t
)
{
t
.
printStackTrace
();
fail
(
"Expected an IAE"
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录