class FSDataInputStreamTail {

    public static FSDataInputStreamTail readTail(String path, long paddedFileSize, FSDataInputStream inputStream, int length)
            throws IOException
    {
        checkArgument(length >= 0, "length is negative: %s", length);
        checkArgument(length <= MAXIMUM_READ_LENGTH, "length (%s) exceeds maximum (%s)", length, MAXIMUM_READ_LENGTH);
        long readSize = min(paddedFileSize, (length + MAX_SUPPORTED_PADDING_BYTES));
        long position = paddedFileSize - readSize;
        // Actual read will be 1 byte larger to ensure we encounter an EOF where expected
        byte[] buffer = new byte[toIntExact(readSize + 1)];
        int bytesRead = 0;
        long startPos = inputStream.getPos();
        try {
            inputStream.seek(position);
            while (bytesRead < buffer.length) {
                int n = inputStream.read(buffer, bytesRead, buffer.length - bytesRead);
                if (n < 0) {
                    break;
                }
                bytesRead += n;
            }
        }
        finally {
            inputStream.seek(startPos);
        }
        if (bytesRead > readSize) {
            throw rejectInvalidFileSize(path, paddedFileSize);
        }
        return new FSDataInputStreamTail(position + bytesRead, Slices.wrappedBuffer(buffer, max(0, bytesRead - length), min(bytesRead, length)));
    }

    private  FSDataInputStreamTail(long fileSize, Slice tailSlice);

}