<commit_before>package org.codehaus.groovy.grails.web.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.util.Arrays;
import java.util.LinkedList;

/**
 *
 * StreamByteBuffer is a in-memory buffer that provides OutputStream and
 * InputStream interfaces
 *
 * This is more efficient than using ByteArrayOutputStream/ByteArrayInputStream
 *
 * This is not thread-safe, it is intended to be used by a single Thread.
 *
 *
 * @author <NAME>, <NAME>
 *
 */
public class StreamByteBuffer {
	private static final int DEFAULT_CHUNK_SIZE = 8192;

	private LinkedList<StreamByteBufferChunk> chunks = new LinkedList<StreamByteBufferChunk>();
	private StreamByteBufferChunk currentWriteChunk;
	private StreamByteBufferChunk currentReadChunk = null;
	private int chunkSize;
	private StreamByteBufferOutputStream output;
	private StreamByteBufferInputStream input;
	private int totalBytesUnreadInList = 0;

	public StreamByteBuffer() {
		this(DEFAULT_CHUNK_SIZE);
	}

	public StreamByteBuffer(int chunkSize) {
		this.chunkSize = chunkSize;
		currentWriteChunk = new StreamByteBufferChunk(chunkSize);
		output = new StreamByteBufferOutputStream();
		input = new StreamByteBufferInputStream();
	}

	public OutputStream getOutputStream() {
		return output;
	}

	public InputStream getInputStream() {
		return input;
	}

	public void writeTo(OutputStream target) throws IOException {
		while (prepareRead() != -1) {
			currentReadChunk.writeTo(target);
		}
	}

	public byte[] readAsByteArray() {
		byte[] buf = new byte[totalBytesUnread()];
		input.readImpl(buf, 0, buf.length);
		return buf;
	}

	public String readAsString(String encoding) throws CharacterCodingException {
		Charset charset = Charset.forName(encoding);
		return readAsString(charset);
	}

	public String readAsString(Charset charset) throws CharacterCodingException {
		int unreadSize = totalBytesUnread();
		if (unreadSize > 0) {
			CharsetDecoder decoder = charset.newDecoder().onMalformedInput(
					CodingErrorAction.REPLACE).onUnmappableCharacter(
					CodingErrorAction.REPLACE);
			CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
			ByteBuffer buf = null;
			while (prepareRead() != -1) {
				buf = currentReadChunk.readToNioBuffer();
				boolean endOfInput = (prepareRead() == -1);
				CoderResult result = decoder
						.decode(buf, charbuffer, endOfInput);
				if (endOfInput) {
					if(!result.isUnderflow()) {
						result.throwException();
					}
				}
			}
			CoderResult result = decoder.flush(charbuffer);
			if(buf.hasRemaining()) {
				throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
			}
			if (!result.isUnderflow())
				result.throwException();
			charbuffer.flip();
			String str;
			if(charbuffer.hasArray()) {
				int len=charbuffer.remaining();
				char[] ch=charbuffer.array();
				if(len != ch.length) {
					ch=Arrays.copyOfRange(ch, 0, len);
				}
				str=StringCharArrayAccessor.createString(ch);
			} else {
				str=charbuffer.toString();
			}
			return str;
		}
		return null;
	}

	public int totalBytesUnread() {
		int total = totalBytesUnreadInList;
		if (currentReadChunk != null) {
			total += currentReadChunk.bytesUnread();
		}
		if (currentWriteChunk != currentReadChunk && currentWriteChunk != null) {
			total += currentWriteChunk.bytesUnread();
		}
		return total;
	}

