/* *************************************************************************************** * Copyright (C) 2006 EsperTech, Inc. All rights reserved. * * http://www.espertech.com/esper * * http://www.espertech.com * * ---------------------------------------------------------------------------------- * * The software in this package is published under the terms of the GPL license * * a copy of which has been included with this distribution in the license.txt file. * *************************************************************************************** */ package com.espertech.esper.supportregression.util; import java.io.InputStream; import java.io.OutputStream; public class SimpleByteArrayOutputStream extends OutputStream { protected byte[] buf = null; protected int size = 0; public SimpleByteArrayOutputStream() { this(5 * 1024); } public SimpleByteArrayOutputStream(int initSize) { this.size = 0; this.buf = new byte[initSize]; } /** * Ensures that we have a large enough buffer for the given size. */ private void verifyBufferSize(int sz) { if (sz > buf.length) { byte[] old = buf; buf = new byte[Math.max(sz, 2 * buf.length )]; System.arraycopy(old, 0, buf, 0, old.length); old = null; } } public int getSize() { return size; } public byte[] getByteArray() { return buf; } public final void write(byte b[]) { verifyBufferSize(size + b.length); System.arraycopy(b, 0, buf, size, b.length); size += b.length; } public final void write(byte b[], int off, int len) { verifyBufferSize(size + len); System.arraycopy(b, off, buf, size, len); size += len; } public final void write(int b) { verifyBufferSize(size + 1); buf[size++] = (byte) b; } public void reset() { size = 0; } public InputStream getInputStream() { return new SimpleByteArrayInputStream(buf, size); } }