1 package org.archive.io; 2 3 import java.io.IOException ; 4 5 public class SeekReaderCharSequence implements CharSequence { 6 7 8 final private SeekReader reader; 9 final private int size; 10 11 12 public SeekReaderCharSequence(SeekReader reader, int size) { 13 this.reader = reader; 14 this.size = size; 15 } 16 17 18 public int length() { 19 return size; 20 } 21 22 23 public char charAt(int index) { 24 if ((index < 0) || (index >= length())) { 25 throw new IndexOutOfBoundsException (Integer.toString(index)); 26 } 27 try { 28 reader.position(index); 29 int r = reader.read(); 30 if (r < 0) { 31 throw new IllegalStateException ("EOF"); 32 } 33 return (char)reader.read(); 34 } catch (IOException e) { 35 throw new RuntimeException (e); 36 } 37 } 38 39 40 public CharSequence subSequence(int start, int end) { 41 return new CharSubSequence(this, start, end); 42 } 43 44 public String toString() { 45 StringBuilder sb = new StringBuilder (); 46 try { 47 reader.position(0); 48 for (int ch = reader.read(); ch >= 0; ch = reader.read()) { 49 sb.append((char)ch); 50 } 51 return sb.toString(); 52 } catch (IOException e) { 53 throw new IllegalStateException (e); 54 } 55 } 56 } 57 | Popular Tags |