1 30 31 package de.susebox.jtopas; 32 33 import java.io.InputStream ; 37 import java.io.FileInputStream ; 38 import java.io.Reader ; 39 import java.io.InputStreamReader ; 40 import java.io.File ; 41 import java.io.IOException ; 42 43 44 48 60 public class ReaderSource implements TokenizerSource { 61 62 66 73 public ReaderSource() { 74 this((Reader )null); 75 } 76 77 87 public ReaderSource(Reader reader) { 88 setReader(reader); 89 } 90 91 103 public ReaderSource(InputStream is) { 104 if ((_inputStream = is) != null) { 105 setReader(new InputStreamReader (is)); 106 } 107 } 108 109 119 public ReaderSource(File file) throws IOException { 120 if (file != null) { 121 _inputStream = new FileInputStream (file); 122 setReader(new InputStreamReader (_inputStream)); 123 } 124 } 125 126 136 public ReaderSource(String path) throws IOException { 137 if (path != null) { 138 _inputStream = new FileInputStream (path); 139 setReader(new InputStreamReader (_inputStream)); 140 } 141 } 142 143 144 148 159 public int read(char[] cbuf, int offset, int maxChars) throws Exception { 160 if (_reader != null) { 161 return _reader.read(cbuf, offset, maxChars); 162 } else { 163 return -1; 164 } 165 } 166 167 171 180 public void close() { 181 if (_reader != null) { 182 try { _reader.close(); } catch (IOException ex) {} 183 } 184 if (_inputStream != null) { 185 try { _inputStream.close(); } catch (IOException ex) {} 186 } 187 } 188 189 198 public void setReader(Reader reader) { 199 _reader = reader; 200 } 201 202 209 public Reader getReader() { 210 return _reader; 211 } 212 213 216 protected void finalize() { 217 close(); 218 } 219 220 private Reader _reader = null; 224 private InputStream _inputStream = null; 225 } 226 | Popular Tags |