1 57 58 package org.enhydra.apache.xerces.utils; 59 60 import org.enhydra.apache.xerces.readers.XMLEntityHandler; 61 62 66 public final class ChunkyCharArray implements XMLEntityHandler.CharBuffer { 67 68 71 public ChunkyCharArray(StringPool stringPool) { 72 fStringPool = stringPool; 73 fCurrentChunk = CharDataChunk.createChunk(stringPool, null); 74 } 75 76 79 public int length() { 80 return fLength; 81 } 82 83 86 public void append(char ch) { 87 try { 88 fCurrentData[fCurrentIndex] = ch; 89 } catch (ArrayIndexOutOfBoundsException ex) { 90 if (fCurrentIndex == CharDataChunk.CHUNK_SIZE) { 91 fCurrentChunk = CharDataChunk.createChunk(fStringPool, fCurrentChunk); 92 fCurrentData = new char[INITIAL_CHUNK_SIZE]; 93 fCurrentIndex = 0; 94 } else { 95 char[] newData = new char[fCurrentIndex * 2]; 96 System.arraycopy(fCurrentData, 0, newData, 0, fCurrentIndex); 97 fCurrentData = newData; 98 } 99 fCurrentChunk.setCharArray(fCurrentData); 100 fCurrentData[fCurrentIndex] = ch; 101 } catch (NullPointerException ex) { 102 fCurrentData = new char[INITIAL_CHUNK_SIZE]; 103 fCurrentChunk.setCharArray(fCurrentData); 104 fCurrentData[fCurrentIndex] = ch; 105 } 106 fCurrentIndex++; 107 fLength++; 108 } 109 110 115 public void append(String s) { 116 int slen = s.length(); 117 for (int i = 0; i < slen; i++) 118 append(s.charAt(i)); 119 } 120 121 124 public void append(char[] ch, int offset, int length) { 125 while (length-- > 0) 126 append(ch[offset++]); 127 } 128 129 136 public void append(ChunkyCharArray charArray, int offset, int length) { 137 fCurrentChunk.append(charArray, offset, length); 138 } 139 140 143 public int addString(int offset, int length) { 144 if (length == 0) 145 return 0; 146 return fCurrentChunk.addString(offset, length); 147 } 148 149 155 public int addSymbol(int offset, int length) { 156 if (length == 0) 157 return 0; 158 return fCurrentChunk.addSymbol(offset, length, 0); 159 } 160 161 private static final int INITIAL_CHUNK_SHIFT = 7; private static final int INITIAL_CHUNK_SIZE = (1 << INITIAL_CHUNK_SHIFT); 166 167 private StringPool fStringPool = null; 171 private CharDataChunk fCurrentChunk = null; 172 private char[] fCurrentData = null; 173 private int fCurrentIndex = 0; 174 private int fLength = 0; 175 } 176 | Popular Tags |