@Override
    protected void ensureAvailableBytes(int size)
    {
        if (offset + size > bufferSize)
        {
            try
            {
                int availableBytes = (int) (fileChannel.size() - fileChannel.position());
                ByteBuffer byteBuffer = ByteBuffer.allocate(Math.min(availableBytes, offset));
                int readBytes = fileChannel.read(byteBuffer);
                if (readBytes == availableBytes)
                {
                    fileChannel.close();
                    fileChannel = null;
                }
                assert readBytes > 0 : "已到达文件尾部！";
                byteBuffer.flip();
                byte[] bytes = byteBuffer.array();
                System.arraycopy(this.bytes, offset, this.bytes, offset - readBytes, bufferSize - offset);
                System.arraycopy(bytes, 0, this.bytes, bufferSize - readBytes, readBytes);
                offset -= readBytes;
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
@ Override protected void ensureAvailableBytes ( int size ) { if ( offset + size > bufferSize ) { try { int availableBytes = ( int ) ( fileChannel . size ( ) - fileChannel . position ( ) ) ; ByteBuffer byteBuffer = ByteBuffer . allocate ( Math . min ( availableBytes , offset ) ) ; int readBytes = fileChannel . read ( byteBuffer ) ; if ( readBytes == availableBytes ) { fileChannel . close ( ) ; fileChannel = null ; } assert readBytes > 0 : "已到达文件尾部！" ; byteBuffer . flip ( ) ; byte [ ] bytes = byteBuffer . array ( ) ; System . arraycopy ( this . bytes , offset , this . bytes , offset - readBytes , bufferSize - offset ) ; System . arraycopy ( bytes , 0 , this . bytes , bufferSize - readBytes , readBytes ) ; offset -= readBytes ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } } }
确保buffer数组余有size个字节
@param size