private void read(ReadableByteChannel channel, ByteBuffer buffer) throws IOException { while (buffer.hasRemaining()) { int r = channel.read(buffer); if (r == -1) { throw new IOException("end of stream when reading header"); } } }
public static boolean readAndFlip( ReadableByteChannel channel, ByteBuffer buffer, int bytes ) throws IOException { buffer.clear(); buffer.limit( bytes ); while ( buffer.hasRemaining() ) { int read = channel.read( buffer ); if ( read == -1 ) { return false; } } buffer.flip(); return true; }
static int skip(final ReadableByteChannel source, final int skip, final ByteBuffer buffer) throws IOException { if (skip <= 0) { return 0; } int toSkip = skip; int skipped = 0; while (toSkip > 0 && skipped != -1) { buffer.clear(); if (toSkip < buffer.capacity()) { buffer.limit(toSkip); } skipped = source.read(buffer); if (skipped > 0) { toSkip -= skipped; } } buffer.clear(); return skip - toSkip; }
/** * Skips bytes from a ReadableByteChannel. * This implementation guarantees that it will read as many bytes * as possible before giving up. * * @param input ReadableByteChannel to skip * @param toSkip number of bytes to skip. * @return number of bytes actually skipped. * @throws IOException if there is a problem reading the ReadableByteChannel * @throws IllegalArgumentException if toSkip is negative * @since 2.5 */ public static long skip(final ReadableByteChannel input, final long toSkip) throws IOException { if (toSkip < 0) { throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip); } final ByteBuffer skipByteBuffer = ByteBuffer.allocate((int) Math.min(toSkip, SKIP_BUFFER_SIZE)); long remain = toSkip; while (remain > 0) { skipByteBuffer.position(0); skipByteBuffer.limit((int) Math.min(remain, SKIP_BUFFER_SIZE)); final int n = input.read(skipByteBuffer); if (n == EOF) { break; } remain -= n; } return toSkip - remain; }
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException { data = ByteBuffer.allocate(CastUtils.l2i(contentSize)); int bytesRead = 0; int b; while (((((b = dataSource.read(data))) + bytesRead) < contentSize)) { bytesRead += b; } }
private static char[] readCharArray( ReadableByteChannel channel, ByteBuffer buffer, char[] charArray ) throws IOException buffer.clear(); int charsLeft = charArray.length; int maxSize = buffer.capacity() / 2; charsLeft = 0; if ( channel.read( buffer ) != buffer.limit() ) buffer.flip(); int length = buffer.limit() / 2; buffer.asCharBuffer().get( charArray, offset, length ); offset += length; buffer.clear();
/** * Does some basic reads from {@code channel}. We execute this against both Okio's channels and * also a standard implementation from the JDK to confirm that their behavior is consistent. */ private void testReadableByteChannel(ReadableByteChannel channel) throws Exception { assertTrue(channel.isOpen()); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byteBuffer.position(3); byteBuffer.limit(23); int byteCount = channel.read(byteBuffer); assertEquals(20, byteCount); assertEquals(23, byteBuffer.position()); assertEquals(23, byteBuffer.limit()); channel.close(); assertEquals(channel instanceof Buffer, channel.isOpen()); // Buffer.close() does nothing. byteBuffer.flip(); byteBuffer.position(3); byte[] data = new byte[byteBuffer.remaining()]; byteBuffer.get(data); assertEquals("abcdefghijklmnopqrst", new String(data, Charsets.UTF_8)); } }
/** * An efficient copy between two channels with a fixed-size buffer. * * @param src the source channel * @param dest the destination channel */ public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { // TODO(yupeng): make the buffer size configurable final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } }
static final int readBytes(ReadableByteChannel source, ByteBuffer dest) throws IOException { // tells how many bytes to read. final int expectedLength = dest.remaining(); int totalRead = 0; // how many bytes were read. int lastRead = source.read(dest); totalRead = lastRead; // if we did not read as many bytes as we had hoped, try reading again. if (lastRead < expectedLength) { // as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading. while (dest.remaining() != 0 && lastRead != -1) { lastRead = source.read(dest); // if we got EOF, do not add to total read. if (lastRead != -1) { totalRead += lastRead; } } } if (totalRead > 0) { dest.limit(dest.position()); } else { dest.position(dest.limit()); } return totalRead; }
private static int readIntFromByteChannel(ReadableByteChannel channel) throws IOException { ByteBuffer intBuffer = ByteBuffer.allocate(INT_BYTES); int result = -1; while (intBuffer.hasRemaining() && (channel.read(intBuffer) >= 0)) { } if (!intBuffer.hasRemaining()) { intBuffer.rewind(); result = intBuffer.asIntBuffer().get(0); } return result; }
@Override public void accept(SubscriberWithContext<Buffer, ReadableByteChannel> sub) { try { ByteBuffer buffer = ByteBuffer.allocate(bufferSize); int read; if ((read = sub.context().read(buffer)) > 0) { buffer.flip(); sub.onNext(new Buffer(buffer).limit(read)); } else { sub.onComplete(); } } catch (IOException e) { sub.onError(e); } } }
public static int read(ReadableByteChannel channel, ByteBuffer buffer) throws IOException { int rem = buffer.position(); while (channel.read(buffer) != -1 && buffer.hasRemaining()) ; return buffer.position() - rem; }
@Deprecated void transferFrom(ReadableByteChannel src, long position, long count) throws IOException { ByteBuffer buff = ByteBuffer.allocateDirect((int) count); src.read(buff); buff.position((int) position); int pos = 0; while (buff.remaining() > 0) { int pageLength = buff.getInt(); Object firstKey = map.getKeyType().read(buff); System.out.println("pageLength=" + pageLength + ", firstKey=" + firstKey); pos += pageLength; buff.position(pos); } }
@Override protected long transferContentTo(WritableByteChannel target) throws IOException { ReadableByteChannel channel = getChannel(); ByteBuffer buffer = getBuffer(); int transferred = 0; int read = channel.read(buffer); if (read > 0) { buffer.flip(); while (buffer.hasRemaining()) { transferred += target.write(buffer); } buffer.compact(); position += transferred; } if (position == getContentLength() || read < 0) { state = MultipartState.POST_CONTENT; if (channel.isOpen()) { channel.close(); } } return transferred; }
while (from.read(buf) != -1) { buf.flip(); while (buf.hasRemaining()) { total += to.write(buf); buf.clear();
@Override public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException { ByteBuffer versionAndFlags = ByteBuffer.allocate(4); dataSource.read(versionAndFlags); parseVersionAndFlags((ByteBuffer) versionAndFlags.rewind()); super.parse(dataSource, header, contentSize, boxParser); }