private static void copy(SeekableByteChannel channel, SeekableByteChannel channel2, ByteBuffer copyBuff, int fileSize) throws IOException { int remain = fileSize; while (remain > 0) { copyBuff.rewind(); copyBuff.limit(Math.min(remain, copyBuff.capacity())); int read = channel.read(copyBuff); copyBuff.position(0); channel2.write(copyBuff); remain -= read; } }
private int read(int len) throws IOException { buffer.rewind().limit(len); int read = channel.read(buffer); buffer.flip(); return read; }
protected int read(long pos, ByteBuffer buf) throws IOException { int read; synchronized (archive) { archive.position(pos); read = archive.read(buf); } buf.flip(); return read; } }
long position = channel.position(); int count = channel.read(buffer); if (count <= 0) { break;
@Override public int read(final byte[] b, final int off, final int len) throws IOException { if (bytesRemaining == 0) { return -1; } int bytesToRead = len; if (bytesToRead > bytesRemaining) { bytesToRead = (int) bytesRemaining; } int bytesRead; ByteBuffer buf; if (bytesToRead <= buffer.capacity()) { buf = buffer; bytesRead = read(bytesToRead); } else { buf = ByteBuffer.allocate(bytesToRead); bytesRead = channel.read(buf); buf.flip(); } if (bytesRead >= 0) { buf.get(b, off, bytesRead); bytesRemaining -= bytesRead; } return bytesRead; }
@Override public WorkUnit call() throws IOException { long pos = ((long) blockSize) * blockIndex; if (pos > chan.size()) { return this; } chan.position(pos); // read until buffer is full, or EOF while (chan.read(buf) > 0) {} ; return this; }
@Override public ByteBuffer call() throws IOException { long pos = ((long) blockSize) * blockIndex; if (pos > chan.size()) { return null; } if (pos < 0) { // This should never happen, if the code's correct. throw new IllegalArgumentException( "blockIndex " + blockIndex + " has position " + pos + ": negative position is not valid."); } chan.position(pos); // read until buffer is full, or EOF while (chan.read(buf) >= 0 && buf.hasRemaining()) {} return buf; }
int readCalls = 0; MessageDigest md = MessageDigest.getInstance("MD5"); while (chan.read(buf) > 0) { readCalls++; md.update(buf.array(), 0, buf.position());
bb.limit(bb.position() + toRead); assert bb.remaining() == toRead; final int i = channel.read(bb); if (i < 0) { // be defensive here, even though we checked before hand, something could have changed throw new EOFException("read past EOF: " + this + " off: " + offset + " len: " + len + " pos: " + pos + " chunkLen: " + toRead + " end: " + end);
channel.position(pos); head.position(0); int c = channel.read(head); head.position(0); if (c == buffSize) {
@Test public void testNewByteChannelRead_seekBeyondSize_reportsEofOnNextRead() throws Exception { Path path = Paths.get(URI.create("gs://lol/cat")); Files.write(path, "hellocat".getBytes(UTF_8)); try (SeekableByteChannel input = Files.newByteChannel(path)) { ByteBuffer buffer = ByteBuffer.allocate(5); input.position(10); assertThat(input.read(buffer)).isEqualTo(-1); input.position(11); assertThat(input.read(buffer)).isEqualTo(-1); assertThat(input.size()).isEqualTo(8); } }
@Test public void testNewByteChannelRead_seeking() throws Exception { Path path = Paths.get(URI.create("gs://lol/cat")); Files.write(path, "helloworld".getBytes(UTF_8)); try (SeekableByteChannel input = Files.newByteChannel(path)) { ByteBuffer buffer = ByteBuffer.allocate(5); input.position(5); assertThat(input.position()).isEqualTo(5); assertThat(input.read(buffer)).isEqualTo(5); assertThat(input.position()).isEqualTo(10); assertThat(new String(buffer.array(), UTF_8)).isEqualTo("world"); buffer.rewind(); assertThat(input.read(buffer)).isEqualTo(-1); input.position(0); assertThat(input.position()).isEqualTo(0); assertThat(input.read(buffer)).isEqualTo(5); assertThat(input.position()).isEqualTo(5); assertThat(new String(buffer.array(), UTF_8)).isEqualTo("hello"); } }
@Test(timeout = 60_000) public void testReadByteChannel() throws IOException { CloudStorageFileSystem testBucket = getTestBucket(); Path path = testBucket.getPath(SML_FILE); long size = Files.size(path); SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ); assertThat(chan.size()).isEqualTo(size); ByteBuffer buf = ByteBuffer.allocate(SML_SIZE); int read = 0; while (chan.isOpen()) { int rc = chan.read(buf); assertThat(chan.size()).isEqualTo(size); if (rc < 0) { // EOF break; } assertThat(rc).isGreaterThan(0); read += rc; assertThat(chan.position()).isEqualTo(read); } assertThat(read).isEqualTo(size); byte[] expected = new byte[SML_SIZE]; new Random(SML_SIZE).nextBytes(expected); assertThat(Arrays.equals(buf.array(), expected)).isTrue(); }
try (SeekableByteChannel ch = Files.newByteChannel(Paths.get("test.txt"))) { ByteBuffer bb = ByteBuffer.allocateDirect(1000); for(;;) { StringBuilder line = new StringBuilder(); int n = ch.read(bb); // add chars to line // ... } }
private void initId3v1Tag(SeekableByteChannel seekableByteChannel) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(ID3v1Tag.TAG_LENGTH); seekableByteChannel.position(getLength() - ID3v1Tag.TAG_LENGTH); byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); if (bytesRead < ID3v1Tag.TAG_LENGTH) throw new IOException("Not enough bytes read"); try { id3v1Tag = new ID3v1Tag(byteBuffer.array()); } catch (NoSuchTagException e) { id3v1Tag = null; } }
private void initCustomTag(SeekableByteChannel seekableByteChannel) throws IOException { int bufferLength = (int) (getLength() - (endOffset + 1)); if (hasId3v1Tag()) bufferLength -= ID3v1Tag.TAG_LENGTH; if (bufferLength <= 0) { customTag = null; } else { ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength); seekableByteChannel.position(endOffset + 1); byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); customTag = byteBuffer.array(); if (bytesRead < bufferLength) throw new IOException("Not enough bytes read"); } }
private void initId3v2Tag(SeekableByteChannel seekableByteChannel) throws IOException, UnsupportedTagException, InvalidDataException { if (xingOffset == 0 || startOffset == 0) { id3v2Tag = null; } else { int bufferLength; if (hasXingFrame()) bufferLength = xingOffset; else bufferLength = startOffset; ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength); seekableByteChannel.position(0); byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); if (bytesRead < bufferLength) throw new IOException("Not enough bytes read"); try { id3v2Tag = ID3v2TagFactory.createTag(byteBuffer.array()); } catch (NoSuchTagException e) { id3v2Tag = null; } } }
protected int preScanFile(SeekableByteChannel seekableByteChannel) { ByteBuffer byteBuffer = ByteBuffer.allocate(AbstractID3v2Tag.HEADER_LENGTH); try { seekableByteChannel.position(0); byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); if (bytesRead == AbstractID3v2Tag.HEADER_LENGTH) { try { byte[] bytes = byteBuffer.array(); ID3v2TagFactory.sanityCheckTag(bytes); return AbstractID3v2Tag.HEADER_LENGTH + BufferTools.unpackSynchsafeInteger(bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 1], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 2], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 3]); } catch (NoSuchTagException | UnsupportedTagException e) { // do nothing } } } catch (IOException e) { // do nothing } return 0; }
private void saveMpegFrames(SeekableByteChannel saveFile) throws IOException { int filePos = xingOffset; if (filePos < 0) filePos = startOffset; if (filePos < 0) return; if (endOffset < filePos) return; SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ); ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength); try { seekableByteChannel.position(filePos); while (true) { byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); byteBuffer.rewind(); if (filePos + bytesRead <= endOffset) { byteBuffer.limit(bytesRead); saveFile.write(byteBuffer); filePos += bytesRead; } else { byteBuffer.limit(endOffset - filePos + 1); saveFile.write(byteBuffer); break; } } } finally { seekableByteChannel.close(); } } }