private List<PngChunk> readChunks(final InputStream is, final ChunkType[] chunkTypes, final boolean returnAfterFirst) throws ImagingException, IOException {
        final List<PngChunk> result = new ArrayList<>();

        while (true) {
            final int length = BinaryFunctions.read4Bytes("Length", is, "Not a Valid PNG File", getByteOrder());
            if (length < 0) {
                throw new ImagingException("Invalid PNG chunk length: " + length);
            }
            final int chunkType = BinaryFunctions.read4Bytes("ChunkType", is, "Not a Valid PNG File", getByteOrder());

            if (LOGGER.isLoggable(Level.FINEST)) {
                BinaryFunctions.logCharQuad("ChunkType", chunkType);
                debugNumber("Length", length, 4);
            }
            final boolean keep = keepChunk(chunkType, chunkTypes);

            byte[] bytes = null;
            if (keep) {
                bytes = BinaryFunctions.readBytes("Chunk Data", is, length, "Not a Valid PNG File: Couldn't read Chunk Data.");
            } else {
                BinaryFunctions.skipBytes(is, length, "Not a Valid PNG File");
            }

            if (LOGGER.isLoggable(Level.FINEST) && bytes != null) {
                debugNumber("bytes", bytes.length, 4);
            }

            final int crc = BinaryFunctions.read4Bytes("CRC", is, "Not a Valid PNG File", getByteOrder());

            if (keep) {
                result.add(ChunkType.makeChunk(length, chunkType, crc, bytes));

                if (returnAfterFirst) {
                    return result;
                }
            }

            if (chunkType == ChunkType.IEND.value) {
                break;
            }

        }

        return result;

    }
Gets TIFF image metadata for a byte source.

@param byteSource The source of the image.
@return TIFF image metadata.
@throws ImagingException In the event that the specified content does not conform to the format of the specific parser implementation.
@throws IOException      In the event of unsuccessful data read operation.
@since 1.0-alpha6
src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java