KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > patch > LineReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Brock Janiczak <brockj@tpg.com.au> - Bug 181919 LineReader creating unneeded garbage
11  *******************************************************************************/

12 package org.eclipse.compare.internal.patch;
13
14 import java.io.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 public class LineReader {
21
22     private boolean fHaveChar= false;
23     private int fLastChar;
24     private boolean fSawEOF= false;
25     private BufferedReader fReader;
26     private boolean fIgnoreSingleCR= false;
27     private StringBuffer JavaDoc fBuffer= new StringBuffer JavaDoc();
28     
29     public LineReader(BufferedReader reader) {
30         fReader= reader;
31         Assert.isNotNull(reader);
32     }
33
34     void ignoreSingleCR() {
35         fIgnoreSingleCR= true;
36     }
37     
38     /**
39      * Reads a line of text. A line is considered to be terminated by any one
40      * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
41      * followed immediately by a line-feed.
42      * @return A string containing the contents of the line including
43      * the line-termination characters, or <code>null</code> if the end of the
44      * stream has been reached
45      * @exception IOException If an I/O error occurs
46      */

47     /* package */ String JavaDoc readLine() throws IOException {
48         try {
49             while (!fSawEOF) {
50                 int c= readChar();
51                 if (c == -1) {
52                     fSawEOF= true;
53                     break;
54                 }
55                 fBuffer.append((char)c);
56                 if (c == '\n')
57                     break;
58                 if (c == '\r') {
59                     c= readChar();
60                     if (c == -1) {
61                         fSawEOF= true;
62                         break; // EOF
63
}
64                     if (c != '\n') {
65                         if (fIgnoreSingleCR) {
66                             fBuffer.append((char)c);
67                             continue;
68                         }
69                         fHaveChar= true;
70                         fLastChar= c;
71                     } else
72                         fBuffer.append((char)c);
73                     break;
74                 }
75             }
76             
77             if (fBuffer.length() != 0) {
78                 return fBuffer.toString();
79             }
80             return null;
81         } finally {
82             fBuffer.setLength(0);
83         }
84     }
85     
86     /* package */ void close() {
87         try {
88             fReader.close();
89         } catch (IOException ex) {
90             // silently ignored
91
}
92     }
93     
94     public List JavaDoc readLines() {
95         try {
96             List JavaDoc lines= new ArrayList JavaDoc();
97             String JavaDoc line;
98             while ((line= readLine()) != null)
99                 lines.add(line);
100             return lines;
101         } catch (IOException ex) {
102             // NeedWork
103
//System.out.println("error while reading file: " + fileName + "(" + ex + ")");
104
} finally {
105             close();
106         }
107         return null;
108     }
109     
110     /*
111      * Returns the number of characters in the given string without
112      * counting a trailing line separator.
113      */

114     /* package */ int lineContentLength(String JavaDoc line) {
115         if (line == null)
116             return 0;
117         int length= line.length();
118         for (int i= length-1; i >= 0; i--) {
119             char c= line.charAt(i);
120             if (c =='\n' || c == '\r')
121                 length--;
122             else
123                 break;
124         }
125         return length;
126     }
127     
128     //---- private
129

130     private int readChar() throws IOException {
131         if (fHaveChar) {
132             fHaveChar= false;
133             return fLastChar;
134         }
135         return fReader.read();
136     }
137 }
138
Popular Tags