1 package com.opensymphony.module.sitemesh.util; 2 3 import java.io.Reader ; 4 import java.io.IOException ; 5 6 22 public class CharArrayReader extends Reader 23 { 24 25 protected char buf[]; 26 27 28 protected int pos; 29 30 31 protected int markedPos = 0; 32 33 37 protected int count; 38 39 44 public CharArrayReader(char buf[]) 45 { 46 this.buf = buf; 47 this.pos = 0; 48 this.count = buf.length; 49 } 50 51 58 public CharArrayReader(char buf[], int offset, int length) 59 { 60 if((offset < 0) || (offset > buf.length) || (length < 0) || ((offset + length) < 0)) 61 { 62 throw new IllegalArgumentException (); 63 } 64 this.buf = buf; 65 this.pos = offset; 66 this.count = Math.min(offset + length, buf.length); 67 this.markedPos = offset; 68 } 69 70 75 public int read() throws IOException 76 { 77 if(pos >= count) 78 return -1; 79 else 80 return buf[pos++]; 81 } 82 83 93 public int read(char b[], int off, int len) throws IOException 94 { 95 if((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) 96 { 97 throw new IndexOutOfBoundsException (); 98 } 99 else if(len == 0) 100 { 101 return 0; 102 } 103 104 if(pos >= count) 105 { 106 return -1; 107 } 108 if(pos + len > count) 109 { 110 len = count - pos; 111 } 112 if(len <= 0) 113 { 114 return 0; 115 } 116 System.arraycopy(buf, pos, b, off, len); 117 pos += len; 118 return len; 119 } 120 121 128 public long skip(long n) throws IOException 129 { 130 if(pos + n > count) 131 { 132 n = count - pos; 133 } 134 if(n < 0) 135 { 136 return 0; 137 } 138 pos += n; 139 return n; 140 } 141 142 148 public boolean ready() throws IOException 149 { 150 return (count - pos) > 0; 151 } 152 153 156 public boolean markSupported() 157 { 158 return true; 159 } 160 161 172 public void mark(int readAheadLimit) throws IOException 173 { 174 markedPos = pos; 175 } 176 177 183 public void reset() throws IOException 184 { 185 pos = markedPos; 186 } 187 188 191 public void close() 192 { 193 buf = null; 194 } 195 } 196 | Popular Tags |