You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by bo...@apache.org on 2011/08/10 16:02:41 UTC
svn commit: r1156189 - in /incubator/rat/main/trunk: ./
apache-rat-core/src/main/java/org/apache/rat/annotation/
apache-rat-core/src/test/java/org/apache/rat/annotation/
apache-rat-core/src/test/resources/violations/
Author: bodewig
Date: Wed Aug 10 14:02:41 2011
New Revision: 1156189
URL: http://svn.apache.org/viewvc?rev=1156189&view=rev
Log:
use a stripped down copy of commons-io 2.x's BOMInputStream to get rid of any BOM when adding licenses
Added:
incubator/rat/main/trunk/apache-rat-core/src/test/resources/violations/FilterTest.cs
- copied unchanged from r1156079, logging/log4net/trunk/tests/src/Filter/FilterTest.cs
Modified:
incubator/rat/main/trunk/RELEASE_NOTES.txt
incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java
incubator/rat/main/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java
Modified: incubator/rat/main/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/RELEASE_NOTES.txt?rev=1156189&r1=1156188&r2=1156189&view=diff
==============================================================================
--- incubator/rat/main/trunk/RELEASE_NOTES.txt (original)
+++ incubator/rat/main/trunk/RELEASE_NOTES.txt Wed Aug 10 14:02:41 2011
@@ -30,6 +30,7 @@ RAT 0.8
declaration now - it used to not add anything.
* RAT-67 The XML and standard plain text reports now contain a
timestamp with the time the report has been generated.
+ * RAT-95 When adding licenses RAT will now remove any BOM from the file.
RAT 0.7
=======
Modified: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java?rev=1156189&r1=1156188&r2=1156189&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java (original)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java Wed Aug 10 14:02:41 2011
@@ -20,9 +20,12 @@ package org.apache.rat.annotation;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
import java.io.FileWriter;
+import java.io.FilterInputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
@@ -218,10 +221,10 @@ public abstract class AbstractLicenceApp
throws IOException {
boolean written = false;
try {
- FileReader fr = new FileReader(document);
+ FileInputStream fis = new FileInputStream(document);
BufferedReader br = null;
try {
- br = new BufferedReader(fr);
+ br = new BufferedReader(new InputStreamReader(new BOMInputStream(fis)));
if (!expectsHashPling
&& !expectsAtEcho
@@ -266,7 +269,7 @@ public abstract class AbstractLicenceApp
if (br != null) {
br.close();
}
- fr.close();
+ fis.close();
}
} finally {
writer.close();
@@ -436,3 +439,117 @@ public abstract class AbstractLicenceApp
return Arrays.binarySearch(arr, key) >= 0;
}
}
+
+/**
+ * Stripped down version of Commons IO 2.0's BOMInputStream.
+ */
+class BOMInputStream extends FilterInputStream {
+ private int[] firstBytes;
+ private int fbLength, fbIndex, markFbIndex;
+ private boolean markedAtStart;
+ private static final int[][] BOMS = {
+ new int[] { 0xEF, 0xBB, 0xBF }, // UTF-8
+ new int[] { 0xFE, 0xFF }, // UTF-16BE
+ new int[] { 0xFF, 0xFE }, // UTF-16LE
+ };
+
+ BOMInputStream(InputStream s) {
+ super(s);
+ }
+
+ public int read() throws IOException {
+ int b = readFirstBytes();
+ return (b >= 0) ? b : in.read();
+ }
+
+ public int read(byte[] buf, int off, int len) throws IOException {
+ int firstCount = 0;
+ int b = 0;
+ while ((len > 0) && (b >= 0)) {
+ b = readFirstBytes();
+ if (b >= 0) {
+ buf[off++] = (byte) (b & 0xFF);
+ len--;
+ firstCount++;
+ }
+ }
+ int secondCount = in.read(buf, off, len);
+ return (secondCount < 0)
+ ? (firstCount > 0 ? firstCount : -1) : firstCount + secondCount;
+ }
+
+ public int read(byte[] buf) throws IOException {
+ return read(buf, 0, buf.length);
+ }
+
+ private int readFirstBytes() throws IOException {
+ getBOM();
+ return (fbIndex < fbLength) ? firstBytes[fbIndex++] : -1;
+ }
+
+ private void getBOM() throws IOException {
+ if (firstBytes == null) {
+ int max = 0;
+ for (int i = 0; i < BOMS.length; i++) {
+ max = Math.max(max, BOMS[i].length);
+ }
+ firstBytes = new int[max];
+ for (int i = 0; i < firstBytes.length; i++) {
+ firstBytes[i] = in.read();
+ fbLength++;
+ if (firstBytes[i] < 0) {
+ break;
+ }
+
+ boolean found = find();
+ if (found) {
+ fbLength = 0;
+ break;
+ }
+ }
+ }
+ }
+ public synchronized void mark(int readlimit) {
+ markFbIndex = fbIndex;
+ markedAtStart = (firstBytes == null);
+ in.mark(readlimit);
+ }
+
+ public synchronized void reset() throws IOException {
+ fbIndex = markFbIndex;
+ if (markedAtStart) {
+ firstBytes = null;
+ }
+
+ in.reset();
+ }
+
+ public long skip(long n) throws IOException {
+ while ((n > 0) && (readFirstBytes() >= 0)) {
+ n--;
+ }
+ return in.skip(n);
+ }
+
+ private boolean find() {
+ for (int i = 0; i < BOMS.length; i++) {
+ if (matches(BOMS[i])) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean matches(int[] bom) {
+ if (bom.length != fbLength) {
+ return false;
+ }
+ for (int i = 0; i < bom.length; i++) {
+ if (bom[i] != firstBytes[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+}
Modified: incubator/rat/main/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java?rev=1156189&r1=1156188&r2=1156189&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java (original)
+++ incubator/rat/main/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java Wed Aug 10 14:02:41 2011
@@ -28,6 +28,8 @@ import java.util.Random;
import junit.framework.TestCase;
+import org.apache.rat.test.utils.Resources;
+
public class TestLicenceAppender extends TestCase {
/** Used to ensure that temporary files have unq */
private Random random = new Random();
@@ -423,7 +425,7 @@ public class TestLicenceAppender extends
},
checkLines(firstLine, null));
}
-
+
public void testAddLicenceToGroovy() throws IOException {
String filename = "tmp.groovy";
String firstLine = "/*";
@@ -455,4 +457,32 @@ public class TestLicenceAppender extends
checkLines(firstLine, null));
}
+ public void testFileWithBOM() throws IOException {
+ File f = Resources.getResourceFile("violations/FilterTest.cs");
+ try {
+ ApacheV2LicenceAppender appender =
+ new ApacheV2LicenceAppender();
+ appender.append(f);
+
+ BufferedReader r = null;
+ try {
+ r = new BufferedReader(new FileReader(f.getAbsolutePath()
+ + ".new"));
+ assertEquals("/*", r.readLine());
+ String line = null;
+ while ((line = r.readLine()) != null) {
+ if (line.trim().length() == 0) {
+ break;
+ }
+ }
+ assertEquals("#if NET_2_0", r.readLine());
+ } finally {
+ if (r != null) {
+ r.close();
+ }
+ }
+ } finally {
+ tryToDelete(new File(f.getAbsolutePath() + ".new"));
+ }
+ }
}