	protected int allocateSpace() {
		int spaceLeft = currentWriteChunk.spaceLeft();
		if (spaceLeft == 0) {
			chunks.add(currentWriteChunk);
			totalBytesUnreadInList += currentWriteChunk.bytesUnread();
			currentWriteChunk = new StreamByteBufferChunk(chunkSize);
			spaceLeft = currentWriteChunk.spaceLeft();
		}
		return spaceLeft;<commit_msg>class used Java 6 methods, changing to use ArrayUtils to remain Java 5 compatible<commit_after>package org.codehaus.groovy.grails.web.util;

import org.apache.commons.lang.ArrayUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;
import java.util.LinkedList;

/**
 *
 * StreamByteBuffer is a in-memory buffer that provides OutputStream and
 * InputStream interfaces
 *
 * This is more efficient than using ByteArrayOutputStream/ByteArrayInputStream
 *
 * This is not thread-safe, it is intended to be used by a single Thread.
 *
 *
 * @author <NAME>, <NAME>
 *
 */
public class StreamByteBuffer {
	private static final int DEFAULT_CHUNK_SIZE = 8192;

	private LinkedList<StreamByteBufferChunk> chunks = new LinkedList<StreamByteBufferChunk>();
	private StreamByteBufferChunk currentWriteChunk;
	private StreamByteBufferChunk currentReadChunk = null;
	private int chunkSize;
	private StreamByteBufferOutputStream output;
	private StreamByteBufferInputStream input;
	private int totalBytesUnreadInList = 0;

	public StreamByteBuffer() {
		this(DEFAULT_CHUNK_SIZE);
	}

	public StreamByteBuffer(int chunkSize) {
		this.chunkSize = chunkSize;
		currentWriteChunk = new StreamByteBufferChunk(chunkSize);
		output = new StreamByteBufferOutputStream();
		input = new StreamByteBufferInputStream();
	}

	public OutputStream getOutputStream() {
		return output;
	}

	public InputStream getInputStream() {
		return input;
	}

	public void writeTo(OutputStream target) throws IOException {
		while (prepareRead() != -1) {
			currentReadChunk.writeTo(target);
		}
	}

	public byte[] readAsByteArray() {
		byte[] buf = new byte[totalBytesUnread()];
		input.readImpl(buf, 0, buf.length);
		return buf;
	}

	public String readAsString(String encoding) throws CharacterCodingException {
		Charset charset = Charset.forName(encoding);
		return readAsString(charset);
	}

	public String readAsString(Charset charset) throws CharacterCodingException {
		int unreadSize = totalBytesUnread();
		if (unreadSize > 0) {
			CharsetDecoder decoder = charset.newDecoder().onMalformedInput(
					CodingErrorAction.REPLACE).onUnmappableCharacter(
					CodingErrorAction.REPLACE);
			CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
			ByteBuffer buf = null;
			while (prepareRead() != -1) {
				buf = currentReadChunk.readToNioBuffer();
				boolean endOfInput = (prepareRead() == -1);
				CoderResult result = decoder
						.decode(buf, charbuffer, endOfInput);
				if (endOfInput) {
					if(!result.isUnderflow()) {
						result.throwException();
					}
				}
			}
			CoderResult result = decoder.flush(charbuffer);
			if(buf.hasRemaining()) {
				throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
			}
			if (!result.isUnderflow())
				result.throwException();
			charbuffer.flip();
			String str;
			if(charbuffer.hasArray()) {
				int len=charbuffer.remaining();
				char[] ch=charbuffer.array();
				if(len != ch.length) {
					ch= ArrayUtils.subarray(ch, 0, len);
				}
				str=StringCharArrayAccessor.createString(ch);
			} else {
				str=charbuffer.toString();
			}
			return str;
		}
		return null;
	}

	public int totalBytesUnread() {
		int total = totalBytesUnreadInList;
		if (currentReadChunk != null) {
			total += currentReadChunk.bytesUnread();
		}
		if (currentWriteChunk != currentReadChunk && currentWriteChunk != null) {
			total += currentWriteChunk.bytesUnread();
		}
		return total;
	}

	protected int allocateSpace() {
		int spaceLeft = currentWriteChunk.spaceLeft();
		if (spaceLeft == 0) {
			chunks.add(currentWriteChunk);
			totalBytesUnreadInList += currentWriteChunk.bytesUnread();
			currentWriteChunk = new StreamByteBufferChunk(chunkSize);
			spaceLeft = currentWriteChunk.spaceLeft();
		}
		return spaceLeft;
{'fraction_non_alphanumeric': 0.10409491006505932, 'fraction_numerical': 0.003316749585406302, 'mean_word_length': 4.5761024182076815, 'pattern_counts': {'":': 0, '<': 11, '<?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': '417929', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2309, 'n_words': 551}