Skip to content

Instantly share code, notes, and snippets.

@dmlloyd
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save dmlloyd/6bee0e51323a7e454085 to your computer and use it in GitHub Desktop.

Select an option

Save dmlloyd/6bee0e51323a7e454085 to your computer and use it in GitHub Desktop.
Buffer source API
/*
* 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 org.xnio;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.function.Supplier;
/**
* A supplier of populated {@link ByteBuffer} instances which are {@linkplain ByteBuffer#flip() flipped} for emptying.
*
* @author <a href="mailto:[email protected]">David M. Lloyd</a>
*/
public interface BufferSource extends Supplier<ByteBuffer> {
/**
* Get the next byte buffer. Return {@code null} if there are no available buffers. The same buffer will be
* returned until the contents of that buffer have been emptied.
*
* @return the next byte buffer, or {@code null} if there are no more buffers available
*/
ByteBuffer get();
/**
* Get the number of bytes available by this supplier.
*
* @return the number of bytes available by this supplier
*/
int bytesAvailable();
/**
* Pre-fill the current buffer so that the number of available bytes is equal to or greater than {@code size}. The
* given {@code size} minus the remaining byte count of the current buffer may not exceed {@link #bytesAvailable()}.
* The position of the buffer or the buffer instance itself may change if it has to be compacted to satisfy the size.
* If the buffer cannot accommodate the size, an exception is thrown. All implementations are required to support
* up to 64 bytes or the current buffer's current capacity, whichever is larger.
*
* @param size the amount of data required in the current buffer
* @throws IllegalArgumentException if {@code size} is less than zero or there is not enough data available to
* prefill the first buffer
* @throws BufferOverflowException if the first buffer is not large enough to support {@code size}
*/
void prefill(int size) throws IllegalArgumentException, BufferOverflowException;
}
package org.xnio.channels.async;
import static org.xnio._private.Messages.msg;
import java.io.IOException;
import java.nio.channels.FileChannel;
import org.xnio.BufferSource;
import org.xnio.IoCallback;
import org.xnio.OptionMap;
import org.xnio.Options;
/**
* @author <a href="mailto:[email protected]">David M. Lloyd</a>
*/
public interface StreamOutputChannel extends AsynchronousChannel {
// ...
<T> void write(BufferSource bufferSource, long minLength, long maxLength, IoCallback<T> callback, T attachment);
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment