1 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 (CommonResourceBundle.getInstance().getString("message.illegalClass", new Object []{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 |