KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > UnsyncByteArrayOutputStream


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20
21 /**
22  * A simple Unsynced ByteArryOutputStream
23  * @author raul
24  *
25  */

26 public class UnsyncByteArrayOutputStream extends ByteArrayOutputStream JavaDoc {
27     int size=4*1024;
28     byte []buf=new byte[size];
29     int pos;
30     /** @inheritDoc */
31     public void write(byte[] arg0) {
32         int newPos=pos+arg0.length;
33         if (newPos>size) {
34             expandSize();
35         }
36         System.arraycopy(arg0,0,buf,pos,arg0.length);
37         pos=newPos;
38     }
39     /** @inheritDoc */
40     public void write(byte[] arg0, int arg1, int arg2) {
41         int newPos=pos+arg2;
42         if (newPos>size) {
43             expandSize();
44         }
45         System.arraycopy(arg0,arg1,buf,pos,arg2);
46         pos=newPos;
47     }
48     /** @inheritDoc */
49     public void write(int arg0) {
50         if (pos>=size) {
51             expandSize();
52         }
53         buf[pos++]=(byte)arg0;
54     }
55     /** @inheritDoc */
56     public byte[] toByteArray() {
57         byte result[]=new byte[pos];
58         System.arraycopy(buf,0,result,0,pos);
59         return result;
60     }
61     
62     /** @inheritDoc */
63     public void reset() {
64         pos=0;
65     }
66     
67     /** @inheritDoc */
68     void expandSize() {
69         int newSize=size<<2;
70         byte newBuf[]=new byte[newSize];
71         System.arraycopy(buf,0,newBuf,0,pos);
72         buf=newBuf;
73         size=newSize;
74         
75     }
76 }
77
Popular Tags