Refactored filesystem api

* Removed Readable- and WritableBytes
** Replaced with Readable-/WritableByteChannel
** Methods now integrated in Readable- and WritableFile
** Replaced positioned read/write by method to set the position
This commit is contained in:
Markus Kreusch
2015-12-28 20:54:18 +01:00
parent 1804a52740
commit 356ea5c319
14 changed files with 155 additions and 131 deletions
@@ -52,31 +52,24 @@ class InMemoryFile extends InMemoryNode implements File, ReadableFile, WritableF
}
@Override
public void read(ByteBuffer target) {
ByteBuffers.copy(content, target);
}
@Override
public void read(ByteBuffer target, long position) {
if (position > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Can not keep files of virtually unlimited size in memory.");
}
public void position(long position) throws UncheckedIOException {
content.position((int) position);
ByteBuffers.copy(content, target);
}
@Override
public void write(ByteBuffer source) {
this.write(source, content.position());
public int read(ByteBuffer target) {
return ByteBuffers.copy(content, target);
}
@Override
public void write(ByteBuffer source, int position) {
public int write(ByteBuffer source) {
assert content != null;
expandContentCapacityIfRequired(position + source.remaining());
content.position(position);
final int initialContentPosition = content.position();
expandContentCapacityIfRequired(initialContentPosition + source.remaining());
content.position(initialContentPosition);
assert content.remaining() >= source.remaining();
content.put(source);
return content.position() - initialContentPosition;
}
private void expandContentCapacityIfRequired(int requiredCapacity) {
@@ -121,7 +114,12 @@ class InMemoryFile extends InMemoryNode implements File, ReadableFile, WritableF
// returning null removes the entry.
return null;
});
assert!this.exists();
assert !this.exists();
}
@Override
public boolean isOpen() {
return lock.isWriteLockedByCurrentThread() || lock.getReadHoldCount() > 0;
}
@Override
@@ -136,7 +136,8 @@ public class InMemoryFileSystemTest {
}
final ByteBuffer readBuf = ByteBuffer.allocate(5);
try (ReadableFile readable = bazFile.openReadable()) {
readable.read(readBuf, 6);
readable.position(6);
readable.read(readBuf);
}
Assert.assertEquals("world", new String(readBuf.array()));
}
@@ -159,7 +160,8 @@ public class InMemoryFileSystemTest {
Assert.assertTrue(test1File.exists());
Assert.assertTrue(test2File.exists());
// copy foo/bar/ to qwe/asd/ (result is qwe/asd/file1.txt & qwe/asd/file2.txt)
// copy foo/bar/ to qwe/asd/ (result is qwe/asd/file1.txt &
// qwe/asd/file2.txt)
fooBarFolder.copyTo(qweAsdFolder);
Assert.assertTrue(qweAsdFolder.exists());
Assert.assertEquals(2, qweAsdFolder.files().count());