<commit_before>package com.googlecode.pngtastic.core;

import com.googlecode.pngtastic.core.processing.PngCompressionHandler;
import com.googlecode.pngtastic.core.processing.PngFilterHandler;
import com.googlecode.pngtastic.core.processing.PngInterlaceHandler;
import com.googlecode.pngtastic.core.processing.PngtasticCompressionHandler;
import com.googlecode.pngtastic.core.processing.PngtasticFilterHandler;
import com.googlecode.pngtastic.core.processing.PngtasticInterlaceHandler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.InflaterInputStream;

/**
 * Base class for png image processing
 *
 * @author ray
 */
public abstract class PngProcessor {

	protected final Logger log;
	protected final PngFilterHandler pngFilterHandler;
	protected final PngInterlaceHandler pngInterlaceHandler;

	protected PngCompressionHandler pngCompressionHandler;

	protected PngProcessor(String logLevel) {
		this.log = new Logger(logLevel);
		this.pngFilterHandler = new PngtasticFilterHandler(log);
		this.pngInterlaceHandler = new PngtasticInterlaceHandler(log, pngFilterHandler);
		this.pngCompressionHandler = new PngtasticCompressionHandler(log);

	}

	protected byte[] getInflatedImageData(PngChunk chunk, Iterator<PngChunk> itChunks) throws IOException {
		final ByteArrayOutputStream imageBytes = new ByteArrayOutputStream(chunk == null ? 0 : chunk.getLength());
		final DataOutputStream imageData = new DataOutputStream(imageBytes);
		while (chunk != null) {
			if (PngChunk.IMAGE_DATA.equals(chunk.getTypeString())) {
				imageData.write(chunk.getData());
			} else {
				break;
			}
			chunk = itChunks.hasNext() ? itChunks.next() : null;
		}
		imageData.close();

		return inflate(imageBytes);
	}

	/**
	 * Inflate (decompress) the compressed image data
	 *
	 * @param imageBytes A stream containing the compressed image data
	 * @return A byte array containing the uncompressed data
	 */
	public byte[] inflate(ByteArrayOutputStream imageBytes) throws IOException {
		InflaterInputStream inflater = new InflaterInputStream(new ByteArrayInputStream(imageBytes.toByteArray()));
		ByteArrayOutputStream inflatedOut = new ByteArrayOutputStream();

		int readLength;
		byte[] block = new byte[8192];
		while ((readLength = inflater.read(block)) != -1) {
			inflatedOut.write(block, 0, readLength);
		}

		byte[] inflatedImageData = inflatedOut.toByteArray();
		return inflatedImageData;
	}

	protected List<byte[]> getScanlines(byte[] inflatedImageData, int sampleBitCount, int rowLength, long height) {
		final List<byte[]> rows = new ArrayList<>(Math.max((int) height, 0));
		byte[] previousRow = new byte[rowLength];

		for (int i = 0; i < height; i++) {
			final int offset = i * rowLength;
			final byte[] row = new byte[rowLength];
			System.arraycopy(inflatedImageData, offset, row, 0, rowLength);
			try {
				pngFilterHandler.deFilter(row, previousRow, sampleBitCount);
				rows.add(row);
				previousRow = row.clone();
			} catch (PngException e) {
				log.error("Error: %s", e.getMessage());
			}
		}
		return rows;
	}

	protected PngChunk processHeadChunks(PngImage result, boolean removeGamma, Iterator<PngChunk> itChunks) throws IOException {
		PngChunk chunk = null;
		while (itChunks.hasNext()) {
			chunk = itChunks.next();
			if (PngChunk.IMAGE_DATA.equals(chunk.getTypeString())) {
				break;
			}

			if (result != null && chunk.isRequired()) {
				if (removeGamma && PngChunk.IMAGE_GAMA.equalsIgnoreCase(chunk.getTypeString())) {
					continue;
				}
				ByteArrayOutputStream bytes = new ByteArrayOutputStream(chunk.getLength());
				DataOutputStream data = new DataOutputStream(bytes);

				data.write(chunk.getData());
				data.close();

				PngChunk newChunk = new PngChunk(chunk.getType(), bytes.toByteArray());
				if (PngChunk.IMAGE_HEADER.equals(chunk.getTypeString())) {
					newChunk.setInterlace((byte) 0);
				}
				result.addChunk(newChunk);
			}
		}
		return chunk;
	}
}<commit_msg>Use try-with-resources<commit_after>package com.googlecode.pngtastic.core;

