001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.util;
040:
041: import com.sun.xml.fastinfoset.CommonResourceBundle;
042:
043: public class ContiguousCharArrayArray extends ValueArray {
044: public static final int INITIAL_CHARACTER_SIZE = 512;
045: public static final int MAXIMUM_CHARACTER_SIZE = Integer.MAX_VALUE;
046:
047: protected int _maximumCharacterSize;
048:
049: public int[] _offset;
050: public int[] _length;
051:
052: public char[] _array;
053: public int _arrayIndex;
054: public int _readOnlyArrayIndex;
055:
056: private String[] _cachedStrings;
057:
058: public int _cachedIndex;
059:
060: private ContiguousCharArrayArray _readOnlyArray;
061:
062: public ContiguousCharArrayArray(int initialCapacity,
063: int maximumCapacity, int initialCharacterSize,
064: int maximumCharacterSize) {
065: _offset = new int[initialCapacity];
066: _length = new int[initialCapacity];
067: _array = new char[initialCharacterSize];
068: _maximumCapacity = maximumCapacity;
069: _maximumCharacterSize = maximumCharacterSize;
070: }
071:
072: public ContiguousCharArrayArray() {
073: this (DEFAULT_CAPACITY, MAXIMUM_CAPACITY,
074: INITIAL_CHARACTER_SIZE, MAXIMUM_CHARACTER_SIZE);
075: }
076:
077: public final void clear() {
078: _arrayIndex = _readOnlyArrayIndex;
079: _size = _readOnlyArraySize;
080:
081: if (_cachedStrings != null) {
082: for (int i = _readOnlyArraySize; i < _cachedStrings.length; i++) {
083: _cachedStrings[i] = null;
084: }
085: }
086: }
087:
088: public final int getArrayIndex() {
089: return _arrayIndex;
090: }
091:
092: public final void setReadOnlyArray(ValueArray readOnlyArray,
093: boolean clear) {
094: if (!(readOnlyArray instanceof ContiguousCharArrayArray)) {
095: throw new IllegalArgumentException(CommonResourceBundle
096: .getInstance().getString("message.illegalClass",
097: new Object[] { readOnlyArray }));
098: }
099:
100: setReadOnlyArray((ContiguousCharArrayArray) readOnlyArray,
101: clear);
102: }
103:
104: public final void setReadOnlyArray(
105: ContiguousCharArrayArray readOnlyArray, boolean clear) {
106: if (readOnlyArray != null) {
107: _readOnlyArray = readOnlyArray;
108: _readOnlyArraySize = readOnlyArray.getSize();
109: _readOnlyArrayIndex = readOnlyArray.getArrayIndex();
110:
111: if (clear) {
112: clear();
113: }
114:
115: _array = getCompleteCharArray();
116: _offset = getCompleteOffsetArray();
117: _length = getCompleteLengthArray();
118: _size = _readOnlyArraySize;
119: }
120: }
121:
122: public final char[] getCompleteCharArray() {
123: if (_readOnlyArray == null) {
124: return _array;
125: } else {
126: final char[] ra = _readOnlyArray.getCompleteCharArray();
127: final char[] a = new char[_readOnlyArrayIndex
128: + _array.length];
129: System.arraycopy(ra, 0, a, 0, _readOnlyArrayIndex);
130: return a;
131: }
132: }
133:
134: public final int[] getCompleteOffsetArray() {
135: if (_readOnlyArray == null) {
136: return _offset;
137: } else {
138: final int[] ra = _readOnlyArray.getCompleteOffsetArray();
139: final int[] a = new int[_readOnlyArraySize + _offset.length];
140: System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
141: return a;
142: }
143: }
144:
145: public final int[] getCompleteLengthArray() {
146: if (_readOnlyArray == null) {
147: return _length;
148: } else {
149: final int[] ra = _readOnlyArray.getCompleteOffsetArray();
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(
160: _array, _offset[i], _length[i]));
161: }
162:
163: final String[] newCachedStrings = new String[_offset.length];
164: if (_cachedStrings != null && i >= _cachedStrings.length) {
165: System.arraycopy(_cachedStrings, 0, newCachedStrings, 0,
166: _cachedStrings.length);
167: }
168: _cachedStrings = newCachedStrings;
169:
170: return _cachedStrings[i] = new String(_array, _offset[i],
171: _length[i]);
172: }
173:
174: public final void ensureSize(int l) {
175: if (_arrayIndex + l >= _array.length) {
176: resizeArray(_arrayIndex + l);
177: }
178: }
179:
180: public final void add(int l) {
181: if (_size == _offset.length) {
182: resize();
183: }
184:
185: _cachedIndex = _size;
186: _offset[_size] = _arrayIndex;
187: _length[_size++] = l;
188:
189: _arrayIndex += l;
190: }
191:
192: public final int add(char[] c, int l) {
193: if (_size == _offset.length) {
194: resize();
195: }
196:
197: final int oldArrayIndex = _arrayIndex;
198: final int arrayIndex = oldArrayIndex + l;
199:
200: _cachedIndex = _size;
201: _offset[_size] = oldArrayIndex;
202: _length[_size++] = l;
203:
204: if (arrayIndex >= _array.length) {
205: resizeArray(arrayIndex);
206: }
207:
208: System.arraycopy(c, 0, _array, oldArrayIndex, l);
209:
210: _arrayIndex = arrayIndex;
211: return oldArrayIndex;
212: }
213:
214: protected final void resize() {
215: if (_size == _maximumCapacity) {
216: throw new ValueArrayResourceException(CommonResourceBundle
217: .getInstance()
218: .getString("message.arrayMaxCapacity"));
219: }
220:
221: int newSize = _size * 3 / 2 + 1;
222: if (newSize > _maximumCapacity) {
223: newSize = _maximumCapacity;
224: }
225:
226: final int[] offset = new int[newSize];
227: System.arraycopy(_offset, 0, offset, 0, _size);
228: _offset = offset;
229:
230: final int[] length = new int[newSize];
231: System.arraycopy(_length, 0, length, 0, _size);
232: _length = length;
233: }
234:
235: protected final void resizeArray(int requestedSize) {
236: if (_arrayIndex == _maximumCharacterSize) {
237: throw new ValueArrayResourceException(CommonResourceBundle
238: .getInstance().getString(
239: "message.maxNumberOfCharacters"));
240: }
241:
242: int newSize = requestedSize * 3 / 2 + 1;
243: if (newSize > _maximumCharacterSize) {
244: newSize = _maximumCharacterSize;
245: }
246:
247: final char[] array = new char[newSize];
248: System.arraycopy(_array, 0, array, 0, _arrayIndex);
249: _array = array;
250: }
251: }
|