java.nio.channels.ScatteringByteChannel Java Examples

The following examples show how to use java.nio.channels.ScatteringByteChannel. 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: HeapChannelBuffer.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #2
Source File: HeapChannelBuffer.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #3
Source File: HeapChannelBuffer.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #4
Source File: HeapChannelBuffer.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #5
Source File: HeapChannelBuffer.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #6
Source File: HeapChannelBuffer.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #7
Source File: WebOperationService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static int read(ReadableByteChannel src, ByteBuf dst) throws IOException {
    if (src instanceof ScatteringByteChannel) {
        return dst.writeBytes((ScatteringByteChannel) src, dst.writableBytes());
    }

    final int readBytes = src.read(dst.nioBuffer(dst.writerIndex(), dst.writableBytes()));
    if (readBytes > 0) {
        dst.writerIndex(dst.writerIndex() + readBytes);
    }
    return readBytes;
}
 
Example #8
Source File: PooledHeapByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public final int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);
    index = idx(index);
    try {
        return in.read((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length));
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #9
Source File: UnpooledHeapByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    try {
        return in.read((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length));
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #10
Source File: UnpooledDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpNioBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #11
Source File: PooledUnsafeDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);
    ByteBuffer tmpBuf = internalNioBuffer();
    index = idx(index);
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #12
Source File: NetworkBuffer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
	// adapted from UnpooledDirectByteBuf:
	checkIndex(index, length);

	ByteBuffer tmpBuf = memorySegment.wrap(index, length);
	try {
		return in.read(tmpBuf);
	} catch (ClosedChannelException ignored) {
		return -1;
	}
}
 
Example #13
Source File: NetworkBuffer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
	// adapted from UnpooledDirectByteBuf:
	checkIndex(index, length);

	ByteBuffer tmpBuf = memorySegment.wrap(index, length);
	try {
		return in.read(tmpBuf);
	} catch (ClosedChannelException ignored) {
		return -1;
	}
}
 
Example #14
Source File: PooledDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);
    ByteBuffer tmpBuf = internalNioBuffer();
    index = idx(index);
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #15
Source File: FileSystemHttpFile.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected int read(ByteChannel src, ByteBuf dst) throws IOException {
    if (src instanceof ScatteringByteChannel) {
        return dst.writeBytes((ScatteringByteChannel) src, dst.writableBytes());
    }

    final int readBytes = src.read(dst.nioBuffer(dst.writerIndex(), dst.writableBytes()));
    if (readBytes > 0) {
        dst.writerIndex(dst.writerIndex() + readBytes);
    }
    return readBytes;
}
 
Example #16
Source File: AbstractByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
    ensureWritable(length);
    int writtenBytes = setBytes(writerIndex, in, length);
    if (writtenBytes > 0) {
        writerIndex += writtenBytes;
    }
    return writtenBytes;
}
 
Example #17
Source File: UnpooledUnsafeDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #18
Source File: PooledUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);
    ByteBuffer tmpBuf = internalNioBuffer();
    index = idx(index);
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #19
Source File: UnpooledHeapByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    try {
        return in.read((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length));
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #20
Source File: PooledHeapByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);
    index = idx(index);
    try {
        return in.read((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length));
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #21
Source File: UnpooledDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpNioBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #22
Source File: MultiQpidByteBuffer.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Override
public final long read(ScatteringByteChannel channel) throws IOException
{
    ByteBuffer[] byteBuffers = new ByteBuffer[_fragments.length];
    for (int i = 0; i < byteBuffers.length; i++)
    {
        final SingleQpidByteBuffer fragment = _fragments[i];
        byteBuffers[i] = fragment.getUnderlyingBuffer();
    }
    return channel.read(byteBuffers);
}
 
Example #23
Source File: TransferLearningModel.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Overwrites the current model parameter values with the values read from a channel.
 *
 * The channel should contain values previously written by
 * {@link #saveParameters(GatheringByteChannel)} for the same underlying model.
 *
 * @param inputChannel where to read the parameters from.
 * @throws IOException if an I/O error occurs.
 */
public void loadParameters(ScatteringByteChannel inputChannel) throws IOException {
  parameterLock.writeLock().lock();
  try {
    inputChannel.read(modelParameters);
    for (ByteBuffer buffer : modelParameters) {
      buffer.rewind();
    }
  } finally {
    parameterLock.writeLock().unlock();
  }
}
 
Example #24
Source File: BasicSliceOutput.java    From aion with MIT License 5 votes vote down vote up
@Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
    int writtenBytes = slice.setBytes(size, in, length);
    if (writtenBytes > 0) {
        size += writtenBytes;
    }
    return writtenBytes;
}
 
Example #25
Source File: Slice.java    From aion with MIT License 5 votes vote down vote up
/**
 * Transfers the content of the specified source channel to this buffer starting at the
 * specified absolute {@code index}.
 *
 * @param length the maximum number of bytes to transfer
 * @return the actual number of bytes read in from the specified channel. {@code -1} if the
 *     specified channel is closed.
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or if
 *     {@code index + length} is greater than {@code this.capacity}
 * @throws java.io.IOException if the specified channel threw an exception during I/O
 */
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    ByteBuffer buf = ByteBuffer.wrap(data, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}
 
Example #26
Source File: UnpooledUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
 
Example #27
Source File: DeadBuf.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
  throw new UnsupportedOperationException(ERROR_MESSAGE);
}
 
Example #28
Source File: EmptyByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) {
    checkIndex(index, length);
    return 0;
}
 
Example #29
Source File: SingleQpidByteBuffer.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public final long read(ScatteringByteChannel channel) throws IOException
{
    return channel.read(getUnderlyingBuffer());
}
 
Example #30
Source File: EmptyByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public int writeBytes(ScatteringByteChannel in, int length) {
    checkLength(length);
    return 0;
}