List of usage examples for io.netty.buffer ByteBuf setBytes
public abstract int setBytes(int index, ScatteringByteChannel in, int length) throws IOException;
From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java
License:Apache License
@Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length);//from www . j av a2 s.com if (length == 0) { return in.read(EmptyArrays.EMPTY_BYTES); } int i = findIndex(index); int readBytes = 0; do { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.writableBytes()); int localReadBytes = s.setBytes(index - adjustment, in, localLength); if (localReadBytes < 0) { if (readBytes == 0) { return -1; } else { break; } } if (localReadBytes == localLength) { index += localLength; length -= localLength; readBytes += localLength; i++; } else { index += localReadBytes; length -= localReadBytes; readBytes += localReadBytes; } } while (length > 0); return readBytes; }
From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java
License:Apache License
@Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { checkIndex(index, length);/* w w w . j a v a 2s . c om*/ if (length == 0) { return in.read(FULL_BYTEBUFFER); } int i = findIndex(index); int readBytes = 0; do { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.writableBytes()); int localReadBytes = s.setBytes(index - adjustment, in, localLength); if (localReadBytes == 0) { break; } if (localReadBytes < 0) { if (readBytes == 0) { return -1; } else { break; } } if (localReadBytes == localLength) { index += localLength; length -= localLength; readBytes += localLength; i++; } else { index += localReadBytes; length -= localReadBytes; readBytes += localReadBytes; } } while (length > 0); return readBytes; }