aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java
Publish
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java85
1 files changed, 85 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java
new file mode 100644
index 00000000000..eed3210f57e
--- /dev/null
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java
@@ -0,0 +1,85 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.jdisc.handler;
+
+import com.google.common.util.concurrent.ListenableFuture;
+
+import java.nio.ByteBuffer;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * <p>This class extends the {@link AbstractContentOutputStream}, and forwards all write() and close() calls to a {@link
+ * FastContentWriter}. This means that once {@link #close()} has been called, the asynchronous completion of all pending
+ * operations can be awaited using the ListenableFuture interface of this class. Any asynchronous failure will be
+ * rethrown when calling either of the get() methods on this class.</p>
+ * <p>Please notice that the Future implementation of this class will NEVER complete unless {@link #close()} has been
+ * called.</p>
+ *
+ * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
+ */
+public class FastContentOutputStream extends AbstractContentOutputStream implements ListenableFuture<Boolean> {
+
+ private final FastContentWriter out;
+
+ /**
+ * <p>Constructs a new FastContentOutputStream that writes into the given {@link ContentChannel}.</p>
+ *
+ * @param out The ContentChannel to write the stream into.
+ */
+ public FastContentOutputStream(ContentChannel out) {
+ this(new FastContentWriter(out));
+ }
+
+ /**
+ * <p>Constructs a new FastContentOutputStream that writes into the given {@link FastContentWriter}.</p>
+ *
+ * @param out The ContentWriter to write the stream into.
+ */
+ public FastContentOutputStream(FastContentWriter out) {
+ Objects.requireNonNull(out, "out");
+ this.out = out;
+ }
+
+ @Override
+ protected void doFlush(ByteBuffer buf) {
+ out.write(buf);
+ }
+
+ @Override
+ protected void doClose() {
+ out.close();
+ }
+
+ @Override
+ public boolean cancel(boolean mayInterruptIfRunning) {
+ return out.cancel(mayInterruptIfRunning);
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return out.isCancelled();
+ }
+
+ @Override
+ public boolean isDone() {
+ return out.isDone();
+ }
+
+ @Override
+ public Boolean get() throws InterruptedException, ExecutionException {
+ return out.get();
+ }
+
+ @Override
+ public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
+ return out.get(timeout, unit);
+ }
+
+ @Override
+ public void addListener(Runnable listener, Executor executor) {
+ out.addListener(listener, executor);
+ }
+}