1 19 20 package org.netbeans.modules.editor.guards; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import java.io.UnsupportedEncodingException ; 27 import java.util.ArrayList ; 28 import org.netbeans.spi.editor.guards.support.AbstractGuardedSectionsProvider; 29 30 36 final class GuardedReader extends Reader { 37 38 39 40 Reader reader; 41 42 private NewLineInputStream newLineStream; 43 44 45 char[] charBuff; 46 int howmany; 47 48 51 boolean justFilter; 52 53 54 int position; 55 56 59 62 63 private final GuardedSectionsImpl callback; 64 65 private final AbstractGuardedSectionsProvider gr; 66 67 private boolean isClosed = false; 68 69 private AbstractGuardedSectionsProvider.Result result; 70 71 77 GuardedReader(AbstractGuardedSectionsProvider gr, InputStream is, boolean justFilter, String encoding, GuardedSectionsImpl guards) throws UnsupportedEncodingException { 82 newLineStream = new NewLineInputStream(is); 83 if (encoding == null) 84 reader = new InputStreamReader (newLineStream); 85 else 86 reader = new InputStreamReader (newLineStream, encoding); 87 this.justFilter = justFilter; 88 this.callback = guards; 91 this.gr = gr; 92 } 93 94 95 public int read(char[] cbuf, int off, int len) throws IOException { 96 97 if (charBuff == null) { 98 char[] readBuff = readCharBuff(); 99 this.result = this.gr.readSections(readBuff); 100 charBuff = this.result.getContent(); 101 howmany = charBuff.length; 102 } 103 104 if (howmany <= 0) { 105 return -1; 106 } else { 107 int min = Math.min(len, howmany); 108 System.arraycopy(charBuff, position, cbuf, off, min); 109 howmany -= min; 110 position += min; 111 return min; 112 } 113 } 114 115 116 final char[] readCharBuff() throws IOException { 117 118 char[] readBuff; 119 char[] tmp = new char[2048]; 120 int read; 121 ArrayList <char[]> buffs = new ArrayList <char[]>(20); 122 123 for (; ; ) { 124 read = readFully(tmp); 125 buffs.add(tmp); 126 if (read < 2048) { 127 break; 128 } else { 129 tmp = new char[2048]; 130 } 131 } 132 133 int listsize = buffs.size() - 1; 134 int size = listsize * 2048 + read; 135 readBuff = new char[size]; 136 charBuff = new char[size]; 137 int copy = 0; 138 139 for (int i = 0; i < listsize; i++) { 140 char[] tmp2 = (char[]) buffs.get(i); 141 System.arraycopy(tmp2, 0, readBuff, copy, 2048); 142 copy += 2048; 143 } 144 System.arraycopy(tmp, 0, readBuff, copy, read); 145 return readBuff; 146 } 147 148 149 final int readFully(final char[] buff) throws IOException { 150 int read = 0; 151 int sum = 0; 152 153 do { 154 read = reader.read(buff, sum, buff.length - sum); 155 sum += read; 156 } while ((sum < buff.length) && (read > 0)); 157 158 return sum + 1; 159 } 160 161 162 public void close() throws IOException { 163 if (!isClosed) { 164 isClosed = true; 165 reader.close(); 166 callback.fillSections(this.result.getGuardedSections(), newLineStream.getNewLineType()); 167 } 168 } 169 170 private final class NewLineInputStream extends InputStream { 171 172 private final InputStream stream; 173 174 private int b; 175 176 private int lookahead; 177 private boolean isLookahead = false; 178 179 180 final int[] newLineTypes; 181 182 public NewLineInputStream(InputStream source) { 183 this.stream = source; 184 this.newLineTypes = new int[] { 0, 0, 0 }; 185 } 186 187 188 public NewLine getNewLineType() { 189 if (newLineTypes[NewLine.N.ordinal()] == newLineTypes[NewLine.R.ordinal()] && 191 newLineTypes[NewLine.R.ordinal()] == newLineTypes[NewLine.RN.ordinal()]) { 192 193 String s = System.getProperty("line.separator"); return NewLine.resolve(s); 195 } 196 if (newLineTypes[NewLine.N.ordinal()] > newLineTypes[NewLine.R.ordinal()]) { 197 return (newLineTypes[NewLine.N.ordinal()] > newLineTypes[NewLine.RN.ordinal()]) ? NewLine.N : NewLine.RN; 198 } else { 199 return (newLineTypes[NewLine.R.ordinal()] > newLineTypes[NewLine.RN.ordinal()]) ? NewLine.R : NewLine.RN; 200 } 201 } 202 203 public int read() throws IOException { 204 205 b = isLookahead? lookahead: this.stream.read(); 206 isLookahead = false; 207 208 switch (b) { 209 case (int) '\n': 210 newLineTypes[NewLine.N.ordinal()]++; 211 return b; 212 case (int) '\r': 213 lookahead = this.stream.read(); 214 if (lookahead != (int) '\n') { 215 newLineTypes[NewLine.R.ordinal()]++; 216 isLookahead = true; 217 } else { 218 newLineTypes[NewLine.RN.ordinal()]++; 219 } 220 return (int) '\n'; 221 default: 222 return b; 223 } 224 225 } 226 227 public void close() throws IOException { 228 super.close(); 229 this.stream.close(); 230 } 231 232 } 233 234 } 235 | Popular Tags |