1 36 package org.columba.ristretto.io; 37 38 import java.io.IOException ; 39 40 46 public class ByteBufferSource implements Source { 47 48 private byte[] source; 49 50 private int start,end; 51 private int pos; 52 53 58 public ByteBufferSource( byte[] source ) { 59 this.source = source; 60 61 pos = 0; 62 start = 0; 63 end = source.length; 64 } 65 66 69 public Source fromActualPosition() { 70 ByteBufferSource subsource = new ByteBufferSource(source); 71 subsource.start = this.start + pos; 72 subsource.end = end; 73 subsource.pos = 0; 74 return subsource; 75 } 76 77 80 public int getPosition() { 81 return pos; 82 } 83 84 87 public void seek(int position) throws IOException { 88 pos = position; 89 } 90 91 94 public char next() throws IOException { 95 byte value = source[ start + (pos++) ]; 98 int trueValue = (value & 0x080) + (value & 0x07F); 99 100 return (char) trueValue; 101 } 102 103 106 public boolean isEOF() { 107 return pos == end; 108 } 109 110 113 public int length() { 114 return end - start; 115 } 116 117 120 public char charAt(int pos) { 121 try { 122 seek(pos); 123 return next(); 124 } catch (IOException e) { 125 e.printStackTrace(); 126 return 0; 127 } 128 } 129 130 133 public Source subSource( int start, int end ) { 134 ByteBufferSource subsource = new ByteBufferSource(source); 135 subsource.start = this.start + start; 136 subsource.end = this.start + end; 137 subsource.pos = 0; 138 return subsource; 139 } 140 141 144 public CharSequence subSequence(int start, int end) { 145 return subSource( start, end); 146 } 147 148 151 public String toString() { 152 StringBuffer buffer = new StringBuffer (length()); 153 for( int i=0; i < length(); i++) { 154 buffer.append(charAt(i)); 155 } 156 return buffer.toString(); 157 } 158 159 162 public void close() throws IOException { 163 source = null; 164 } 165 166 169 public void deepClose() throws IOException { 170 close(); 171 } 172 } 173 | Popular Tags |