1 36 package org.columba.ristretto.io; 37 38 import java.io.IOException ; 39 import java.util.ArrayList ; 40 41 47 public class ConcatenatedSource implements Source { 48 49 ArrayList sources; 50 ArrayList nextSourceBegin; 51 int position; 52 int length; 53 int sourceIndex; 54 57 public ConcatenatedSource() { 58 sources = new ArrayList (); 59 nextSourceBegin = new ArrayList (); 60 nextSourceBegin.add(new Integer (0)); 61 position = 0; 62 length = 0; 63 sourceIndex = 0; 64 } 65 66 71 public void addSource( Source source ) { 72 sources.add( source ); 73 length += source.length(); 74 nextSourceBegin.add( new Integer (length) ); 75 } 76 77 80 public Source fromActualPosition() { 81 ConcatenatedSource newsource = new ConcatenatedSource(); 82 newsource.addSource(((Source)sources.get(sourceIndex)).fromActualPosition()); 83 for( int i=sourceIndex+1; i<sources.size(); i++) { 84 newsource.addSource((Source)sources.get(i)); 85 } 86 return newsource; 87 } 88 89 92 public int getPosition() { 93 return position; 94 } 95 96 99 public boolean isEOF() { 100 return position == length; 101 } 102 103 106 public char next() throws IOException { 107 if( ((Source)sources.get(sourceIndex)).isEOF() ) { 108 sourceIndex++; 109 } 110 111 return ((Source)sources.get(sourceIndex)).next(); 112 } 113 114 117 public void seek(int arg0) throws IOException { 118 if( !(((Integer )nextSourceBegin.get(sourceIndex)).intValue() < arg0 && arg0 < ((Integer )nextSourceBegin.get(sourceIndex+1)).intValue()) ) { 120 sourceIndex = 0; 122 while( !(((Integer )nextSourceBegin.get(sourceIndex)).intValue() <= arg0 && arg0 < ((Integer )nextSourceBegin.get(sourceIndex+1)).intValue()) ) { 123 sourceIndex++; 124 } 125 } 126 int posInSource = arg0 - ((Integer )nextSourceBegin.get(sourceIndex)).intValue(); 127 position = arg0; 128 129 ((Source)sources.get(sourceIndex)).seek(posInSource); 130 } 131 132 135 public char charAt(int arg0) { 136 try { 137 seek( arg0 ); 138 } catch (IOException e) { 139 return (char)0; 140 } 141 return ((Source)sources.get(sourceIndex)).charAt(((Source)sources.get(sourceIndex)).getPosition()); 142 } 143 144 147 public int length() { 148 return length; 149 } 150 151 154 public CharSequence subSequence(int arg0, int arg1) { 155 if( arg1 - arg0 < 1024) { 156 StringBuffer result = new StringBuffer ( arg1- arg0); 157 for( int i=arg0; i<arg1; i++ ) { 158 result.append(charAt(i)); 159 } 160 return result; 161 } else { 162 return subSource( arg0, arg1); 163 } 164 } 165 166 167 170 public Source subSource(int arg0, int arg1) { 171 ConcatenatedSource newsource = new ConcatenatedSource(); 172 173 int beginSourceIndex = 0; 175 while( !(((Integer )nextSourceBegin.get(beginSourceIndex)).intValue() <= arg0 && arg0 < ((Integer )nextSourceBegin.get(beginSourceIndex+1)).intValue()) ) { 176 beginSourceIndex++; 177 } 178 179 int beginPos = arg0 - ((Integer )nextSourceBegin.get(beginSourceIndex)).intValue(); 180 181 182 int endSourceIndex = 0; 184 while( !(((Integer )nextSourceBegin.get(endSourceIndex)).intValue() <= arg1 && arg1 < ((Integer )nextSourceBegin.get(endSourceIndex+1)).intValue()) ) { 185 endSourceIndex++; 186 } 187 188 int endPos = arg1 - ((Integer )nextSourceBegin.get(endSourceIndex)).intValue(); 189 190 if( beginSourceIndex == endSourceIndex ) { 192 newsource.addSource( (Source) ((Source)sources.get(beginSourceIndex)).subSequence( beginPos, endPos )); 193 } else { 194 newsource.addSource( (Source) ((Source)sources.get(beginSourceIndex)).subSequence( beginPos, ((Source)sources.get(beginSourceIndex)).length()) ); 195 for( int i=beginSourceIndex+1; i<endSourceIndex-1; i++) { 196 newsource.addSource( (Source) sources.get(i)); 197 } 198 newsource.addSource( (Source) ((Source)sources.get(endSourceIndex)).subSequence( 0, endPos )); 199 } 200 201 return newsource; 202 } 203 204 207 public String toString() { 208 StringBuffer result = new StringBuffer (length); 209 for( int i=0; i<sources.size(); i++) { 210 result.append( sources.get(i).toString() ); 211 } 212 return result.toString(); 213 } 214 215 218 public void close() throws IOException { 219 for( int i=0; i<sources.size(); i++) { 220 ((Source)sources.get(i)).close(); 221 } 222 sources = null; 223 } 224 225 228 public void deepClose() throws IOException { 229 for( int i=0; i<sources.size(); i++) { 230 ((Source)sources.get(i)).deepClose(); 231 } 232 sources = null; 233 } 234 } 235 | Popular Tags |