提交 db439021 编写于 作者: J Jason Holmes

Merge pull request #88 from square/jw/files-flies

Inline usage of Files helper class.
// 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
......@@ -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();
}
}
/**
......
// 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.
先完成此消息的编辑!
想要评论请 注册