Commits

Jason Hinch committed da83f29

CRUC-4846: Modifying DvcsChangeParser so that it uses LineReader to handle files with carrier returns in them

Comments (0)

Files changed (3)

processutils/pom.xml

     <artifactId>atlassian-processutils</artifactId>
     <version>1.2-SNAPSHOT</version>
 
+  <scm>
+    <connection>scm:hg:${basedir}</connection>
+  </scm>
+
+
     <dependencies>
         <dependency>
             <groupId>commons-io</groupId>

processutils/src/main/java/com/atlassian/utils/process/LineOutputHandler.java

 
 import org.apache.commons.io.IOUtils;
 
+import java.io.Closeable;
 import java.io.InputStream;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.InterruptedIOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.Buffer;
 
 /**
  * An Output Handler which breaks the output from the process into
     }
 
     public void process(InputStream output) throws ProcessException {
-        BufferedReader reader = null;
+        if (encoding == null) {
+            process(new BufferReaderLineReader(new BufferedReader(new InputStreamReader(output))));
+        } else {
+            try {
+                process(new BufferReaderLineReader(new BufferedReader(new InputStreamReader(output, encoding))));
+            } catch (UnsupportedEncodingException e) {
+                throw new ProcessException(e);
+            }
+        }
+    }
+
+    protected void process(LineReadable reader) throws ProcessException {
         int counter = 0;
         try {
-            if (encoding == null) {
-                reader = new BufferedReader(new InputStreamReader(output));
-            } else {
-                reader = new BufferedReader(new InputStreamReader(output, encoding));
-            }
             String line;
             while ((line = reader.readLine()) != null) {
                 resetWatchdog();
             processEndByException(counter);
             throw new ProcessException(e);
         } finally {
-            IOUtils.closeQuietly(reader);
+            closeQuietly(reader);
+        }
+    }
+
+    private void closeQuietly(Closeable closeable) {
+        try {
+            if (closeable != null) {
+                closeable.close();
+            }
+        } catch (IOException ioe) {
+            // ignore
         }
     }
 
      * @param line the content of the line
      */
     protected abstract void processLine(int lineNum, String line);
+    
+    static class BufferReaderLineReader implements LineReadable {
+
+        private final BufferedReader reader;
+
+        BufferReaderLineReader(BufferedReader reader) {
+            this.reader = reader;
+        } 
+
+        public String readLine() throws IOException {
+            return reader.readLine();
+        }
+
+        public void close() throws IOException {
+            reader.close();
+        }
+    }
 
 }

processutils/src/main/java/com/atlassian/utils/process/LineReadable.java

+package com.atlassian.utils.process;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+public interface LineReadable extends Closeable {
+
+    String readLine() throws IOException;
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.