1 36 package org.columba.ristretto.io; 37 38 import java.io.IOException ; 39 40 46 public class CharSequenceSource implements Source { 47 48 private CharSequence source; 49 private int start,end; 50 private int pos; 51 52 57 public CharSequenceSource( CharSequence source ) { 58 this.source = source; 59 pos = 0; 60 start = 0; 61 end = source.length(); 62 } 63 64 67 public Source subSource(int start, int end) { 68 CharSequenceSource subsource = new CharSequenceSource(source); 69 subsource.start = this.start + start; 70 subsource.end = this.start + end; 71 subsource.pos = 0; 72 return subsource; 73 } 74 75 78 public CharSequence subSequence( int start, int end) { 79 return subSource(start, end); 80 } 81 82 85 public Source fromActualPosition() { 86 CharSequenceSource subsource = new CharSequenceSource(source); 87 subsource.start = start + pos; 88 subsource.end = end; 89 subsource.pos = 0; 90 return subsource; 91 } 92 93 96 public int getPosition() { 97 return pos; 98 } 99 100 103 public void seek(int position) throws IOException { 104 pos = position; 105 } 106 107 110 public char next() throws IOException { 111 return source.charAt(start + pos++); 112 } 113 114 117 public int length() { 118 return end - start; 119 } 120 121 124 public char charAt(int arg0) { 125 try { 126 pos = arg0; 127 return next(); 128 } catch (IOException e) { 129 e.printStackTrace(); 130 return 0; 131 } 132 } 133 134 135 138 public String toString() { 139 if (length() < 0) return ""; 140 StringBuffer buffer = new StringBuffer (length()); 141 for( int i=0; i < length(); i++) { 142 buffer.append(charAt(i)); 143 } 144 return buffer.toString(); 145 } 146 147 150 public boolean isEOF() { 151 return pos == end; 152 } 153 154 157 public void close() throws IOException { 158 source = null; 159 } 160 161 164 public void deepClose() throws IOException { 165 close(); 166 } 167 } 168 | Popular Tags |