src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/ContiguousCharArrayArray.java

changeset 286
f50545b5e2f1
parent 0
373ffda63c9a
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
27
28 package com.sun.xml.internal.fastinfoset.util;
29 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
30
31 public class ContiguousCharArrayArray extends ValueArray {
32 public static final int INITIAL_CHARACTER_SIZE = 512;
33 public static final int MAXIMUM_CHARACTER_SIZE = Integer.MAX_VALUE;
34
35 protected int _maximumCharacterSize;
36
37 public int[] _offset;
38 public int[] _length;
39
40 public char[] _array;
41 public int _arrayIndex;
42 public int _readOnlyArrayIndex;
43
44 private String[] _cachedStrings;
45
46 public int _cachedIndex;
47
48 private ContiguousCharArrayArray _readOnlyArray;
49
50 public ContiguousCharArrayArray(int initialCapacity, int maximumCapacity,
51 int initialCharacterSize, int maximumCharacterSize) {
52 _offset = new int[initialCapacity];
53 _length = new int[initialCapacity];
54 _array = new char[initialCharacterSize];
55 _maximumCapacity = maximumCapacity;
56 _maximumCharacterSize = maximumCharacterSize;
57 }
58
59 public ContiguousCharArrayArray() {
60 this(DEFAULT_CAPACITY, MAXIMUM_CAPACITY,
61 INITIAL_CHARACTER_SIZE, MAXIMUM_CHARACTER_SIZE);
62 }
63
64 public final void clear() {
65 _arrayIndex = _readOnlyArrayIndex;
66 _size = _readOnlyArraySize;
67
68 if (_cachedStrings != null) {
69 for (int i = _readOnlyArraySize; i < _cachedStrings.length; i++) {
70 _cachedStrings[i] = null;
71 }
72 }
73 }
74
75 public final int getArrayIndex() {
76 return _arrayIndex;
77 }
78
79 public final void setReadOnlyArray(ValueArray readOnlyArray, boolean clear) {
80 if (!(readOnlyArray instanceof ContiguousCharArrayArray)) {
81 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.illegalClass", new Object[]{readOnlyArray}));
82 }
83
84 setReadOnlyArray((ContiguousCharArrayArray)readOnlyArray, clear);
85 }
86
87 public final void setReadOnlyArray(ContiguousCharArrayArray readOnlyArray, boolean clear) {
88 if (readOnlyArray != null) {
89 _readOnlyArray = readOnlyArray;
90 _readOnlyArraySize = readOnlyArray.getSize();
91 _readOnlyArrayIndex = readOnlyArray.getArrayIndex();
92
93 if (clear) {
94 clear();
95 }
96
97 _array = getCompleteCharArray();
98 _offset = getCompleteOffsetArray();
99 _length = getCompleteLengthArray();
100 _size = _readOnlyArraySize;
101 _arrayIndex = _readOnlyArrayIndex;
102 }
103 }
104
105 public final char[] getCompleteCharArray() {
106 if (_readOnlyArray == null) {
107 if (_array == null) return null;
108
109 // Return cloned version of internal _array
110 final char[] clonedArray = new char[_array.length];
111 System.arraycopy(_array, 0, clonedArray, 0, _array.length);
112 return clonedArray;
113 // return _array;
114 } else {
115 final char[] ra = _readOnlyArray.getCompleteCharArray();
116 final char[] a = new char[_readOnlyArrayIndex + _array.length];
117 System.arraycopy(ra, 0, a, 0, _readOnlyArrayIndex);
118 return a;
119 }
120 }
121
122 public final int[] getCompleteOffsetArray() {
123 if (_readOnlyArray == null) {
124 if (_offset == null) return null;
125
126 // Return cloned version of internal _offset
127 final int[] clonedArray = new int[_offset.length];
128 System.arraycopy(_offset, 0, clonedArray, 0, _offset.length);
129 return clonedArray;
130 // return _offset;
131 } else {
132 final int[] ra = _readOnlyArray.getCompleteOffsetArray();
133 final int[] a = new int[_readOnlyArraySize + _offset.length];
134 System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
135 return a;
136 }
137 }
138
139 public final int[] getCompleteLengthArray() {
140 if (_readOnlyArray == null) {
141 if (_length == null) return null;
142
143 // Return cloned version of internal _length
144 final int[] clonedArray = new int[_length.length];
145 System.arraycopy(_length, 0, clonedArray, 0, _length.length);
146 return clonedArray;
147 // return _length;
148 } else {
149 final int[] ra = _readOnlyArray.getCompleteLengthArray();
150 final int[] a = new int[_readOnlyArraySize + _length.length];
151 System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
152 return a;
153 }
154 }
155
156 public final String getString(int i) {
157 if (_cachedStrings != null && i < _cachedStrings.length) {
158 final String s = _cachedStrings[i];
159 return (s != null) ? s : (_cachedStrings[i] = new String(_array, _offset[i], _length[i]));
160 }
161
162 final String[] newCachedStrings = new String[_offset.length];
163 if (_cachedStrings != null && i >= _cachedStrings.length) {
164 System.arraycopy(_cachedStrings, 0, newCachedStrings, 0, _cachedStrings.length);
165 }
166 _cachedStrings = newCachedStrings;
167
168 return _cachedStrings[i] = new String(_array, _offset[i], _length[i]);
169 }
170
171 public final void ensureSize(int l) {
172 if (_arrayIndex + l >= _array.length) {
173 resizeArray(_arrayIndex + l);
174 }
175 }
176
177 public final void add(int l) {
178 if (_size == _offset.length) {
179 resize();
180 }
181
182 _cachedIndex = _size;
183 _offset[_size] = _arrayIndex;
184 _length[_size++] = l;
185
186 _arrayIndex += l;
187 }
188
189 public final int add(char[] c, int l) {
190 if (_size == _offset.length) {
191 resize();
192 }
193
194 final int oldArrayIndex = _arrayIndex;
195 final int arrayIndex = oldArrayIndex + l;
196
197 _cachedIndex = _size;
198 _offset[_size] = oldArrayIndex;
199 _length[_size++] = l;
200
201 if (arrayIndex >= _array.length) {
202 resizeArray(arrayIndex);
203 }
204
205 System.arraycopy(c, 0, _array, oldArrayIndex, l);
206
207 _arrayIndex = arrayIndex;
208 return oldArrayIndex;
209 }
210
211 protected final void resize() {
212 if (_size == _maximumCapacity) {
213 throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.arrayMaxCapacity"));
214 }
215
216 int newSize = _size * 3 / 2 + 1;
217 if (newSize > _maximumCapacity) {
218 newSize = _maximumCapacity;
219 }
220
221 final int[] offset = new int[newSize];
222 System.arraycopy(_offset, 0, offset, 0, _size);
223 _offset = offset;
224
225 final int[] length = new int[newSize];
226 System.arraycopy(_length, 0, length, 0, _size);
227 _length = length;
228 }
229
230 protected final void resizeArray(int requestedSize) {
231 if (_arrayIndex == _maximumCharacterSize) {
232 throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.maxNumberOfCharacters"));
233 }
234
235 int newSize = requestedSize * 3 / 2 + 1;
236 if (newSize > _maximumCharacterSize) {
237 newSize = _maximumCharacterSize;
238 }
239
240 final char[] array = new char[newSize];
241 System.arraycopy(_array, 0, array, 0, _arrayIndex);
242 _array = array;
243 }
244 }

mercurial