Added ReadableFile#size()

This commit is contained in:
Sebastian Stenzel
2016-01-09 16:39:14 +01:00
parent 0c42392244
commit fcf4476ae3
10 changed files with 53 additions and 6 deletions

View File

@@ -29,8 +29,16 @@ public interface ReadableFile extends ReadableByteChannel {
* if an {@link IOException} occurs while reading from this
* {@code ReadableBytes}
*/
@Override
int read(ByteBuffer target) throws UncheckedIOException;
/**
* @return The current size of the file. This value is a snapshot and might have been changed by concurrent modifications.
* @throws UncheckedIOException
* if an {@link IOException} occurs
*/
long size() throws UncheckedIOException;
/**
* <p>
* Fast-forwards or rewinds the file to the specified position.

View File

@@ -42,6 +42,11 @@ public class DelegatingReadableFile implements ReadableFile {
return delegate.read(target);
}
@Override
public long size() throws UncheckedIOException {
return delegate.size();
}
@Override
public void position(long position) throws UncheckedIOException {
delegate.position(position);

View File

@@ -61,6 +61,12 @@ class CryptoReadableFile implements ReadableFile {
}
}
@Override
public long size() throws UncheckedIOException {
assert decryptor != null : "decryptor is always being set during position(long)";
return decryptor.contentLength();
}
@Override
public void position(long position) throws UncheckedIOException {
if (readAheadTask != null) {

View File

@@ -109,6 +109,7 @@ class CryptoWritableFile implements WritableFile {
encryptor.append(FileContentCryptor.EOF);
writeTask.get();
writeHeader();
// TODO append padding
} catch (ExecutionException e) {
if (e.getCause() instanceof UncheckedIOException) {
throw (UncheckedIOException) e.getCause();

View File

@@ -55,6 +55,11 @@ class InMemoryReadableFile implements ReadableFile {
}
}
@Override
public long size() throws UncheckedIOException {
return contentGetter.get().limit();
}
@Override
public void position(long position) throws UncheckedIOException {
assert position < Integer.MAX_VALUE : "Can not use that big in-memory files.";

View File

@@ -103,6 +103,11 @@ public class InMemoryFileSystemTest {
}
Assert.assertTrue(fooFile.exists());
// check if size = 11 bytes
try (ReadableFile readable = fooFile.openReadable()) {
Assert.assertEquals(11, readable.size());
}
// copy foo to bar
File barFile = fs.file("bar.txt");
try (WritableFile writable = barFile.openWritable()) {

View File

@@ -45,6 +45,11 @@ class ReadableNioFile implements ReadableFile {
return open;
}
@Override
public long size() throws UncheckedIOException {
return channel.size();
}
@Override
public void position(long position) throws UncheckedIOException {
assertOpen();
@@ -73,7 +78,7 @@ class ReadableNioFile implements ReadableFile {
target.ensureChannelIsOpened();
SharedFileChannel targetChannel = target.channel();
targetChannel.truncate(0);
long size = channel.size();
long size = size();
long transferred = 0;
while (transferred < size) {
transferred += channel.transferTo(transferred, size - transferred, targetChannel, transferred);

View File

@@ -88,6 +88,16 @@ public class ReadableNioFileTest {
inTest.position(-1);
}
@Test
public void testSizeReturnsSizeOfChannel() {
long expectedSize = 85472;
when(channel.size()).thenReturn(expectedSize);
long actualSize = inTest.size();
assertThat(actualSize, is(expectedSize));
}
@Test
public void testReadDelegatesToChannelReadFullyWithZeroPositionIfNotSet() {
ByteBuffer buffer = mock(ByteBuffer.class);

View File

@@ -28,6 +28,8 @@ import org.cryptomator.filesystem.jackrabbit.FileLocator;
class DavFile extends DavNode<FileLocator> {
private static final int BUFFER_SIZE = 32 * 1024;
public DavFile(FilesystemResourceFactory factory, LockManager lockManager, DavSession session, FileLocator node) {
super(factory, lockManager, session, node);
}
@@ -44,9 +46,8 @@ class DavFile extends DavNode<FileLocator> {
return;
}
try (ReadableFile src = node.openReadable(); WritableByteChannel dst = Channels.newChannel(outputContext.getOutputStream())) {
// TODO filesize before sending content
outputContext.setContentLength(-1l);
ByteBuffer buf = ByteBuffer.allocate(1337);
outputContext.setContentLength(src.size());
ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
do {
buf.clear();
src.read(buf);

View File

@@ -40,6 +40,8 @@ import org.cryptomator.filesystem.jackrabbit.FolderLocator;
class DavFolder extends DavNode<FolderLocator> {
private static final int BUFFER_SIZE = 32 * 1024;
public DavFolder(FilesystemResourceFactory factory, LockManager lockManager, DavSession session, FolderLocator folder) {
super(factory, lockManager, session, folder);
properties.add(new ResourceType(ResourceType.COLLECTION));
@@ -73,7 +75,7 @@ class DavFolder extends DavNode<FolderLocator> {
private void addMemberFile(DavFile memberFile, InputStream inputStream) {
try (ReadableByteChannel src = Channels.newChannel(inputStream); WritableFile dst = node.file(memberFile.getDisplayName()).openWritable()) {
ByteBuffer buf = ByteBuffer.allocate(1337);
ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
while (src.read(buf) != -1) {
buf.flip();
dst.write(buf);
@@ -150,7 +152,6 @@ class DavFolder extends DavNode<FolderLocator> {
@Override
protected void setModificationTime(Instant instant) {
// TODO Auto-generated method stub
}
@Override