Search in sources :

Example 1 with GatheringByteChannel

use of java.nio.channels.GatheringByteChannel in project netty by netty.

the class ReadOnlyByteBufTest method shouldForwardReadCallsBlindly.

@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());
}
Also used : GatheringByteChannel(java.nio.channels.GatheringByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with GatheringByteChannel

use of java.nio.channels.GatheringByteChannel in project alluxio by Alluxio.

the class DataServerBlockWriteHandler method writeBuf.

@Override
protected void writeBuf(ByteBuf buf, long pos) throws Exception {
    if (mBytesReserved < pos) {
        long bytesToReserve = Math.max(FILE_BUFFER_SIZE, pos - mBytesReserved);
        // Allocate enough space in the existing temporary block for the write.
        mWorker.requestSpace(mRequest.mSessionId, mRequest.mId, bytesToReserve);
        mBytesReserved += bytesToReserve;
    }
    BlockWriter blockWriter = ((BlockWriteRequestInternal) mRequest).mBlockWriter;
    GatheringByteChannel outputChannel = blockWriter.getChannel();
    int sz = buf.readableBytes();
    Preconditions.checkState(buf.readBytes(outputChannel, sz) == sz);
}
Also used : GatheringByteChannel(java.nio.channels.GatheringByteChannel) BlockWriter(alluxio.worker.block.io.BlockWriter)

Example 3 with GatheringByteChannel

use of java.nio.channels.GatheringByteChannel in project druid by druid-io.

the class FileSmoosher method delegateSmooshedWriter.

/**
   * Returns a new SmooshedWriter which writes into temporary file and close
   * method on returned SmooshedWriter tries to merge temporary file into
   * original FileSmoosher object(if not open).
   *
   * @param name fileName
   * @param size size of the file.
   *
   * @return
   *
   * @throws IOException
   */
private SmooshedWriter delegateSmooshedWriter(final String name, final long size) throws IOException {
    final File tmpFile = new File(baseDir, name);
    filesInProcess.add(tmpFile);
    return new SmooshedWriter() {

        private final FileOutputStream out = new FileOutputStream(tmpFile);

        private final GatheringByteChannel channel = out.getChannel();

        private final Closer closer = Closer.create();

        private int currOffset = 0;

        {
            closer.register(out);
            closer.register(channel);
        }

        @Override
        public void close() throws IOException {
            closer.close();
            completedFiles.add(tmpFile);
            filesInProcess.remove(tmpFile);
            if (!writerCurrentlyInUse) {
                mergeWithSmoosher();
            }
        }

        public int bytesLeft() {
            return (int) (size - currOffset);
        }

        @Override
        public int write(ByteBuffer buffer) throws IOException {
            return addToOffset(channel.write(buffer));
        }

        @Override
        public int write(InputStream in) throws IOException {
            return addToOffset(ByteStreams.copy(Channels.newChannel(in), channel));
        }

        @Override
        public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
            return addToOffset(channel.write(srcs, offset, length));
        }

        @Override
        public long write(ByteBuffer[] srcs) throws IOException {
            return addToOffset(channel.write(srcs));
        }

        public int addToOffset(long numBytesWritten) {
            if (numBytesWritten > bytesLeft()) {
                throw new ISE("Wrote more bytes[%,d] than available[%,d]. Don't do that.", numBytesWritten, bytesLeft());
            }
            currOffset += numBytesWritten;
            return Ints.checkedCast(numBytesWritten);
        }

        @Override
        public boolean isOpen() {
            return channel.isOpen();
        }
    };
}
Also used : Closer(com.google.common.io.Closer) GatheringByteChannel(java.nio.channels.GatheringByteChannel) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ISE(io.druid.java.util.common.ISE) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 4 with GatheringByteChannel

use of java.nio.channels.GatheringByteChannel in project async-http-client by AsyncHttpClient.

the class MultipartPart method transfer.

protected long transfer(ByteBuf source, WritableByteChannel target, MultipartState sourceFullyWrittenState) throws IOException {
    int transferred = 0;
    if (target instanceof GatheringByteChannel) {
        transferred = source.readBytes((GatheringByteChannel) target, source.readableBytes());
    } else {
        for (ByteBuffer byteBuffer : source.nioBuffers()) {
            int len = byteBuffer.remaining();
            int written = target.write(byteBuffer);
            transferred += written;
            if (written != len) {
                // couldn't write full buffer, exit loop
                break;
            }
        }
        // assume this is a basic single ByteBuf
        source.readerIndex(source.readerIndex() + transferred);
    }
    if (source.isReadable()) {
        slowTarget = true;
    } else {
        state = sourceFullyWrittenState;
    }
    return transferred;
}
Also used : GatheringByteChannel(java.nio.channels.GatheringByteChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

GatheringByteChannel (java.nio.channels.GatheringByteChannel)4 ByteBuffer (java.nio.ByteBuffer)3 BlockWriter (alluxio.worker.block.io.BlockWriter)1 Closer (com.google.common.io.Closer)1 ISE (io.druid.java.util.common.ISE)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Test (org.junit.Test)1