FileChannelBuffer extends Buffer { @Override public short getShort() { assert remaining() >= Short.BYTES; final ByteBuffer shortBuf = shortBufCache.get(); try { final int c = ch.read(shortBuf, this.offset + this.position); assert c == Short.BYTES; } catch (IOException e) { throw new RuntimeException(e); } this.position += Short.BYTES; return shortBuf.getShort(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 public void getShortTest() throws IOException { short data = 32765; byte[] bytes = new byte[]{ (byte) ((data >> 8) & 0xff), (byte) ((data) & 0xff) }; final Path path = nextTempFile(bytes); final FileChannel read = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE); final Buffer buf = Buffer.from(read); short result = buf.getShort(); assertEquals(data, result); }
@Test public void getShortTestWithIndex() throws IOException { short data = 32765; byte[] bytes = new byte[]{ (byte) ((data >> 8) & 0xff), (byte) ((data) & 0xff) }; final Path path = nextTempFile(bytes); final FileChannel read = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE); final Buffer buf = Buffer.from(read); short result = buf.getShort(0); assertEquals(data, result); }
@Test(expected = RuntimeException.class) public void getShortBroken() 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).getShort(); }
@Test(expected = RuntimeException.class) public void getShortByIndexBroken() 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).getShort(0L); }
@Test public void getShort() throws IOException { final short snum = 123; final byte[] bytes = Shorts.toByteArray(snum); final Path path = nextTempFile(bytes); final FileChannel read = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE); final Buffer buf = Buffer.from(read); assertEquals(snum, buf.getShort()); }
@Test public void getShortByIndex() throws IOException { final short snum = 123; final byte[] bytes = Shorts.toByteArray(snum); final Path path = nextTempFile(bytes); final FileChannel read = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE); final Buffer buf = Buffer.from(read); assertEquals(snum, buf.getShort(0)); }