View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /**
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements. See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership. The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License. You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied. See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  package com.sun.org.apache.xml.internal.security.utils;
24  
25  import java.io.OutputStream;
26  
27  /**
28   * A simple Unsynced ByteArrayOutputStream
29   * @author raul
30   *
31   */
32  public class UnsyncByteArrayOutputStream extends OutputStream  {
33  
34      private static final int INITIAL_SIZE = 8192;
35  
36      private byte[] buf;
37      private int size = INITIAL_SIZE;
38      private int pos = 0;
39  
40      public UnsyncByteArrayOutputStream() {
41          buf = new byte[INITIAL_SIZE];
42      }
43  
44      public void write(byte[] arg0) {
45          if ((Integer.MAX_VALUE - pos) < arg0.length) {
46              throw new OutOfMemoryError();
47          }
48          int newPos = pos + arg0.length;
49          if (newPos > size) {
50              expandSize(newPos);
51          }
52          System.arraycopy(arg0, 0, buf, pos, arg0.length);
53          pos = newPos;
54      }
55  
56      public void write(byte[] arg0, int arg1, int arg2) {
57          if ((Integer.MAX_VALUE - pos) < arg2) {
58              throw new OutOfMemoryError();
59          }
60          int newPos = pos + arg2;
61          if (newPos > size) {
62              expandSize(newPos);
63          }
64          System.arraycopy(arg0, arg1, buf, pos, arg2);
65          pos = newPos;
66      }
67  
68      public void write(int arg0) {
69          if ((Integer.MAX_VALUE - pos) == 0) {
70              throw new OutOfMemoryError();
71          }
72          int newPos = pos + 1;
73          if (newPos > size) {
74              expandSize(newPos);
75          }
76          buf[pos++] = (byte)arg0;
77      }
78  
79      public byte[] toByteArray() {
80          byte result[] = new byte[pos];
81          System.arraycopy(buf, 0, result, 0, pos);
82          return result;
83      }
84  
85      public void reset() {
86          pos = 0;
87      }
88  
89      private void expandSize(int newPos) {
90          int newSize = size;
91          while (newPos > newSize) {
92              newSize = newSize << 1;
93              // Deal with overflow
94              if (newSize < 0) {
95                  newSize = Integer.MAX_VALUE;
96              }
97          }
98          byte newBuf[] = new byte[newSize];
99          System.arraycopy(buf, 0, newBuf, 0, pos);
100         buf = newBuf;
101         size = newSize;
102     }
103 }