1 12 package org.eclipse.compare.internal.patch; 13 14 import java.io.*; 15 import java.util.ArrayList ; 16 import java.util.List ; 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 fBuffer= new StringBuffer (); 28 29 public LineReader(BufferedReader reader) { 30 fReader= reader; 31 Assert.isNotNull(reader); 32 } 33 34 void ignoreSingleCR() { 35 fIgnoreSingleCR= true; 36 } 37 38 47 String 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; } 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 void close() { 87 try { 88 fReader.close(); 89 } catch (IOException ex) { 90 } 92 } 93 94 public List readLines() { 95 try { 96 List lines= new ArrayList (); 97 String line; 98 while ((line= readLine()) != null) 99 lines.add(line); 100 return lines; 101 } catch (IOException ex) { 102 } finally { 105 close(); 106 } 107 return null; 108 } 109 110 114 int lineContentLength(String 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 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 |