KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > search > LineReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.refactoring.nls.search;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.Reader JavaDoc;
18
19
20 class LineReader extends Object JavaDoc {
21     protected static final int LF= '\n';
22     protected static final int CR= '\r';
23
24     private BufferedReader JavaDoc fReader;
25
26     protected int fPushbackChar;
27     protected boolean fPushback;
28
29     public LineReader(InputStream JavaDoc in, String JavaDoc encoding) throws IOException JavaDoc {
30         this(new InputStreamReader JavaDoc(in, encoding));
31     }
32
33     public LineReader(Reader JavaDoc reader) {
34         fPushback= false;
35         fReader= new BufferedReader JavaDoc(reader);
36     }
37
38     public int readLine(StringBuffer JavaDoc sb) throws IOException JavaDoc {
39         int ch= -1;
40         sb.setLength(0);
41         if (fPushback) {
42             ch= fPushbackChar;
43             fPushback= false;
44         } else
45             ch= fReader.read();
46         while (ch >= 0) {
47             if (ch == LF)
48                 return 1;
49             if (ch == CR) {
50                 ch= fReader.read();
51                 if (ch == LF)
52                     return 2;
53                 else {
54                     fPushbackChar= ch;
55                     fPushback= true;
56                     return 1;
57                 }
58             }
59             sb.append((char) ch);
60             ch= fReader.read();
61         }
62         return -1;
63     }
64
65     public void close() throws IOException JavaDoc {
66         fReader.close();
67     }
68 }
69
Popular Tags