Browse Source

Merge "EolCanonicalizingInputStream: binary detection should be optional"

tags/v2.0.0.201206130900-r
Robin Rosenberg 12 years ago
parent
commit
9a9877da4e

+ 19
- 8
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/EolCanonicalizingInputStreamTest.java View File

@Test @Test
public void testLF() throws IOException { public void testLF() throws IOException {
final byte[] bytes = asBytes("1\n2\n3"); final byte[] bytes = asBytes("1\n2\n3");
test(bytes, bytes);
test(bytes, bytes, false);
} }


@Test @Test
public void testCR() throws IOException { public void testCR() throws IOException {
final byte[] bytes = asBytes("1\r2\r3"); final byte[] bytes = asBytes("1\r2\r3");
test(bytes, bytes);
test(bytes, bytes, false);
} }


@Test @Test
public void testCRLF() throws IOException { public void testCRLF() throws IOException {
test(asBytes("1\r\n2\r\n3"), asBytes("1\n2\n3"));
test(asBytes("1\r\n2\r\n3"), asBytes("1\n2\n3"), false);
} }


@Test @Test
public void testLFCR() throws IOException { public void testLFCR() throws IOException {
final byte[] bytes = asBytes("1\n\r2\n\r3"); final byte[] bytes = asBytes("1\n\r2\n\r3");
test(bytes, bytes);
test(bytes, bytes, false);
} }


@Test @Test
public void testEmpty() throws IOException { public void testEmpty() throws IOException {
final byte[] bytes = asBytes(""); final byte[] bytes = asBytes("");
test(bytes, bytes);
test(bytes, bytes, false);
} }


private void test(byte[] input, byte[] expected) throws IOException {
@Test
public void testBinaryDetect() throws IOException {
final byte[] bytes = asBytes("1\r\n2\r\n3\0");
test(bytes, bytes, true);
}

@Test
public void testBinaryDontDetect() throws IOException {
test(asBytes("1\r\n2\r\n3\0"), asBytes("1\n2\n3\0"), false);
}

private void test(byte[] input, byte[] expected, boolean detectBinary) throws IOException {
final InputStream bis1 = new ByteArrayInputStream(input); final InputStream bis1 = new ByteArrayInputStream(input);
final InputStream cis1 = new EolCanonicalizingInputStream(bis1);
final InputStream cis1 = new EolCanonicalizingInputStream(bis1, detectBinary);
int index1 = 0; int index1 = 0;
for (int b = cis1.read(); b != -1; b = cis1.read()) { for (int b = cis1.read(); b != -1; b = cis1.read()) {
assertEquals(expected[index1], (byte) b); assertEquals(expected[index1], (byte) b);
for (int bufferSize = 1; bufferSize < 10; bufferSize++) { for (int bufferSize = 1; bufferSize < 10; bufferSize++) {
final byte[] buffer = new byte[bufferSize]; final byte[] buffer = new byte[bufferSize];
final InputStream bis2 = new ByteArrayInputStream(input); final InputStream bis2 = new ByteArrayInputStream(input);
final InputStream cis2 = new EolCanonicalizingInputStream(bis2);
final InputStream cis2 = new EolCanonicalizingInputStream(bis2, detectBinary);


int read = 0; int read = 0;
for (int readNow = cis2.read(buffer, 0, buffer.length); readNow != -1 for (int readNow = cis2.read(buffer, 0, buffer.length); readNow != -1

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java View File

} }


private InputStream filterClean(InputStream in) throws IOException { private InputStream filterClean(InputStream in) throws IOException {
return new EolCanonicalizingInputStream(in);
return new EolCanonicalizingInputStream(in, true);
} }


/** /**
InputStream rawis = current().openInputStream(); InputStream rawis = current().openInputStream();
InputStream is; InputStream is;
if (getOptions().getAutoCRLF() != AutoCRLF.FALSE) if (getOptions().getAutoCRLF() != AutoCRLF.FALSE)
is = new EolCanonicalizingInputStream(rawis);
is = new EolCanonicalizingInputStream(rawis, true);
else else
is = rawis; is = rawis;
return is; return is;

+ 9
- 7
org.eclipse.jgit/src/org/eclipse/jgit/util/io/EolCanonicalizingInputStream.java View File

import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.diff.RawText;


/** /**
* An input stream which canonicalizes EOLs bytes on the fly to '\n', unless the
* first 8000 bytes indicate the stream is binary.
* An input stream which canonicalizes EOLs bytes on the fly to '\n'.
* *
* Note: Make sure to apply this InputStream only to text files!
* Optionally, a binary check on the first 8000 bytes is performed
* and in case of binary files, canonicalization is turned off
* (for the complete file).
*/ */
public class EolCanonicalizingInputStream extends InputStream { public class EolCanonicalizingInputStream extends InputStream {
private final byte[] single = new byte[1]; private final byte[] single = new byte[1];


private boolean isBinary; private boolean isBinary;


private boolean modeDetected;
private boolean detectBinary;


/** /**
* Creates a new InputStream, wrapping the specified stream * Creates a new InputStream, wrapping the specified stream
* @param in * @param in
* raw input stream * raw input stream
*/ */
public EolCanonicalizingInputStream(InputStream in) {
public EolCanonicalizingInputStream(InputStream in, boolean detectBinary) {
this.in = in; this.in = in;
this.detectBinary = detectBinary;
} }


@Override @Override
cnt = in.read(buf, 0, buf.length); cnt = in.read(buf, 0, buf.length);
if (cnt < 1) if (cnt < 1)
return false; return false;
if (!modeDetected) {
if (detectBinary) {
isBinary = RawText.isBinary(buf, cnt); isBinary = RawText.isBinary(buf, cnt);
modeDetected = true;
detectBinary = false;
} }
ptr = 0; ptr = 0;
return true; return true;

Loading…
Cancel
Save