Skip to content

Instantly share code, notes, and snippets.

@dmlloyd
Last active August 29, 2015 14:15
Show Gist options
  • Save dmlloyd/fa10fbab1bdb94b7ab9b to your computer and use it in GitHub Desktop.
Save dmlloyd/fa10fbab1bdb94b7ab9b to your computer and use it in GitHub Desktop.
Stream input 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 java.nio.channels.FileChannel;
import io.examples.IoCallback2;
/**
* @author <a href="mailto:[email protected]">David M. Lloyd</a>
*/
public interface StreamInputChannel3 extends AsynchronousChannel3 {
/**
* Get the number of bytes consumed by this channel over all time. This includes buffered bytes which have not
* yet been dequeued.
*
* @return the number of bytes ever consumed by this channel
*/
long getByteCounter();
/**
*
* @param channel
* @param length
* @param callback
*/
default void transferTo(StreamOutputChannel3 channel, long length, IoCallback2<?> callback) {
transferTo(channel, length, callback, null);
}
default <T> void transferTo(StreamOutputChannel3 channel, long length, IoCallback2<T> callback, T attachment) {
channel.transferFrom(this, length, callback, attachment);
}
default void transferTo(FileChannel channel, long length, IoCallback2<?> callback) {
transferTo(channel, length, callback, null);
}
<T> void transferTo(FileChannel channel, long length, IoCallback2<T> callback, T attachment);
default void skip(long length, IoCallback2<?> callback) {
skip(length, callback, null);
}
<T> void skip(long length, IoCallback2<T> callback, T attachment);
long skipBlocking(long length) throws IOException;
/**
* Initiate a read operation, calling the given callback once a certain number of bytes have been read. Use
* {@link #checkResult()} to acquire the thrown exception and {@link #peekBuffer()} or {@link #getBuffer()}
* to examine the result of the read operation.
* <p>
* The read operation will not trigger the callback until at least {@code minLength} bytes are available or an error
* occurs. The operation will not attempt to make more than {@code maxLength} bytes available. The value of
* {@code maxLength} must be equal to or greater than the value of {@code minLength}.
* <p>
* The channel is not thread-safe, and thus should not be accessed until the callback has been called.
*
* @param minLength the minimum number of available bytes before returning
* @param maxLength the maximum number of bytes to make available
* @param callback the callback to call when the read operation is complete (or an error occurs)
* @param attachment the attachment to pass to the callback
* @param <T> the attachment type
*/
<T> void read(int minLength, int maxLength, IoCallback2<T> callback, T attachment);
default void read(int minLength, int maxLength, IoCallback2<?> callback) {
read(minLength, maxLength, callback, null);
}
default <T> void read(int minLength, IoCallback2<T> callback, T attachment) {
read(minLength, Integer.MAX_VALUE, callback, attachment);
}
default void read(int minLength, IoCallback2<?> callback) {
read(minLength, callback, null);
}
default <T> void read(IoCallback2<T> callback, T attachment) {
read(0, callback, attachment);
}
default void read(IoCallback2<?> callback) {
read(0, callback, null);
}
/**
* Get an input stream for this channel.
*
* @return the input stream
*/
ChannelInputStream getInputStream();
/**
* Get the number of buffered bytes available on this channel.
*
* @return the number of available bytes
*/
long available();
/**
* Get a peek at the first buffer of the channel, if any. Any emptied buffers at the head of the queue will
* automatically be released.
*
* @return the buffer, or {@code null} if no buffers are in the queue
*/
ByteBuffer peekBuffer();
/**
* Get and remove the first buffer of the channel, if any.
*
* @return the buffer, or {@code null} if no buffers are in the queue
*/
ByteBuffer getBuffer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment