1 4 package org.terracotta.dso; 5 6 import org.apache.commons.io.IOUtils; 7 import org.eclipse.core.resources.IFile; 8 import org.eclipse.core.runtime.CoreException; 9 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.io.InputStreamReader ; 13 import java.io.Reader ; 14 import java.util.ArrayList ; 15 import java.util.ConcurrentModificationException ; 16 17 30 31 public class LineLengths implements java.io.Serializable { 32 private int[] m_lines; 33 34 public LineLengths() { 35 super(); 36 } 37 38 public LineLengths(IFile file) 39 throws ConcurrentModificationException , 40 IOException , 41 CoreException 42 { 43 this(); 44 setFile(file); 45 } 46 47 public LineLengths(Reader reader) 48 throws ConcurrentModificationException , 49 IOException  50 { 51 this(); 52 setReader(reader); 53 } 54 55 public void setFile(IFile file) 56 throws ConcurrentModificationException , 57 IOException , 58 CoreException 59 { 60 initLines(new InputStreamReader (file.getContents())); 61 } 62 63 public void setReader(Reader reader) 64 throws ConcurrentModificationException , 65 IOException  66 { 67 initLines(reader); 68 } 69 70 private void initLines(Reader reader) 71 throws ConcurrentModificationException , 72 IOException  73 { 74 ArrayList <String > list = new ArrayList <String >(); 75 76 try { 77 BufferedReader br = new BufferedReader (reader); 78 String s; 79 80 while((s = readLine(br)) != null) { 81 list.add(s); 82 } 83 } catch(IOException e) { 84 IOUtils.closeQuietly(reader); 85 throw e; 86 } catch(ConcurrentModificationException e) { 87 IOUtils.closeQuietly(reader); 88 throw e; 89 } finally { 90 IOUtils.closeQuietly(reader); 91 } 92 93 int size = list.size(); 94 m_lines = new int[size]; 95 for(int i = 0; i < size; i++) { 96 m_lines[i] = list.get(i).length(); 97 } 98 } 99 100 104 private String readLine(BufferedReader br) throws IOException { 105 StringBuffer sb = new StringBuffer (); 106 boolean haveCR = false; 107 boolean done = false; 108 int i; 109 char c; 110 111 while((i = br.read()) != -1) { 112 c = (char)i; 113 114 if(haveCR) { 115 switch(c) { 116 case '\n': 117 sb.append(c); 118 done = true; 119 break; 120 default: 121 br.reset(); 122 done = true; 123 break; 124 } 125 } 126 else { 127 sb.append(c); 128 switch(c) { 129 case '\n': 130 done = true; 131 break; 132 case '\r': 133 br.mark(1); 134 haveCR = true; 135 } 136 } 137 138 if(done) { 139 break; 140 } 141 } 142 143 return sb.length() > 0 ? sb.toString() : null; 144 } 145 146 public int lineSize(int line) { 147 if(line < 0 || line > m_lines.length) { 148 return 0; 149 } 150 return m_lines[line]; 151 } 152 153 public int offset(int line) { 154 int result = 0; 155 156 if(line == 0) return 0; 157 158 for(int i = 0; i < line; i++) { 159 result += m_lines[i]; 160 } 161 162 return result; 163 } 164 165 public int offset(int line, int col) { 166 if(line < 0 || line > m_lines.length) { 167 return 0; 168 } 169 int result = offset(line); 170 if(col > 0) { 171 result += col; 172 } 173 return result; 174 } 175 } 176
| Popular Tags
|