import com.googlecode.pngtastic.core.processing.PngCompressionHandler;
import com.googlecode.pngtastic.core.processing.PngFilterHandler;
import com.googlecode.pngtastic.core.processing.PngInterlaceHandler;
import com.googlecode.pngtastic.core.processing.PngtasticCompressionHandler;
import com.googlecode.pngtastic.core.processing.PngtasticFilterHandler;
import com.googlecode.pngtastic.core.processing.PngtasticInterlaceHandler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.InflaterInputStream;

/**
 * Base class for png image processing
 *
 * @author ray
 */
public abstract class PngProcessor {

	protected final Logger log;
	protected final PngFilterHandler pngFilterHandler;
	protected final PngInterlaceHandler pngInterlaceHandler;

	protected PngCompressionHandler pngCompressionHandler;

	protected PngProcessor(String logLevel) {
		this.log = new Logger(logLevel);
		this.pngFilterHandler = new PngtasticFilterHandler(log);
		this.pngInterlaceHandler = new PngtasticInterlaceHandler(log, pngFilterHandler);
		this.pngCompressionHandler = new PngtasticCompressionHandler(log);

	}

	protected byte[] getInflatedImageData(PngChunk chunk, Iterator<PngChunk> itChunks) throws IOException {
		final ByteArrayOutputStream imageBytes = new ByteArrayOutputStream(chunk == null ? 0 : chunk.getLength());
		try (final DataOutputStream imageData = new DataOutputStream(imageBytes)) {
			while (chunk != null) {
				if (PngChunk.IMAGE_DATA.equals(chunk.getTypeString())) {
					imageData.write(chunk.getData());
				} else {
					break;
				}
				chunk = itChunks.hasNext() ? itChunks.next() : null;
			}

			return inflate(imageBytes);
		}
	}

	/**
	 * Inflate (decompress) the compressed image data
	 *
	 * @param imageBytes A stream containing the compressed image data
	 * @return A byte array containing the uncompressed data
	 */
	public byte[] inflate(ByteArrayOutputStream imageBytes) throws IOException {
		InflaterInputStream inflater = new InflaterInputStream(new ByteArrayInputStream(imageBytes.toByteArray()));
		try (final ByteArrayOutputStream inflatedOut = new ByteArrayOutputStream()) {
			int readLength;
			final byte[] block = new byte[8192];
			while ((readLength = inflater.read(block)) != -1) {
				inflatedOut.write(block, 0, readLength);
			}

			return inflatedOut.toByteArray();
		}
	}

	protected List<byte[]> getScanlines(byte[] inflatedImageData, int sampleBitCount, int rowLength, long height) {
		final List<byte[]> rows = new ArrayList<>(Math.max((int) height, 0));
		byte[] previousRow = new byte[rowLength];

		for (int i = 0; i < height; i++) {
			final int offset = i * rowLength;
			final byte[] row = new byte[rowLength];
			System.arraycopy(inflatedImageData, offset, row, 0, rowLength);
			try {
				pngFilterHandler.deFilter(row, previousRow, sampleBitCount);
				rows.add(row);
				previousRow = row.clone();
			} catch (PngException e) {
				log.error("Error: %s", e.getMessage());
			}
		}
		return rows;
	}

	protected PngChunk processHeadChunks(PngImage result, boolean removeGamma, Iterator<PngChunk> itChunks) throws IOException {
		PngChunk chunk = null;
		while (itChunks.hasNext()) {
			chunk = itChunks.next();
			if (PngChunk.IMAGE_DATA.equals(chunk.getTypeString())) {
				break;
			}

			if (result != null && chunk.isRequired()) {
				if (removeGamma && PngChunk.IMAGE_GAMA.equalsIgnoreCase(chunk.getTypeString())) {
					continue;
				}

				PngChunk newChunk = new PngChunk(chunk.getType(), chunk.getData());
				if (PngChunk.IMAGE_HEADER.equals(chunk.getTypeString())) {
					newChunk.setInterlace((byte) 0);
				}
				result.addChunk(newChunk);
			}
		}
		return chunk;
	}
}
{'fraction_non_alphanumeric': 0.11018113678950656, 'fraction_numerical': 0.0027482823235477826, 'mean_word_length': 4.943578322197476, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '676791', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2382, 'n_words': 537}