diff --git a/tape/src/main/java/com/squareup/tape2/QueueFile.java b/tape/src/main/java/com/squareup/tape2/QueueFile.java index c1cae7b6..44777469 100644 --- a/tape/src/main/java/com/squareup/tape2/QueueFile.java +++ b/tape/src/main/java/com/squareup/tape2/QueueFile.java @@ -133,7 +133,7 @@ public final class QueueFile implements Closeable, Iterable { @Private boolean closed; - @Private static RandomAccessFile initializeFromFile(File file, boolean forceLegacy) + @Private static void initializeFromFile(File file, boolean forceLegacy) throws IOException { if (!file.exists()) { // Use a temp file so we don't leave a partially-initialized file. @@ -157,8 +157,6 @@ public final class QueueFile implements Closeable, Iterable { throw new IOException("Rename failed!"); } } - - return open(file); } /** Opens a random access file that writes synchronously. */ @@ -166,6 +164,11 @@ private static RandomAccessFile open(File file) throws FileNotFoundException { return new RandomAccessFile(file, "rwd"); } + /** Opens a random access file that writes asynchronously. */ + private static RandomAccessFile openAsynchronously(File file) throws FileNotFoundException { + return new RandomAccessFile(file, "rw"); + } + QueueFile(File file, RandomAccessFile raf, boolean zero, boolean forceLegacy) throws IOException { this.file = file; this.raf = raf; @@ -723,6 +726,7 @@ public static final class Builder { final File file; boolean zero = true; boolean forceLegacy = false; + boolean writeSynchronously = true; /** Start constructing a new queue backed by the given file. */ public Builder(File file) { @@ -744,12 +748,21 @@ public Builder forceLegacy(boolean forceLegacy) { return this; } + /** + * When false, writing to file is asynchronous + */ + public Builder writeSynchronously(boolean writeSynchronously) { + this.writeSynchronously = writeSynchronously; + return this; + } + /** * Constructs a new queue backed by the given builder. Only one instance should access a given * file at a time. */ public QueueFile build() throws IOException { - RandomAccessFile raf = initializeFromFile(file, forceLegacy); + initializeFromFile(file, forceLegacy); + RandomAccessFile raf = writeSynchronously ? open(file) : openAsynchronously(file); QueueFile qf = null; try { qf = new QueueFile(file, raf, zero, forceLegacy);