java.nio.channels.GatheringByteChannel Java Examples

The following examples show how to use java.nio.channels.GatheringByteChannel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PooledDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: PagedBytesReference.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: PooledUnsafeDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #4
Source File: PooledHeapByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #5
Source File: NetworkBuffer.java    From flink with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: ChannelEndPoint.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
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;
}
 
Example #7
Source File: CompositeByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@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;
        }
    }
}
 
Example #8
Source File: UnpooledDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: ChannelEndPoint.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
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;
}
 
Example #10
Source File: ChannelEndPoint.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
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;
}
 
Example #11
Source File: ReadOnlyByteBufferBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: StreamReplier.java    From fastdfs-client with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #13
Source File: QpidByteBufferFactory.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
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()]));
}
 
Example #14
Source File: PooledHeapByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #15
Source File: UnpooledHeapByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #16
Source File: TransferLearningModel.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #17
Source File: StreamReplier.java    From azeroth with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #18
Source File: UnpooledUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #19
Source File: PullMessageProcessor.java    From qmq with Apache License 2.0 5 votes vote down vote up
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
    GatheringByteChannel channel = (GatheringByteChannel) target;
    long write = channel.write(this.buffers);
    transferred += write;
    return write;
}
 
Example #20
Source File: DataTransfer.java    From qmq with Apache License 2.0 5 votes vote down vote up
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
    GatheringByteChannel channel = (GatheringByteChannel) target;
    long write = channel.write(this.buffers);
    transferred += write;
    return write;
}
 
Example #21
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: ReadOnlyPooledHeapByteBuf.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #23
Source File: ReplayingDecoderByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public int readBytes(GatheringByteChannel out, int length) {
    throw reject();
}
 
Example #24
Source File: ReplayingDecoderByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public int getBytes(int index, GatheringByteChannel out, int length) {
    throw reject();
}
 
Example #25
Source File: ReadOnlyByteBufTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldForwardReadCallsBlindly() throws Exception {
    ByteBuf buf = mock(ByteBuf.class);
    when(buf.order()).thenReturn(BIG_ENDIAN);
    when(buf.maxCapacity()).thenReturn(65536);
    when(buf.readerIndex()).thenReturn(0);
    when(buf.writerIndex()).thenReturn(0);
    when(buf.capacity()).thenReturn(0);

    when(buf.getBytes(1, (GatheringByteChannel) null, 2)).thenReturn(3);
    when(buf.getBytes(4, (OutputStream) null, 5)).thenReturn(buf);
    when(buf.getBytes(6, (byte[]) null, 7, 8)).thenReturn(buf);
    when(buf.getBytes(9, (ByteBuf) null, 10, 11)).thenReturn(buf);
    when(buf.getBytes(12, (ByteBuffer) null)).thenReturn(buf);
    when(buf.getByte(13)).thenReturn(Byte.valueOf((byte) 14));
    when(buf.getShort(15)).thenReturn(Short.valueOf((short) 16));
    when(buf.getUnsignedMedium(17)).thenReturn(18);
    when(buf.getInt(19)).thenReturn(20);
    when(buf.getLong(21)).thenReturn(22L);

    ByteBuffer bb = ByteBuffer.allocate(100);

    when(buf.nioBuffer(23, 24)).thenReturn(bb);
    when(buf.capacity()).thenReturn(27);

    ByteBuf roBuf = unmodifiableBuffer(buf);
    assertEquals(3, roBuf.getBytes(1, (GatheringByteChannel) null, 2));
    roBuf.getBytes(4, (OutputStream) null, 5);
    roBuf.getBytes(6, (byte[]) null, 7, 8);
    roBuf.getBytes(9, (ByteBuf) null, 10, 11);
    roBuf.getBytes(12, (ByteBuffer) null);
    assertEquals((byte) 14, roBuf.getByte(13));
    assertEquals((short) 16, roBuf.getShort(15));
    assertEquals(18, roBuf.getUnsignedMedium(17));
    assertEquals(20, roBuf.getInt(19));
    assertEquals(22L, roBuf.getLong(21));

    ByteBuffer roBB = roBuf.nioBuffer(23, 24);
    assertEquals(100, roBB.capacity());
    assertTrue(roBB.isReadOnly());

    assertEquals(27, roBuf.capacity());
}
 
Example #26
Source File: ReadOnlyByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public int getBytes(int index, GatheringByteChannel out, int length)
        throws IOException {
    return unwrap().getBytes(index, out, length);
}
 
Example #27
Source File: ProtocolBuffer.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
@Override
public int getBytes(int i, GatheringByteChannel gatheringByteChannel, int i1) throws IOException {
    return byteBuf.getBytes(i, gatheringByteChannel, i1);
}
 
Example #28
Source File: ReplayingDecoderBuffer.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public int getBytes(final int n, final GatheringByteChannel gatheringByteChannel, final int n2) {
	throw reject();
}
 
Example #29
Source File: PooledHeapByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
    return getBytes(index, out, length, false);
}
 
Example #30
Source File: PooledUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
    return getBytes(index, out, length, false);
}