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:53:01 +01:00
parent 1804a52740
commit 356ea5c319
14 changed files with 155 additions and 131 deletions

View File

@@ -39,7 +39,7 @@ public class OpenFiles implements AutoCloseable {
}
static void cleanup(Collection<ReadableFile> readableFiles, Collection<WritableFile> writableFiles) {
Iterator<AutoCloseable> iterator = Stream.concat(readableFiles.stream(), writableFiles.stream()).iterator();
Iterator<? extends AutoCloseable> iterator = Stream.concat(readableFiles.stream(), writableFiles.stream()).iterator();
UncheckedIOException firstException = null;
while (iterator.hasNext()) {
AutoCloseable openFile = iterator.next();

View File

@@ -1,46 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
public interface ReadableBytes {
/**
* <p>
* Tries to fill the remaining space in the given byte buffer with data from
* this readable bytes from the current position.
* <p>
* May read less bytes if the end of this readable bytes has been reached.
*
* @param target
* the byte buffer to fill
* @throws UncheckedIOException
* if an {@link IOException} occurs while reading from this
* {@code ReadableBytes}
*/
void read(ByteBuffer target) throws UncheckedIOException;
/**
* <p>
* Tries to fill the remaining space in the given byte buffer with data from
* this readable bytes from the given position.
* <p>
* May read less bytes if the end of this readable bytes has been reached.
*
* @param target
* the byte buffer to fill
* @param position
* the position to read bytes from
* @throws UncheckedIOException
* if an {@link IOException} occurs while reading from this
* {@code ReadableBytes}
*/
void read(ByteBuffer target, long position) throws UncheckedIOException;
}

View File

@@ -5,12 +5,46 @@
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
public interface ReadableFile extends ReadableBytes, AutoCloseable {
public interface ReadableFile extends ReadableByteChannel {
void copyTo(WritableFile other) throws UncheckedIOException;
/**
* <p>
* Tries to fill the remaining space in the given byte buffer with data from
* this readable bytes from the current position.
* <p>
* May read less bytes if the end of this readable bytes has been reached.
*
* @param target
* the byte buffer to fill
* @return the number of bytes actually read, or {@code -1} if the end of
* file has been reached
* @throws UncheckedIOException
* if an {@link IOException} occurs while reading from this
* {@code ReadableBytes}
*/
int read(ByteBuffer target) throws UncheckedIOException;
/**
* <p>
* Fast-forwards or rewinds the file to the specified position.
* <p>
* Consecutive reads on the file will begin at the new position.
*
* @param position
* the position to set the file to
* @throws UncheckedIOException
* if an {@link IOException} occurs
*
*/
void position(long position) throws UncheckedIOException;
@Override
void close() throws UncheckedIOException;

View File

@@ -1,38 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
public interface WritableBytes {
/**
* Writes the data in the given byte buffer to this readable bytes at the
* current position.
*
* @param target
* the byte buffer to use
* @throws UncheckedIOException
* if an {@link IOException} occurs while writing
*/
void write(ByteBuffer source) throws UncheckedIOException;
/**
* Writes the data in the given byte buffer to this readable bytes at the
* given position, overwriting existing content (not inserting).
*
* @param target
* the byte buffer to use
* @param position
* the position to write the data to
* @throws UncheckedIOException
* if an {@link IOException} occurs while writing
*/
void write(ByteBuffer source, int position) throws UncheckedIOException;
}

View File

@@ -5,10 +5,13 @@
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.time.Instant;
public interface WritableFile extends WritableBytes, AutoCloseable {
public interface WritableFile extends WritableByteChannel {
void moveTo(WritableFile other) throws UncheckedIOException;
@@ -24,6 +27,37 @@ public interface WritableFile extends WritableBytes, AutoCloseable {
void truncate() throws UncheckedIOException;
/**
* Writes the data in the given byte buffer to this readable bytes at the
* current position.
*
* @param source
* the byte buffer to use
* @return the number of bytes written, always equal to
* {@code source.remaining()}
* @throws UncheckedIOException
* if an {@link IOException} occurs while writing
*/
int write(ByteBuffer source) throws UncheckedIOException;
/**
* <p>
* Fast-forwards or rewinds the file to the specified position.
* <p>
* Consecutive writes on the file will begin at the new position.
* <p>
* If the position is set to a value greater than the current end of file
* consecutive writes will write data to the given position. The value of
* all bytes between this position and the previous end of file will be
* unspecified.
*
* @param position
* the position to set the file to
* @throws UncheckedIOException
* if an {@link IOException} occurs
*/
void position(long position) throws UncheckedIOException;
/**
* <p>
* Closes this {@code WritableFile} which finally commits all operations