1 18 package org.apache.tools.ant.filters; 19 20 import java.io.FilterReader ; 21 import java.io.IOException ; 22 import java.io.Reader ; 23 import java.io.StringReader ; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.util.FileUtils; 26 27 31 public abstract class BaseFilterReader extends FilterReader { 32 33 private static final int BUFFER_SIZE = 8192; 34 35 36 private boolean initialized = false; 37 38 39 private Project project = null; 40 41 49 public BaseFilterReader() { 50 super(new StringReader ("")); 51 FileUtils.close(this); 52 } 53 54 61 public BaseFilterReader(final Reader in) { 62 super(in); 63 } 64 65 80 public final int read(final char[] cbuf, final int off, 81 final int len) throws IOException { 82 for (int i = 0; i < len; i++) { 83 final int ch = read(); 84 if (ch == -1) { 85 if (i == 0) { 86 return -1; 87 } else { 88 return i; 89 } 90 } 91 cbuf[off + i] = (char) ch; 92 } 93 return len; 94 } 95 96 107 public final long skip(final long n) 108 throws IOException , IllegalArgumentException { 109 if (n < 0L) { 110 throw new IllegalArgumentException ("skip value is negative"); 111 } 112 113 for (long i = 0; i < n; i++) { 114 if (read() == -1) { 115 return i; 116 } 117 } 118 return n; 119 } 120 121 126 protected final void setInitialized(final boolean initialized) { 127 this.initialized = initialized; 128 } 129 130 135 protected final boolean getInitialized() { 136 return initialized; 137 } 138 139 145 public final void setProject(final Project project) { 146 this.project = project; 147 } 148 149 154 protected final Project getProject() { 155 return project; 156 } 157 158 168 protected final String readLine() throws IOException { 169 int ch = in.read(); 170 171 if (ch == -1) { 172 return null; 173 } 174 175 StringBuffer line = new StringBuffer (); 176 177 while (ch != -1) { 178 line.append ((char) ch); 179 if (ch == '\n') { 180 break; 181 } 182 ch = in.read(); 183 } 184 return line.toString(); 185 } 186 187 195 protected final String readFully() throws IOException { 196 return FileUtils.readFully(in, BUFFER_SIZE); 197 } 198 } 199 | Popular Tags |