-
-
Notifications
You must be signed in to change notification settings - Fork 967
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implementation of NioFile methods * Extracted Readable/WritableNioFile into separate classes ** Created SharedFileChannel to allow Readable/WritableNioFile for the same NioFile to use a single, shared FileChannel * Added tests for NioFile * Tests for Readable/WritableNioFile pending
- Loading branch information
1 parent
806e366
commit 39535d0
Showing
10 changed files
with
838 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
main/filesystem-nio/src/main/java/org/cryptomator/filesystem/nio/OpenMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.cryptomator.filesystem.nio; | ||
|
||
enum OpenMode { | ||
READ, WRITE | ||
} |
95 changes: 95 additions & 0 deletions
95
main/filesystem-nio/src/main/java/org/cryptomator/filesystem/nio/ReadableNioFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.cryptomator.filesystem.nio; | ||
|
||
import static java.lang.String.format; | ||
import static org.cryptomator.filesystem.nio.OpenMode.READ; | ||
|
||
import java.io.UncheckedIOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ClosedChannelException; | ||
|
||
import org.cryptomator.filesystem.ReadableFile; | ||
import org.cryptomator.filesystem.WritableFile; | ||
|
||
class ReadableNioFile implements ReadableFile { | ||
|
||
private final NioFile nioFile; | ||
|
||
private boolean open = true; | ||
private long position = 0; | ||
|
||
public ReadableNioFile(NioFile nioFile) { | ||
this.nioFile = nioFile; | ||
nioFile.channel().open(READ); | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer target) throws UncheckedIOException { | ||
assertOpen(); | ||
int read = nioFile.channel().readFully(position, target); | ||
if (read != SharedFileChannel.EOF) { | ||
position += read; | ||
} | ||
return read; | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return open; | ||
} | ||
|
||
@Override | ||
public void position(long position) throws UncheckedIOException { | ||
assertOpen(); | ||
this.position = position; | ||
} | ||
|
||
@Override | ||
public void copyTo(WritableFile other) throws UncheckedIOException { | ||
assertOpen(); | ||
if (belongsToSameFilesystem(other)) { | ||
internalCopyTo((WritableNioFile) other); | ||
} else { | ||
throw new IllegalArgumentException("Can only copy to a WritableFile from the same FileSystem"); | ||
} | ||
} | ||
|
||
private boolean belongsToSameFilesystem(WritableFile other) { | ||
return other instanceof WritableNioFile && ((WritableNioFile) other).nioFile().belongsToSameFilesystem(nioFile); | ||
} | ||
|
||
private void internalCopyTo(WritableNioFile target) { | ||
target.ensureChannelIsOpened(); | ||
SharedFileChannel targetChannel = target.channel(); | ||
targetChannel.truncate(0); | ||
long size = nioFile.channel().size(); | ||
long transferred = 0; | ||
while (transferred < size) { | ||
transferred += nioFile.channel().transferTo(transferred, size - transferred, targetChannel); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (!open) { | ||
return; | ||
} | ||
open = false; | ||
try { | ||
nioFile.channel().close(); | ||
} finally { | ||
nioFile.lock().readLock().unlock(); | ||
} | ||
} | ||
|
||
private void assertOpen() { | ||
if (!open) { | ||
throw new UncheckedIOException(format("%s already closed.", this), new ClosedChannelException()); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return format("Readable%s", nioFile); | ||
} | ||
|
||
} |
Oops, something went wrong.