All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.jdisc.handler.FastContentOutputStream Maven / Gradle / Ivy

// Copyright 2017 Yahoo Holdings. 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;

/**
 * 

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.

*

Please notice that the Future implementation of this class will NEVER complete unless {@link #close()} has been * called.

* * @author Simon Thoresen Hult */ public class FastContentOutputStream extends AbstractContentOutputStream implements ListenableFuture { private final FastContentWriter out; /** *

Constructs a new FastContentOutputStream that writes into the given {@link ContentChannel}.

* * @param out The ContentChannel to write the stream into. */ public FastContentOutputStream(ContentChannel out) { this(new FastContentWriter(out)); } /** *

Constructs a new FastContentOutputStream that writes into the given {@link FastContentWriter}.

* * @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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy