提交 d86f495f 编写于 作者: E Eric Burke

TypedFile gets its length from the underlying file now

上级 0edb2af0
......@@ -4,6 +4,8 @@ package retrofit.io;
import junit.framework.TestCase;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/** @author Eric Burke (eric@squareup.com) */
public class TypedFileTest extends TestCase {
......@@ -29,4 +31,30 @@ public class TypedFileTest extends TestCase {
assertEquals(file.getAbsolutePath() + " (PNG)",
new TypedFile(file, MimeType.PNG).toString());
}
public void testLength() throws IOException {
File tempFile = File.createTempFile("foo", ".tmp");
try {
TypedFile typedFile = new TypedFile(tempFile, MimeType.PNG);
assertEquals("length", 0, typedFile.length());
writeToFile(tempFile, new byte[]{0, 1, 2, 3, 4});
assertEquals("file length", 5, tempFile.length());
assertEquals("typed file length", 5, typedFile.length());
} finally {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();
}
}
private void writeToFile(File file, byte[] data) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(data);
} finally {
fos.close();
}
}
}
\ No newline at end of file
......@@ -14,15 +14,13 @@ public abstract class AbstractTypedBytes implements TypedBytes, Serializable {
private static final long serialVersionUID = 0;
private final MimeType mimeType;
private final int length;
/**
* Stores the mime type.
*
* @throws NullPointerException if mimeType is null
*/
public AbstractTypedBytes(MimeType mimeType, int length) {
this.length = length;
public AbstractTypedBytes(MimeType mimeType) {
this.mimeType = Objects.nonNull(mimeType, "mimeType");
}
......@@ -30,7 +28,6 @@ public abstract class AbstractTypedBytes implements TypedBytes, Serializable {
return mimeType;
}
public int length() {
return length;
}
/** Returns the length in bytes. */
public abstract int length();
}
\ No newline at end of file
......@@ -23,7 +23,7 @@ public class TypedByteArray extends AbstractTypedBytes {
* @throws NullPointerException if bytes or mimeType is null
*/
public TypedByteArray(byte[] bytes, MimeType mimeType) {
super(mimeType, bytes.length);
super(mimeType);
this.bytes = Objects.nonNull(bytes, "bytes");
}
......@@ -31,6 +31,10 @@ public class TypedByteArray extends AbstractTypedBytes {
out.write(bytes);
}
@Override public int length() {
return bytes.length;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
......@@ -23,7 +23,7 @@ public class TypedFile extends AbstractTypedBytes {
* @throws NullPointerException if file or mimeType is null
*/
public TypedFile(File file, MimeType mimeType) {
super(mimeType, (int) file.length());
super(mimeType);
this.file = Objects.nonNull(file, "file");
}
......@@ -70,4 +70,8 @@ public class TypedFile extends AbstractTypedBytes {
public int hashCode() {
return file.hashCode();
}
@Override public int length() {
return (int) file.length();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册