KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > util > ContiguousCharArrayArray


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.util;
41 import com.sun.xml.fastinfoset.CommonResourceBundle;
42
43 public class ContiguousCharArrayArray extends ValueArray {
44     public static final int INITIAL_CHARACTER_SIZE = 512;
45     public static final int MAXIMUM_CHARACTER_SIZE = Integer.MAX_VALUE;
46     
47     protected int _maximumCharacterSize;
48     
49     public int[] _offset;
50     public int[] _length;
51     
52     public char[] _array;
53     public int _arrayIndex;
54     public int _readOnlyArrayIndex;
55     
56     private CharArray _charArray = new CharArray();
57     
58     private ContiguousCharArrayArray _readOnlyArray;
59     
60     public ContiguousCharArrayArray(int initialCapacity, int maximumCapacity,
61             int initialCharacterSize, int maximumCharacterSize) {
62         _offset = new int[initialCapacity];
63         _length = new int[initialCapacity];
64         _array = new char[initialCharacterSize];
65         _charArray.ch = _array;
66         _maximumCapacity = maximumCapacity;
67         _maximumCharacterSize = maximumCharacterSize;
68     }
69
70     public ContiguousCharArrayArray() {
71         this(DEFAULT_CAPACITY, MAXIMUM_CAPACITY,
72                 INITIAL_CHARACTER_SIZE, MAXIMUM_CHARACTER_SIZE);
73     }
74     
75     public final void clear() {
76         _arrayIndex = _readOnlyArrayIndex;
77         _size = _readOnlyArraySize;
78     }
79
80     public final int getArrayIndex() {
81         return _arrayIndex;
82     }
83     
84     public final void setReadOnlyArray(ValueArray readOnlyArray, boolean clear) {
85         if (!(readOnlyArray instanceof ContiguousCharArrayArray)) {
86             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.illegalClass", new Object JavaDoc[]{readOnlyArray}));
87         }
88         
89         setReadOnlyArray((ContiguousCharArrayArray)readOnlyArray, clear);
90     }
91
92     public final void setReadOnlyArray(ContiguousCharArrayArray readOnlyArray, boolean clear) {
93         if (readOnlyArray != null) {
94             _readOnlyArray = readOnlyArray;
95             _readOnlyArraySize = readOnlyArray.getSize();
96             _readOnlyArrayIndex = readOnlyArray.getArrayIndex();
97             
98             if (clear) {
99                 clear();
100             }
101
102             _charArray.ch = _array = getCompleteCharArray();
103             _offset = getCompleteOffsetArray();
104             _length = getCompleteLengthArray();
105             _size = _readOnlyArraySize;
106         }
107     }
108
109     public final char[] getCompleteCharArray() {
110         if (_readOnlyArray == null) {
111             return _array;
112         } else {
113             final char[] ra = _readOnlyArray.getCompleteCharArray();
114             final char[] a = new char[_readOnlyArrayIndex + _array.length];
115             System.arraycopy(ra, 0, a, 0, _readOnlyArrayIndex);
116             return a;
117         }
118     }
119     
120     public final int[] getCompleteOffsetArray() {
121         if (_readOnlyArray == null) {
122             return _offset;
123         } else {
124             final int[] ra = _readOnlyArray.getCompleteOffsetArray();
125             final int[] a = new int[_readOnlyArraySize + _offset.length];
126             System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
127             return a;
128         }
129     }
130     
131     public final int[] getCompleteLengthArray() {
132         if (_readOnlyArray == null) {
133             return _length;
134         } else {
135             final int[] ra = _readOnlyArray.getCompleteOffsetArray();
136             final int[] a = new int[_readOnlyArraySize + _length.length];
137             System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
138             return a;
139         }
140     }
141     
142     public final CharArray get(int i) {
143         _charArray.start = _offset[i];
144         _charArray.length = _length[i];
145         return _charArray;
146    }
147     
148     public final void add(int o, int l) {
149         if (_size == _offset.length) {
150             resize();
151         }
152             
153        _offset[_size] = o;
154        _length[_size++] = l;
155     }
156     
157     public final void add(char[] c, int l) {
158         if (_size == _offset.length) {
159             resize();
160         }
161             
162        _offset[_size] = _arrayIndex;
163        _length[_size++] = l;
164        
165        final int arrayIndex = _arrayIndex + l;
166        if (arrayIndex >= _array.length) {
167            resizeArray(arrayIndex);
168        }
169        
170        System.arraycopy(c, 0, _array, _arrayIndex, l);
171        _arrayIndex = arrayIndex;
172     }
173     
174     protected final void resize() {
175         if (_size == _maximumCapacity) {
176             throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.arrayMaxCapacity"));
177         }
178
179         int newSize = _size * 3 / 2 + 1;
180         if (newSize > _maximumCapacity) {
181             newSize = _maximumCapacity;
182         }
183
184         final int[] offset = new int[newSize];
185         System.arraycopy(_offset, 0, offset, 0, _size);
186         _offset = offset;
187
188         final int[] length = new int[newSize];
189         System.arraycopy(_length, 0, length, 0, _size);
190         _length = length;
191     }
192
193     protected final void resizeArray(int requestedSize) {
194         if (_arrayIndex == _maximumCharacterSize) {
195             throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.maxNumberOfCharacters"));
196         }
197
198         int newSize = requestedSize * 3 / 2 + 1;
199         if (newSize > _maximumCharacterSize) {
200             newSize = _maximumCharacterSize;
201         }
202         
203         final char[] array = new char[newSize];
204         System.arraycopy(_array, 0, array, 0, _arrayIndex);
205         _charArray.ch = _array = array;
206     }
207 }
208
Popular Tags