1 19 20 package org.netbeans.lib.cvsclient.file; 21 22 import java.io.*; 23 import org.netbeans.lib.cvsclient.util.*; 24 25 34 public class WriteRcsDiffFilePreprocessor implements WriteTextFilePreprocessor { 35 36 private static final int CHUNK_SIZE = 32768; 37 38 private static final int READ_REMAINING = -2; 39 40 43 private String lineEnding = System.getProperty("line.separator"); 44 45 public WriteRcsDiffFilePreprocessor() { 46 } 47 48 52 public String getLineEnding() { 53 return lineEnding; 54 } 55 56 60 public void setLineEnding(String lineEnding) { 61 this.lineEnding = lineEnding; 62 } 63 64 73 public void copyTextFileToLocation(InputStream processedInput, File fileToWrite, OutputStreamProvider customOutput) 74 throws IOException { 75 ReadInfo tempFileReader = null; 80 OutputStream out = null; 81 ReadInfo tempDiffReader = null; 82 File tempFile = null; 83 85 try { 86 tempDiffReader = new ReadInfo(new BufferedInputStream(processedInput)); 87 tempFileReader = new ReadInfo(new BufferedInputStream(new FileInputStream(fileToWrite))); 88 tempFile = File.createTempFile(".#merg", "cvs"); 90 out = new BufferedOutputStream(new FileOutputStream(tempFile)); 91 92 int fileStart = 0; 93 int diffCount = 0; 94 byte[] diff = tempDiffReader.readLine(); 95 while (diff != null && diff.length > 0) { 96 if (diff[0] == 'd') { 98 int startLine = getStart(diff); 100 int count = getLength(diff); 101 if (startLine >= 0 && count > 0) { 103 readToLine(startLine - 1, tempFileReader, out); 106 readToLine(startLine - 1 + count, tempFileReader, null); 107 } 109 else { 110 BugLog.getInstance().bug("wrong parsing.." + new String (diff)); 112 throw new IOException(); } 114 } 115 else if (diff[0] == 'a') { 116 int startLine = getStart(diff); 118 int count = getLength(diff); 119 if (startLine >= 0 && count > 0) { 121 readToLine(startLine, tempFileReader, out); 122 tempDiffReader.setLineNumber(0); 123 readToLine(count, tempDiffReader, out); 124 } 125 else { 126 BugLog.getInstance().bug("wrong parsing.." + new String (diff)); 128 throw new IOException(); } 130 } 131 diff = tempDiffReader.readLine(); 133 } 134 readToLine(READ_REMAINING, tempFileReader, out); 136 137 if (tempFile != null) { 138 tempFileReader.close(); 139 out.close(); 140 InputStream in = null; 141 OutputStream customOutputStream = customOutput.createOutputStream(); 142 try { 143 in = new BufferedInputStream(new FileInputStream(tempFile)); 144 while (true) { 145 int ch = in.read(); 146 if (ch == -1) { 147 break; 148 } 149 customOutputStream.write(ch); 150 } 151 } finally { 152 if (in != null) { 153 try { 154 in.close(); 155 } catch (IOException ex) { 156 } 157 } 158 try { 159 customOutputStream.close(); 160 } catch (IOException ex) { 161 } 162 } 163 } 164 } 165 catch (Exception exc) { 166 BugLog.getInstance().showException(exc); 167 } 168 finally { 169 if (tempDiffReader != null) { 170 try { 171 tempDiffReader.close(); 172 } 173 catch (IOException ex) { 174 } 176 } 177 if (tempFileReader != null) { 178 try { 179 tempFileReader.close(); 180 } 181 catch (IOException ex) { 182 } 184 } 185 if (out != null) { 186 try { 187 out.close(); 188 } 189 catch (IOException ex) { 190 } 192 } 193 if (tempFile != null) { 194 tempFile.delete(); 195 } 196 } 197 } 198 199 208 private void readToLine(int finalLine, final ReadInfo reader, 209 final OutputStream out) throws IOException { 210 byte[] line; 211 while (reader.getLineNumber() < finalLine || finalLine == READ_REMAINING) { 212 line = reader.readLine(); 213 if (line == null) { 214 return; 216 } 217 if (out != null) { 218 out.write(line); 219 out.write(getLineEnding().getBytes()); 220 } 221 } 222 } 223 224 private static int indexOf(byte[] bytes, byte b) { 225 return indexOf(bytes, b, 0); 226 } 227 228 private static int indexOf(byte[] bytes, byte b, int start) { 229 int index = -1; 230 for (int i = start; i < bytes.length; i++) { 231 if (bytes[i] == b) { 232 index = i; 233 break; 234 } 235 } 236 return index; 237 } 238 239 242 private static int getStart(byte[] diffLine) { 243 int spacePos = indexOf(diffLine, (byte) ' '); 244 if (spacePos > 0) { 245 String number = new String (diffLine, 1, spacePos - 1); 246 try { 247 int toReturn = Integer.parseInt(number); 248 return toReturn; 249 } 250 catch (NumberFormatException exc) { 251 return -1; 253 } 254 } 255 return -1; 257 } 258 259 264 private static int getLength(byte[] diffLine) { 265 int spacePos = indexOf(diffLine, (byte) ' '); 267 if (spacePos > 0) { 268 int end = indexOf(diffLine, (byte) ' ', spacePos + 1); 269 if (end < 0) end = diffLine.length; 270 String number = new String (diffLine, spacePos + 1, end - spacePos - 1); 271 try { 272 int toReturn = Integer.parseInt(number); 273 return toReturn; 274 } 275 catch (NumberFormatException exc) { 276 return -1; 278 } 279 } 280 return -1; 282 } 283 284 private static class ReadInfo { 285 286 private static final boolean crLines = "\r".equals(System.getProperty("line.separator")); 287 288 private PushbackInputStream in; 289 private int readLength; 290 private int startIndex; 291 private int lineNumber; 292 private ByteArray line; 293 294 public ReadInfo(InputStream in) { 295 this.in = new PushbackInputStream(in, 1); 296 readLength = -1; 297 startIndex = 0; 298 lineNumber = 0; 299 line = new ByteArray(); 300 } 301 302 public int getLineNumber() { 303 return lineNumber; 304 } 305 306 public void setLineNumber(int lineNumber) { 307 this.lineNumber = lineNumber; 308 } 309 310 314 public byte[] readLine() throws IOException { 315 line.reset(); 316 boolean end = false; 317 do { 318 int b = in.read(); 319 if (b == -1) { 320 end = true; 321 break; 322 } 323 if (b == '\n') { 324 lineNumber++; 325 break; 326 } 327 if (b == '\r') { 328 int next = in.read(); 329 if (next == '\n') { 330 lineNumber++; 331 break; 332 } 333 in.unread(next); 334 if (crLines) { 335 lineNumber++; 336 break; 337 } 338 } 339 line.add((byte) b); 340 } while (true); 341 byte[] bytes = line.getBytes(); 342 if (end && bytes.length == 0) { 343 bytes = null; 344 } 345 return bytes; 346 384 } 385 386 public void close() throws IOException { 387 if (in != null) { 388 in.close(); 389 } 390 } 391 } 393 } 394 | Popular Tags |