FileChannelBuffer extends Buffer { @Override public char getChar() { assert remaining() >= Character.BYTES; final ByteBuffer charBuf = charBufCache.get(); try { final int c = ch.read(charBuf, this.offset + this.position); assert c == Character.BYTES; } catch (IOException e) { throw new RuntimeException(e); } this.position += Character.BYTES; return charBuf.getChar(0); } FileChannelBuffer(
            @NotNull
            final FileChannel ch); private  FileChannelBuffer(
            @NotNull
            final FileChannel ch,
            final long offset,
            final long limit); @Override long position(); @Override Buffer position(final long position); @Override Buffer advance(final long bytes); @Override long limit(); @Override boolean hasRemaining(); @Override long remaining(); @Override Buffer get(byte[] dst, int offset, int length); @Override Buffer get(final byte[] dst); @Override byte get(); @Override byte get(final long index); @Override int getInt(); @Override int getInt(final long index); @Override long getLong(); @Override long getLong(final long index); @Override char getChar(); @Override char getChar(final long index); @Override short getShort(); @Override short getShort(final long index); @Override Buffer slice(final long from, final long size);  }
@Test(expected = RuntimeException.class) public void getCharBroken() throws IOException { final FileChannel broken = mock(FileChannel.class); when(broken.size()).thenReturn(1024L); when(broken.read(any(ByteBuffer.class), anyLong())) .thenThrow(new IOException("Test")); new FileChannelBuffer(broken).getChar(); }
@Test(expected = RuntimeException.class) public void getCharByIndexBroken() throws IOException { final FileChannel broken = mock(FileChannel.class); when(broken.size()).thenReturn(1024L); when(broken.read(any(ByteBuffer.class), anyLong())) .thenThrow(new IOException("Test")); new FileChannelBuffer(broken).getChar(0L); }
@Test public void getChar() throws IOException { final char ch = 'a'; final byte[] bytes = Chars.toByteArray(ch); final Path path = nextTempFile(bytes); final FileChannel read = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE); final Buffer buf = Buffer.from(read); assertEquals(ch, buf.getChar()); }
@Test public void getCharByIndex() throws IOException { final char ch = 'a'; final byte[] bytes = Chars.toByteArray(ch); final Path path = nextTempFile(bytes); final FileChannel read = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE); final Buffer buf = Buffer.from(read); assertEquals(ch, buf.getChar(0)); }