Skip to content

The Unified and Autonomous Development Platform

Add git line read utility method.

robin shine40eb5d4dfaa1
diff --git a/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java b/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java
new file mode 100644
index 0000000000..d6968ca560
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java
@@ -0,0 +1,48 @@
+package com.pmease.commons.git;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.pmease.commons.util.Charsets;
+
+public class GitUtils {
+
+ /**
+ * Read lines from specified bytes. This method only treats "\n" as EOL character,
+ * and any occurrences of "\r" will be preserved in read result. This method is
+ * intended to read text files for comparison purpose.
+ *
+ * @param bytes
+ * bytes to read lines from
+ * @return
+ * <tt>null</tt> if character encoding can not be detected, for instance
+ * when bytes represents binary data
+ */
+ public static LineReadResult readLines(byte[] bytes) {
+ Charset charset = Charsets.detectFrom(bytes);
+ if (charset != null) {
+ List<String> lines = new ArrayList<>();
+ StringBuilder builder = new StringBuilder();
+ String string = new String(bytes, charset);
+ for (int i=0; i<string.length(); i++) {
+ char ch = string.charAt(i);
+ if (ch == '\n') {
+ lines.add(builder.toString());
+ builder = new StringBuilder();
+ } else {
+ builder.append(ch);
+ }
+ }
+ if (builder.length() != 0) {
+ lines.add(builder.toString());
+ return new LineReadResult(lines, charset, false);
+ } else {
+ return new LineReadResult(lines, charset, true);
+ }
+ } else {
+ return null;
+ }
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/LineReadResult.java b/commons.git/src/main/java/com/pmease/commons/git/LineReadResult.java
new file mode 100644
index 0000000000..b3907c55d2
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/LineReadResult.java
@@ -0,0 +1,64 @@
+package com.pmease.commons.git;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class LineReadResult {
+
+ private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
+
+ private final List<String> lines;
+
+ private final Charset charset;
+
+ private final boolean hasEolAtEof;
+
+ public LineReadResult(List<String> lines, Charset charset, boolean hasEolAtEof) {
+ this.lines = lines;
+ this.charset = charset;
+ this.hasEolAtEof = hasEolAtEof;
+ }
+
+ public boolean isHasEolAtEof() {
+ return hasEolAtEof;
+ }
+
+ public List<String> getLines() {
+ return lines;
+ }
+
+ public Charset getCharset() {
+ return charset;
+ }
+
+ public LineReadResult ignoreEOL() {
+ List<String> processedLines = new ArrayList<>();
+ for (String line: lines) {
+ if (line.endsWith("\r"))
+ line = line.substring(0, line.length()-1);
+ processedLines.add(line);
+ }
+ return new LineReadResult(processedLines, charset, hasEolAtEof);
+ }
+
+ public LineReadResult ignoreEOLWhitespaces() {
+ List<String> processedLines = new ArrayList<>();
+ for (String line: lines)
+ processedLines.add(StringUtils.stripEnd(line, " \t\r"));
+ return new LineReadResult(processedLines, charset, hasEolAtEof);
+ }
+
+ public LineReadResult ignoreWhitespaces() {
+ List<String> processedLines = new ArrayList<>();
+ for (String line: lines) {
+ line = WHITESPACE_PATTERN.matcher(line.trim()).replaceAll(" ");
+ processedLines.add(line);
+ }
+ return new LineReadResult(processedLines, charset, hasEolAtEof);
+ }
+
+}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.java b/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.java
new file mode 100644
index 0000000000..16d413b17c
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/GitUtilsTest.java
@@ -0,0 +1,25 @@
+package com.pmease.commons.git;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+import com.pmease.commons.util.Charsets;
+
+public class GitUtilsTest {
+
+ @Test
+ public void testReadLines() {
+ assertEquals(null, GitUtils.readLines(new byte[]{1,2,3,4,5}));
+ assertEquals(Charsets.ISO_8859_1, GitUtils.readLines("some text".getBytes()).getCharset());
+ LineReadResult result = GitUtils.readLines("中文测试\r\nsecond line".getBytes());
+ assertEquals(Lists.newArrayList("中文测试\r", "second line"), result.getLines());
+ assertEquals(false, result.isHasEolAtEof());
+ assertEquals(Charsets.UTF_8, result.getCharset());
+
+ result = GitUtils.readLines("\nhello\n\nworld\n".getBytes());
+ assertEquals(Lists.newArrayList("", "hello", "", "world"), result.getLines());
+ }
+
+}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/LineReadResultTest.java b/commons.git/src/test/java/com/pmease/commons/git/LineReadResultTest.java
new file mode 100644
index 0000000000..915abe71e7
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/LineReadResultTest.java
@@ -0,0 +1,20 @@
+package com.pmease.commons.git;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+import com.pmease.commons.util.Charsets;
+
+public class LineReadResultTest {
+
+ @Test
+ public void test() {
+ LineReadResult result = new LineReadResult(Lists.newArrayList(" hello \tworld \t \r"), Charsets.UTF_8, true);
+ assertEquals(" hello \tworld \t ", result.ignoreEOL().getLines().get(0));
+ assertEquals(" hello \tworld", result.ignoreEOLWhitespaces().getLines().get(0));
+ assertEquals("hello world", result.ignoreWhitespaces().getLines().get(0));
+ }
+
+}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/UniversalEncodingDetector.java b/commons.util/src/main/java/com/pmease/commons/util/UniversalEncodingDetector.java
index dc0fd14fa6..c9506c2c6b 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/UniversalEncodingDetector.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/UniversalEncodingDetector.java
@@ -40,8 +40,6 @@ class UniversalEncodingDetector {
return listener.dataEnd();
} catch (IOException e) {
throw e;
- } catch (LinkageError e) {
- return null; // juniversalchardet is not available
} finally {
input.reset();
}