KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > encoding > EncodedByteArray


1 /*
2  * Copyright 2001-2004 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 package org.apache.axis.components.encoding;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19
20 /**
21  *
22  * Simple byte array with variable array length, used within the
23  * XMLEncoder.
24  *
25  * It is used as a ByteArrayOutputStream replacement to limit
26  * the size of created temporary objects
27  *
28  *
29  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
30  */

31 class EncodedByteArray {
32     private byte[] array = null;
33     private int pointer;
34     
35     private final double PADDING = 1.5;
36
37     public EncodedByteArray(byte[] bytes, int startPos, int length) {
38         // string length will be at least the size of the byte array
39
array = new byte[(int) (bytes.length * PADDING)];
40         System.arraycopy(bytes, startPos, array, 0, length);
41         pointer = length;
42     }
43
44     public EncodedByteArray(int size) {
45         array = new byte[size];
46     }
47
48     public void append(int aByte) {
49         if (pointer + 1 >= array.length) {
50             byte[] newArray = new byte[(int) (array.length * PADDING)];
51             System.arraycopy(array, 0, newArray, 0, pointer);
52             array = newArray;
53         }
54         array[pointer] = (byte) aByte;
55         pointer++;
56     }
57
58     public void append(byte[] byteArray) {
59         if (pointer + byteArray.length >= array.length) {
60             byte[] newArray = new byte[((int)(array.length * PADDING)) + byteArray.length];
61             System.arraycopy(array, 0, newArray, 0, pointer);
62             array = newArray;
63         }
64
65         System.arraycopy(byteArray, 0, array, pointer, byteArray.length);
66         pointer += byteArray.length;
67     }
68
69     public void append(byte[] byteArray, int pos, int length) {
70         if (pointer + length >= array.length) {
71             byte[] newArray = new byte[((int) (array.length * PADDING)) + byteArray.length];
72             System.arraycopy(array, 0, newArray, 0, pointer);
73             array = newArray;
74         }
75         System.arraycopy(byteArray, pos, array, pointer, length);
76         pointer += length;
77     }
78
79     /**
80      * convert to a string using the platform's default charset
81      * @return string
82      */

83     public String JavaDoc toString() {
84         return new String JavaDoc(array, 0, pointer);
85     }
86
87     /**
88      * convert the encoded byte array to a string according to the given charset
89      * @param charsetName
90      * @return string
91      * @throws UnsupportedEncodingException
92      */

93     public String JavaDoc toString(String JavaDoc charsetName) throws UnsupportedEncodingException JavaDoc {
94         return new String JavaDoc(array, 0, pointer, charsetName);
95     }
96 }
97
Popular Tags