Skip to content

Instantly share code, notes, and snippets.

@dmlloyd
Last active August 29, 2015 14:15
Show Gist options
  • Save dmlloyd/000db234b8847ed25a47 to your computer and use it in GitHub Desktop.
Save dmlloyd/000db234b8847ed25a47 to your computer and use it in GitHub Desktop.
Stream output channel
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.examples.example1;
import java.io.IOException;
import java.nio.ByteBuffer;
import io.examples.IoCallback2;
/**
* @author <a href="mailto:[email protected]">David M. Lloyd</a>
*/
public interface StreamOutputChannel3 extends AsynchronousChannel3 {
/**
* Get the number of bytes actually flushed by this channel. Any bytes sent after the channel was closed or errored
* do not contribute to this total.
*
* @return the number of bytes flushed by this channel
*/
long getByteCounter();
/**
* Enqueue a buffer into this channel. If the channel was shut down or failed due to error, the buffer will be re-pooled.
*
* @param buffer the buffer to enqueue
* @throws IllegalArgumentException if the given buffer does not match the channel buffer pool
*/
void enqueueBuffer(ByteBuffer buffer) throws IllegalArgumentException;
/**
* Enqueue a channel shutdown into this channel. No more buffers may be enqueued.
*/
void shutdown();
/**
* Get the number of pending unflushed bytes on this channel.
*
* @return the number of pending unflushed bytes
*/
long pending();
/**
* Flush some amount of data.
*
* @param minLength the minimum amount to flush, or 0 to flush all data enqueued at the time of this method call
* @param callback the callback to call when the flush operation completes
* @param attachment the attachment to pass to the callback
* @param <T> the attachment type
* @throws IllegalArgumentException if {@code minLength} exceeds {@link #pending()}
*/
<T> void flush(long minLength, IoCallback2<T> callback, T attachment) throws IllegalArgumentException;
default void flush(IoCallback2<?> callback) {
flush(callback, null);
}
<T> void flush(IoCallback2<T> callback, T attachment);
default void transferFrom(StreamInputChannel3 channel, long length, IoCallback2<?> callback) throws IllegalStateException {
transferFrom(channel, length, callback, null);
}
<T> void transferFrom(StreamInputChannel3 channel, long length, IoCallback2<T> callback, T attachment) throws IllegalStateException;
// blocking operations
void flushBlocking(long minLength) throws IllegalArgumentException, IOException;
// stream
ChannelOutputStream getOutputStream();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment