public int readNext(boolean[] values, int offset, int length)
            throws IOException
    {
        int nonNullCount = 0;
        int destinationIndex = offset;
        int remainingToCopy = length;
        while (remainingToCopy > 0) {
            if (currentCount == 0) {
                if (!decode()) {
                    break;
                }
            }

            int chunkSize = Math.min(remainingToCopy, currentCount);
            int endIndex = destinationIndex + chunkSize;
            switch (mode) {
                case RLE: {
                    boolean rleValue = currentValue == 0;
                    while (destinationIndex < endIndex) {
                        values[destinationIndex++] = rleValue;
                    }
                    nonNullCount += currentValue * chunkSize;
                    break;
                }
                case PACKED: {
                    int[] buffer = currentBuffer;
                    for (int sourceIndex = buffer.length - currentCount; destinationIndex < endIndex; sourceIndex++, destinationIndex++) {
                        final int value = buffer[sourceIndex];
                        values[destinationIndex] = value == 0;
                        nonNullCount += value;
                    }
                    break;
                }
                default:
                    throw new ParquetDecodingException("not a valid mode " + mode);
            }
            currentCount -= chunkSize;
            remainingToCopy -= chunkSize;
        }

        checkState(remainingToCopy == 0, "Failed to copy the requested number of definition levels");
        return nonNullCount;
    }
@Test
    public void tryReadingTooMany()
    {
        FlatDefinitionLevelDecoder dlDecoder = new FlatDefinitionLevelDecoder(valueCount, new ByteArrayInputStream(pageBytes));
        try {
            dlDecoder.readNext(new boolean[valueCount + 500], 0, valueCount + 500);
            fail("shouldn't come here");
        }
        catch (Exception e) {
            assertEquals(e.getMessage(), "Failed to copy the requested number of definition levels");
        }
    }