下面列出了怎么用java.nio.channels.GatheringByteChannel的API类实例代码及写法,或者点击链接到github查看源代码。
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
checkIndex(index, length);
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = memory.duplicate();
}
index = idx(index);
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
checkIndex(index, length);
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = memory.duplicate();
}
index = idx(index);
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
@Override
public void writeTo(GatheringByteChannel channel) throws IOException {
// nothing to do
if (length == 0) {
return;
}
int currentLength = length;
int currentOffset = offset;
BytesRef ref = new BytesRef();
while (currentLength > 0) {
// try to align to the underlying pages while writing, so no new arrays will be created.
int fragmentSize = Math.min(currentLength, PAGE_SIZE - (currentOffset % PAGE_SIZE));
boolean newArray = bytearray.get(currentOffset, fragmentSize, ref);
assert !newArray : "PagedBytesReference failed to align with underlying bytearray. offset [" + currentOffset + "], size [" + fragmentSize + "]";
Channels.writeToChannel(ref.bytes, ref.offset, ref.length, channel);
currentLength -= ref.length;
currentOffset += ref.length;
}
assert currentLength == 0;
}
public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
{
int length=0;
Buffer buf0 = header==null?null:header.buffer();
Buffer buf1 = buffer==null?null:buffer.buffer();
if (_channel instanceof GatheringByteChannel &&
header!=null && header.length()!=0 && buf0 instanceof NIOBuffer &&
buffer!=null && buffer.length()!=0 && buf1 instanceof NIOBuffer)
{
length = gatheringFlush(header,((NIOBuffer)buf0).getByteBuffer(),buffer,((NIOBuffer)buf1).getByteBuffer());
}
else
{
// flush header
if (header!=null && header.length()>0)
length=flush(header);
// flush buffer
if ((header==null || header.length()==0) &&
buffer!=null && buffer.length()>0)
length+=flush(buffer);
// flush trailer
if ((header==null || header.length()==0) &&
(buffer==null || buffer.length()==0) &&
trailer!=null && trailer.length()>0)
length+=flush(trailer);
}
return length;
}
protected int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException
{
int length;
synchronized(this)
{
// Adjust position indexs of buf0 and buf1
bbuf0=bbuf0.asReadOnlyBuffer();
bbuf0.position(header.getIndex());
bbuf0.limit(header.putIndex());
bbuf1=bbuf1.asReadOnlyBuffer();
bbuf1.position(buffer.getIndex());
bbuf1.limit(buffer.putIndex());
_gather2[0]=bbuf0;
_gather2[1]=bbuf1;
// do the gathering write.
length=(int)((GatheringByteChannel)_channel).write(_gather2);
int hl=header.length();
if (length>hl)
{
header.clear();
buffer.skip(length-hl);
}
else if (length>0)
{
header.skip(length);
}
}
return length;
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
// adapted from UnpooledDirectByteBuf:
checkIndex(index, length);
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf = memorySegment.wrap(index, length);
return out.write(tmpBuf);
}
protected int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException
{
int length;
synchronized(this)
{
// Adjust position indexs of buf0 and buf1
bbuf0=bbuf0.asReadOnlyBuffer();
bbuf0.position(header.getIndex());
bbuf0.limit(header.putIndex());
bbuf1=bbuf1.asReadOnlyBuffer();
bbuf1.position(buffer.getIndex());
bbuf1.limit(buffer.putIndex());
_gather2[0]=bbuf0;
_gather2[1]=bbuf1;
// do the gathering write.
length=(int)((GatheringByteChannel)_channel).write(_gather2);
int hl=header.length();
if (length>hl)
{
header.clear();
buffer.skip(length-hl);
}
else if (length>0)
{
header.skip(length);
}
}
return length;
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException {
int count = nioBufferCount();
if (count == 1) {
return out.write(internalNioBuffer(index, length));
} else {
long writtenBytes = out.write(nioBuffers(index, length));
if (writtenBytes > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else {
return (int) writtenBytes;
}
}
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
checkIndex(index, length);
index = idx(index);
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = ByteBuffer.wrap(memory);
}
return out.write((ByteBuffer) tmpBuf.clear().position(index).limit(index + length));
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
ensureAccessible();
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = ByteBuffer.wrap(array);
}
return out.write((ByteBuffer) tmpBuf.clear().position(index).limit(index + length));
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
ensureAccessible();
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = buffer.duplicate();
}
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
ensureAccessible();
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf = internalNioBuffer();
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
checkIndex(index, length);
index = idx(index);
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = ByteBuffer.wrap(array);
}
return out.write((ByteBuffer) tmpBuf.clear().position(index).limit(index + length));
}
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
GatheringByteChannel channel = (GatheringByteChannel) target;
long write = channel.write(this.buffers);
transferred += write;
return write;
}
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
GatheringByteChannel channel = (GatheringByteChannel) target;
long write = channel.write(this.buffers);
transferred += write;
return write;
}
private static Sink newSink(Object out) {
if (out instanceof OutputStream) {
return new OioSink((OutputStream) out);
}
if (out instanceof GatheringByteChannel) {
return new NioSink((GatheringByteChannel) out);
}
throw new FastdfsException("unknown sink output type " + out.getClass().getName());
}
/**
* Writes the current values of the model parameters to a writable channel.
*
* The written values can be restored later using {@link #loadParameters(ScatteringByteChannel)},
* under condition that the same underlying model is used.
*
* @param outputChannel where to write the parameters.
* @throws IOException if an I/O error occurs.
*/
public void saveParameters(GatheringByteChannel outputChannel) throws IOException {
parameterLock.readLock().lock();
try {
outputChannel.write(modelParameters);
for (ByteBuffer buffer : modelParameters) {
buffer.rewind();
}
} finally {
parameterLock.readLock().unlock();
}
}
static long write(GatheringByteChannel channel, Collection<QpidByteBuffer> qpidByteBuffers)
throws IOException
{
List<ByteBuffer> byteBuffers = new ArrayList<>();
for (QpidByteBuffer qpidByteBuffer : qpidByteBuffers)
{
Collections.addAll(byteBuffers, getUnderlyingBuffers(qpidByteBuffer));
}
return channel.write(byteBuffers.toArray(new ByteBuffer[byteBuffers.size()]));
}
private static Sink newSink(Object out) {
if (out instanceof OutputStream) {
return new OioSink((OutputStream) out);
}
if (out instanceof GatheringByteChannel) {
return new NioSink((GatheringByteChannel) out);
}
throw new FastdfsException("unknown sink output type " + out.getClass().getName());
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
ensureAccessible();
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = buffer.duplicate();
}
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
checkIndex(index, length);
index = idx(index);
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = ByteBuffer.wrap(memory);
}
return out.write((ByteBuffer) tmpBuf.clear().position(index).limit(index + length));
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
ensureAccessible();
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf = internalNioBuffer();
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
public int getBytes(final int index, final GatheringByteChannel out, final int length) throws IOException {
byte[] bytesToGet = new byte[length];
getBytes(index, bytesToGet);
return out.write(ByteBuffer.wrap(bytesToGet));
}
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
long bytes = channel.write(buffers.toArray(new ByteBuffer[0]));
findCurrent();
return bytes;
}
public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException {
return out.write(ByteBuffer.wrap(array, index, length));
}
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
return channel.write(buffers.toArray(new ByteBuffer[0]));
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
return udle.getBytes(index + offset, out, length);
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException {
return buffer.getBytes(index, out, length);
}
@Override
public int readBytes(GatheringByteChannel out, int length) throws IOException {
throw new UnsupportedOperationException(ERROR_MESSAGE);
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
return getBytes(index, out, length, false);
}