0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
2932N/A * Copyright 1999-2010 The Apache Software Foundation.
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A *
0N/A */
0N/Apackage com.sun.org.apache.xml.internal.security.utils;
0N/A
661N/Aimport java.io.OutputStream;
0N/A
0N/A/**
2932N/A * A simple Unsynced ByteArrayOutputStream
0N/A * @author raul
0N/A *
0N/A */
661N/Apublic class UnsyncByteArrayOutputStream extends OutputStream {
2932N/A private static final int INITIAL_SIZE = 8192;
2932N/A private static ThreadLocal bufCache = new ThreadLocal() {
661N/A protected synchronized Object initialValue() {
2932N/A return new byte[INITIAL_SIZE];
661N/A }
661N/A };
2932N/A
2932N/A private byte[] buf;
2932N/A private int size = INITIAL_SIZE;
2932N/A private int pos = 0;
2932N/A
2932N/A public UnsyncByteArrayOutputStream() {
2932N/A buf = (byte[])bufCache.get();
2932N/A }
2932N/A
2932N/A public void write(byte[] arg0) {
2932N/A int newPos = pos + arg0.length;
2932N/A if (newPos > size) {
2932N/A expandSize(newPos);
661N/A }
2932N/A System.arraycopy(arg0, 0, buf, pos, arg0.length);
2932N/A pos = newPos;
2932N/A }
2932N/A
2932N/A public void write(byte[] arg0, int arg1, int arg2) {
2932N/A int newPos = pos + arg2;
2932N/A if (newPos > size) {
2932N/A expandSize(newPos);
0N/A }
2932N/A System.arraycopy(arg0, arg1, buf, pos, arg2);
2932N/A pos = newPos;
2932N/A }
2932N/A
2932N/A public void write(int arg0) {
2932N/A int newPos = pos + 1;
2932N/A if (newPos > size) {
2932N/A expandSize(newPos);
0N/A }
2932N/A buf[pos++] = (byte)arg0;
2932N/A }
0N/A
2932N/A public byte[] toByteArray() {
2932N/A byte result[] = new byte[pos];
2932N/A System.arraycopy(buf, 0, result, 0, pos);
2932N/A return result;
2932N/A }
2932N/A
2932N/A public void reset() {
2932N/A pos = 0;
2932N/A }
0N/A
2932N/A private void expandSize(int newPos) {
2932N/A int newSize = size;
2932N/A while (newPos > newSize) {
2932N/A newSize = newSize<<2;
0N/A }
2932N/A byte newBuf[] = new byte[newSize];
2932N/A System.arraycopy(buf, 0, newBuf, 0, pos);
2932N/A buf = newBuf;
2932N/A size = newSize;
2932N/A }
0N/A}