1 7 8 package java.io; 9 10 32 @Deprecated 33 public 34 class LineNumberInputStream extends FilterInputStream { 35 int pushBack = -1; 36 int lineNumber; 37 int markLineNumber; 38 int markPushBack = -1; 39 40 46 public LineNumberInputStream(InputStream in) { 47 super(in); 48 } 49 50 73 public int read() throws IOException { 74 int c = pushBack; 75 76 if (c != -1) { 77 pushBack = -1; 78 } else { 79 c = in.read(); 80 } 81 82 switch (c) { 83 case '\r': 84 pushBack = in.read(); 85 if (pushBack == '\n') { 86 pushBack = -1; 87 } 88 case '\n': 89 lineNumber++; 90 return '\n'; 91 } 92 return c; 93 } 94 95 112 public int read(byte b[], int off, int len) throws IOException { 113 if (b == null) { 114 throw new NullPointerException (); 115 } else if ((off < 0) || (off > b.length) || (len < 0) || 116 ((off + len) > b.length) || ((off + len) < 0)) { 117 throw new IndexOutOfBoundsException (); 118 } else if (len == 0) { 119 return 0; 120 } 121 122 int c = read(); 123 if (c == -1) { 124 return -1; 125 } 126 b[off] = (byte)c; 127 128 int i = 1; 129 try { 130 for (; i < len ; i++) { 131 c = read(); 132 if (c == -1) { 133 break; 134 } 135 if (b != null) { 136 b[off + i] = (byte)c; 137 } 138 } 139 } catch (IOException ee) { 140 } 141 return i; 142 } 143 144 161 public long skip(long n) throws IOException { 162 int chunk = 2048; 163 long remaining = n; 164 byte data[]; 165 int nr; 166 167 if (n <= 0) { 168 return 0; 169 } 170 171 data = new byte[chunk]; 172 while (remaining > 0) { 173 nr = read(data, 0, (int) Math.min(chunk, remaining)); 174 if (nr < 0) { 175 break; 176 } 177 remaining -= nr; 178 } 179 180 return n - remaining; 181 } 182 183 189 public void setLineNumber(int lineNumber) { 190 this.lineNumber = lineNumber; 191 } 192 193 199 public int getLineNumber() { 200 return lineNumber; 201 } 202 203 204 222 public int available() throws IOException { 223 return (pushBack == -1) ? super.available()/2 : super.available()/2 + 1; 224 } 225 226 241 public void mark(int readlimit) { 242 markLineNumber = lineNumber; 243 markPushBack = pushBack; 244 in.mark(readlimit); 245 } 246 247 270 public void reset() throws IOException { 271 lineNumber = markLineNumber; 272 pushBack = markPushBack; 273 in.reset(); 274 } 275 } 276 | Popular Tags |