From db8b45d9924867522fd095c169b911b848972d2b Mon Sep 17 00:00:00 2001 From: Colin Decker Date: Thu, 3 Oct 2013 21:57:05 -0400 Subject: A few minor changes. --- .../com/google/jimfs/internal/CopyOptions.java | 14 ++-- .../com/google/jimfs/internal/DirectoryEntry.java | 3 +- .../google/jimfs/internal/JimfsFileChannel.java | 90 ++++------------------ .../com/google/jimfs/internal/OpenOptions.java | 31 +++----- 4 files changed, 33 insertions(+), 105 deletions(-) (limited to 'jimfs/src/main') diff --git a/jimfs/src/main/java/com/google/jimfs/internal/CopyOptions.java b/jimfs/src/main/java/com/google/jimfs/internal/CopyOptions.java index bba1d2f..be70a51 100644 --- a/jimfs/src/main/java/com/google/jimfs/internal/CopyOptions.java +++ b/jimfs/src/main/java/com/google/jimfs/internal/CopyOptions.java @@ -62,16 +62,12 @@ final class CopyOptions extends LinkOptions { } private final boolean move; - private final boolean copyAttributes; - private final boolean atomicMove; - private final boolean replaceExisting; + private final ImmutableSet options; private CopyOptions(boolean move, Set options) { super(move ? ImmutableSet.of(LinkOption.NOFOLLOW_LINKS) : options); this.move = move; - this.copyAttributes = options.contains(COPY_ATTRIBUTES); - this.atomicMove = options.contains(ATOMIC_MOVE); - this.replaceExisting = options.contains(REPLACE_EXISTING); + this.options = ImmutableSet.copyOf(options); } /** @@ -92,20 +88,20 @@ final class CopyOptions extends LinkOptions { * Returns whether or not attributes should be copied as well. */ public boolean isCopyAttributes() { - return copyAttributes; + return options.contains(COPY_ATTRIBUTES); } /** * Returns whether or not a move must be an atomic move. */ public boolean isAtomicMove() { - return atomicMove; + return options.contains(ATOMIC_MOVE); } /** * Returns whether or not an existing file at the destination should be replaced. */ public boolean isReplaceExisting() { - return replaceExisting; + return options.contains(REPLACE_EXISTING); } } diff --git a/jimfs/src/main/java/com/google/jimfs/internal/DirectoryEntry.java b/jimfs/src/main/java/com/google/jimfs/internal/DirectoryEntry.java index d455b8b..64547ef 100644 --- a/jimfs/src/main/java/com/google/jimfs/internal/DirectoryEntry.java +++ b/jimfs/src/main/java/com/google/jimfs/internal/DirectoryEntry.java @@ -76,7 +76,8 @@ final class DirectoryEntry { * @return this * @throws FileAlreadyExistsException if this entry does not exist */ - public DirectoryEntry requireDoesNotExist(Path pathForException) throws FileAlreadyExistsException { + public DirectoryEntry requireDoesNotExist( + Path pathForException) throws FileAlreadyExistsException { if (exists()) { throw new FileAlreadyExistsException(pathForException.toString()); } diff --git a/jimfs/src/main/java/com/google/jimfs/internal/JimfsFileChannel.java b/jimfs/src/main/java/com/google/jimfs/internal/JimfsFileChannel.java index cfa28aa..914d337 100644 --- a/jimfs/src/main/java/com/google/jimfs/internal/JimfsFileChannel.java +++ b/jimfs/src/main/java/com/google/jimfs/internal/JimfsFileChannel.java @@ -37,8 +37,6 @@ import java.nio.channels.WritableByteChannel; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; import javax.annotation.Nullable; @@ -62,11 +60,6 @@ final class JimfsFileChannel extends FileChannel { @Nullable private volatile Thread blockingThread; - /** - * Lock enforcing one thread at a time for operations on the channel. - */ - private final Lock lock = new ReentrantLock(); - private final File file; private final ByteStore store; @@ -144,8 +137,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkReadable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -174,8 +166,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -189,8 +179,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkReadable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -219,8 +208,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -229,8 +216,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkWritable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -260,8 +246,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -275,8 +259,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkWritable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -306,8 +289,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -315,11 +296,8 @@ final class JimfsFileChannel extends FileChannel { public long position() throws IOException { checkOpen(); - lock.lock(); - try { + synchronized (this) { return position; - } finally { - lock.unlock(); } } @@ -328,25 +306,17 @@ final class JimfsFileChannel extends FileChannel { checkNotNegative(newPosition, "newPosition"); checkOpen(); - lock.lock(); - try { + synchronized (this) { this.position = (int) newPosition; - return this; - } finally { - lock.unlock(); } + + return this; } @Override public long size() throws IOException { checkOpen(); - - lock.lock(); - try { - return store.sizeInBytes(); - } finally { - lock.unlock(); - } + return store.sizeInBytes(); } @Override @@ -355,8 +325,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkWritable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -385,20 +354,13 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @Override public void force(boolean metaData) throws IOException { - lock.lock(); - try { - checkOpen(); - // do nothing... writes are all synchronous anyway - } finally { - lock.unlock(); - } + checkOpen(); + // do nothing... writes are all synchronous anyway } @Override @@ -409,8 +371,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkReadable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -435,8 +396,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -448,8 +407,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkWritable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -483,8 +441,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -495,8 +451,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkReadable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -521,8 +476,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -533,8 +486,7 @@ final class JimfsFileChannel extends FileChannel { checkOpen(); checkWritable(); - lock.lock(); - try { + synchronized (this) { boolean completed = false; try { beginBlocking(); @@ -568,8 +520,6 @@ final class JimfsFileChannel extends FileChannel { // if InterruptedException is caught, endBlocking will throw ClosedByInterruptException throw new AssertionError(); - } finally { - lock.unlock(); } } @@ -593,13 +543,7 @@ final class JimfsFileChannel extends FileChannel { checkWritable(); } - lock.lock(); - try { - checkOpen(); - return new FakeFileLock(this, position, size, shared); - } finally { - lock.unlock(); - } + return new FakeFileLock(this, position, size, shared); } @Override diff --git a/jimfs/src/main/java/com/google/jimfs/internal/OpenOptions.java b/jimfs/src/main/java/com/google/jimfs/internal/OpenOptions.java index 9e6657f..b1241af 100644 --- a/jimfs/src/main/java/com/google/jimfs/internal/OpenOptions.java +++ b/jimfs/src/main/java/com/google/jimfs/internal/OpenOptions.java @@ -63,72 +63,59 @@ final class OpenOptions extends LinkOptions { return new OpenOptions(ImmutableSet.copyOf(options)); } - private final boolean read; - private final boolean write; - private final boolean append; - private final boolean truncateExisting; - private final boolean create; - private final boolean createNew; - private final boolean sparse; + private final ImmutableSet options; private OpenOptions(Set options) { super(options); - this.read = options.contains(READ); - this.write = options.contains(WRITE); - this.append = options.contains(APPEND); - this.truncateExisting = options.contains(TRUNCATE_EXISTING); - this.create = options.contains(CREATE); - this.createNew = options.contains(CREATE_NEW); - this.sparse = options.contains(SPARSE); - // we don't care about any other open options currently + this.options = ImmutableSet.copyOf(options); } /** * Returns whether or not to open the file for reading. */ public boolean isRead() { - return read; + return options.contains(READ); } /** * Returns whether or not to open the file for writing. */ public boolean isWrite() { - return write; + return options.contains(WRITE); } /** * Returns whether or not to open the file in append mode. */ public boolean isAppend() { - return append; + return options.contains(APPEND); } /** * Returns whether or not to truncate the file when opening it. */ public boolean isTruncateExisting() { - return truncateExisting; + return options.contains(TRUNCATE_EXISTING); } /** * Returns whether or not to create the file if it doesn't exist. */ public boolean isCreate() { - return create; + return options.contains(CREATE); } /** * Returns whether or not to create the file and throw an exception if it already exists. */ public boolean isCreateNew() { - return createNew; + return options.contains(CREATE_NEW); } /** * Returns whether or not the created file should be sparse, if supported. */ public boolean isSparse() { - return sparse; + return options.contains(SPARSE); } } -- cgit v1.2.3