KickJava   Java API By Example, From Geeks To Geeks.

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


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

30
31 package org.jboss.axis.components.encoding;
32
33
34 import java.io.UnsupportedEncodingException JavaDoc;
35
36
37 /**
38  * Simple byte array with variable array length, used within the
39  * <p/>
40  * XMLEncoder.
41  * <p/>
42  * <p/>
43  * <p/>
44  * It is used as a ByteArrayOutputStream replacement to limit
45  * <p/>
46  * the size of created temporary objects
47  *
48  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
49  * @version $Id: EncodedByteArray.java,v 1.1.2.1 2005/03/02 14:28:47 tdiesler Exp $
50  */

51
52 class EncodedByteArray
53 {
54
55    private byte[] array = null;
56
57    private int pointer;
58
59
60    private final double PADDING = 1.5;
61
62
63    public EncodedByteArray(byte[] bytes, int startPos, int length)
64    {
65
66       // string length will be at least the size of the byte array
67

68       array = new byte[(int)(bytes.length * PADDING)];
69
70       System.arraycopy(bytes, startPos, array, 0, length);
71
72       pointer = length;
73
74    }
75
76
77    public EncodedByteArray(int size)
78    {
79
80       array = new byte[size];
81
82    }
83
84
85    public void append(int aByte)
86    {
87
88       if (pointer + 1 >= array.length)
89       {
90
91          byte[] newArray = new byte[(int)(array.length * PADDING)];
92
93          System.arraycopy(array, 0, newArray, 0, pointer);
94
95          array = newArray;
96
97       }
98
99       array[pointer] = (byte)aByte;
100
101       pointer++;
102
103    }
104
105
106    public void append(byte[] byteArray)
107    {
108
109       if (pointer + byteArray.length >= array.length)
110       {
111
112          byte[] newArray = new byte[((int)(array.length * PADDING)) + byteArray.length];
113
114          System.arraycopy(array, 0, newArray, 0, pointer);
115
116          array = newArray;
117
118       }
119
120
121       System.arraycopy(byteArray, 0, array, pointer, byteArray.length);
122
123       pointer += byteArray.length;
124
125    }
126
127
128    public void append(byte[] byteArray, int pos, int length)
129    {
130
131       if (pointer + length >= array.length)
132       {
133
134          byte[] newArray = new byte[((int)(array.length * PADDING)) + byteArray.length];
135
136          System.arraycopy(array, 0, newArray, 0, pointer);
137
138          array = newArray;
139
140       }
141
142       System.arraycopy(byteArray, pos, array, pointer, length);
143
144       pointer += length;
145
146    }
147
148
149    /**
150     * convert to a string using the platform's default charset
151     *
152     * @return string
153     */

154
155    public String JavaDoc toString()
156    {
157
158       return new String JavaDoc(array, 0, pointer);
159
160    }
161
162
163    /**
164     * convert the encoded byte array to a string according to the given charset
165     *
166     * @param charsetName
167     * @return string
168     * @throws UnsupportedEncodingException
169     */

170
171    public String JavaDoc toString(String JavaDoc charsetName) throws UnsupportedEncodingException JavaDoc
172    {
173
174       return new String JavaDoc(array, 0, pointer, charsetName);
175
176    }
177
178 }
179
180
Popular Tags