Browse Source
* master: Revert "Prepend hostname to subsection used to store file timestamp resolution" Remove use of org.bouncycastle.util.encoders.Hex Remove use of org.bouncycastle.util.io.TeeOutputStream Make the IMatcher public API SimilarityRenameDetector: Fix inconsistent indentation Use indexOf(char) and lastIndexOf(char) rather than String versions Reorder modifiers to follow Java Language Specification GitmoduleEntry: Remove redundant import of class from same package Remove redundant "static" qualifier from enum declarations Change-Id: Ie5a3f55229307c900c8974fcaeb1e77c1c87e87fnext
Matthias Sohn
5 years ago
90 changed files with 436 additions and 148 deletions
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Google Inc. |
||||||
|
* Copyright (C) 2020 Michael Dardis and others |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available under the |
||||||
|
* terms of the Eclipse Distribution License v. 1.0 which is available at |
||||||
|
* https://www.eclipse.org/org/documents/edl-v10.php.
|
||||||
|
* |
||||||
|
* SPDX-License-Identifier: BSD-3-Clause |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.eclipse.jgit.util; |
||||||
|
|
||||||
|
import static org.eclipse.jgit.util.Hex.decode; |
||||||
|
import static org.eclipse.jgit.util.Hex.toHexString; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
import org.eclipse.jgit.junit.JGitTestUtil; |
||||||
|
import org.eclipse.jgit.lib.Constants; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
public class HexTest { |
||||||
|
@Test |
||||||
|
public void testEncode() { |
||||||
|
assertEquals("68690a", toHexString(b("hi\n"))); |
||||||
|
assertEquals("0001020d0a0971", toHexString(b("\0\1\2\r\n\tq"))); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testDecode() { |
||||||
|
JGitTestUtil.assertEquals(b("hi\n"), decode("68690a")); |
||||||
|
JGitTestUtil.assertEquals(b("\0\1\2\r\n\tq"), decode("0001020d0a0971")); |
||||||
|
JGitTestUtil.assertEquals(b("\u000EB"), decode("0E42")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testEncodeMatchesDecode() { |
||||||
|
String[] testStrings = { "", "cow", "a", "a secret string", |
||||||
|
"\0\1\2\r\n\t" }; |
||||||
|
for (String e : testStrings) { |
||||||
|
JGitTestUtil.assertEquals(b(e), decode(toHexString(b(e)))); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static byte[] b(String str) { |
||||||
|
return Constants.encode(str); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,81 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2020, Michael Dardis. and others |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available under the |
||||||
|
* terms of the Eclipse Distribution License v. 1.0 which is available at |
||||||
|
* https://www.eclipse.org/org/documents/edl-v10.php.
|
||||||
|
* |
||||||
|
* SPDX-License-Identifier: BSD-3-Clause |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.util.io; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals; |
||||||
|
import static org.junit.Assert.assertFalse; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import org.eclipse.jgit.lib.Constants; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
public class TeeOutputStreamTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void test() throws IOException { |
||||||
|
byte[] data = Constants.encode("Hello World"); |
||||||
|
|
||||||
|
TestOutput first = new TestOutput(); |
||||||
|
TestOutput second = new TestOutput(); |
||||||
|
try (TeeOutputStream tee = new TeeOutputStream(first, second)) { |
||||||
|
tee.write(data); |
||||||
|
assertArrayEquals("Stream output must match", first.toByteArray(), |
||||||
|
second.toByteArray()); |
||||||
|
|
||||||
|
tee.write(1); |
||||||
|
assertArrayEquals("Stream output must match", first.toByteArray(), |
||||||
|
second.toByteArray()); |
||||||
|
|
||||||
|
tee.write(data, 1, 4); // Test partial write methods
|
||||||
|
assertArrayEquals("Stream output must match", first.toByteArray(), |
||||||
|
second.toByteArray()); |
||||||
|
} |
||||||
|
assertTrue("First stream should be closed", first.closed); |
||||||
|
assertTrue("Second stream should be closed", second.closed); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testCloseException() { |
||||||
|
TestOutput first = new TestOutput() { |
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
throw new IOException(); |
||||||
|
} |
||||||
|
|
||||||
|
}; |
||||||
|
TestOutput second = new TestOutput(); |
||||||
|
|
||||||
|
@SuppressWarnings("resource") |
||||||
|
TeeOutputStream tee = new TeeOutputStream(first, second); |
||||||
|
try { |
||||||
|
tee.close(); |
||||||
|
} catch (IOException ex) { |
||||||
|
// Expected from first closed
|
||||||
|
} |
||||||
|
assertFalse("First stream should not be closed", first.closed); |
||||||
|
assertTrue("Second stream should still be closed if first close failed", |
||||||
|
second.closed); |
||||||
|
} |
||||||
|
|
||||||
|
private static class TestOutput extends ByteArrayOutputStream { |
||||||
|
|
||||||
|
private boolean closed; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
closed = true; |
||||||
|
super.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,63 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2020, Michael Dardis. and others |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available under the |
||||||
|
* terms of the Eclipse Distribution License v. 1.0 which is available at |
||||||
|
* https://www.eclipse.org/org/documents/edl-v10.php.
|
||||||
|
* |
||||||
|
* SPDX-License-Identifier: BSD-3-Clause |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.eclipse.jgit.util; |
||||||
|
|
||||||
|
/** |
||||||
|
* Encodes and decodes to and from hexadecimal notation. |
||||||
|
* |
||||||
|
* @since 5.7 |
||||||
|
*/ |
||||||
|
public final class Hex { |
||||||
|
|
||||||
|
private static final char[] HEX = "0123456789abcdef".toCharArray(); //$NON-NLS-1$
|
||||||
|
|
||||||
|
/** Defeats instantiation. */ |
||||||
|
private Hex() { |
||||||
|
// empty
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Decode a hexadecimal string to a byte array. |
||||||
|
* |
||||||
|
* Note this method performs no validation on input content. |
||||||
|
* |
||||||
|
* @param s hexadecimal string |
||||||
|
* @return decoded array |
||||||
|
*/ |
||||||
|
public static byte[] decode(String s) { |
||||||
|
int len = s.length(); |
||||||
|
byte[] b = new byte[len / 2]; |
||||||
|
|
||||||
|
for (int i = 0; i < len; i += 2) { |
||||||
|
b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | Character.digit(s.charAt(i + 1), 16)); |
||||||
|
} |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Encode a byte array to a hexadecimal string. |
||||||
|
* |
||||||
|
* @param b byte array |
||||||
|
* @return hexadecimal string |
||||||
|
*/ |
||||||
|
public static String toHexString(byte[] b) { |
||||||
|
char[] c = new char[b.length * 2]; |
||||||
|
|
||||||
|
for (int i = 0; i < b.length; i++) { |
||||||
|
int v = b[i] & 0xFF; |
||||||
|
|
||||||
|
c[i * 2] = HEX[v >>> 4]; |
||||||
|
c[i * 2 + 1] = HEX[v & 0x0F]; |
||||||
|
} |
||||||
|
|
||||||
|
return new String(c); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,74 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2020, Michael Dardis. and others |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available under the |
||||||
|
* terms of the Eclipse Distribution License v. 1.0 which is available at |
||||||
|
* https://www.eclipse.org/org/documents/edl-v10.php.
|
||||||
|
* |
||||||
|
* SPDX-License-Identifier: BSD-3-Clause |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.eclipse.jgit.util.io; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.io.OutputStream; |
||||||
|
|
||||||
|
/** |
||||||
|
* An output stream that writes all data to two streams. |
||||||
|
* |
||||||
|
* @since 5.7 |
||||||
|
*/ |
||||||
|
public class TeeOutputStream extends OutputStream { |
||||||
|
|
||||||
|
private final OutputStream stream1; |
||||||
|
private final OutputStream stream2; |
||||||
|
|
||||||
|
/** |
||||||
|
* Initialize a tee output stream. |
||||||
|
* |
||||||
|
* @param stream1 first output stream |
||||||
|
* @param stream2 second output stream |
||||||
|
*/ |
||||||
|
public TeeOutputStream(OutputStream stream1, OutputStream stream2) { |
||||||
|
this.stream1 = stream1; |
||||||
|
this.stream2 = stream2; |
||||||
|
} |
||||||
|
|
||||||
|
/** {@inheritDoc} */ |
||||||
|
@Override |
||||||
|
public void write(byte[] buf) throws IOException { |
||||||
|
this.stream1.write(buf); |
||||||
|
this.stream2.write(buf); |
||||||
|
} |
||||||
|
|
||||||
|
/** {@inheritDoc} */ |
||||||
|
@Override |
||||||
|
public void write(byte[] buf, int off, int len) throws IOException { |
||||||
|
this.stream1.write(buf, off, len); |
||||||
|
this.stream2.write(buf, off, len); |
||||||
|
} |
||||||
|
|
||||||
|
/** {@inheritDoc} */ |
||||||
|
@Override |
||||||
|
public void write(int b) throws IOException { |
||||||
|
this.stream1.write(b); |
||||||
|
this.stream2.write(b); |
||||||
|
} |
||||||
|
|
||||||
|
/** {@inheritDoc} */ |
||||||
|
@Override |
||||||
|
public void flush() throws IOException { |
||||||
|
this.stream1.flush(); |
||||||
|
this.stream2.flush(); |
||||||
|
} |
||||||
|
|
||||||
|
/** {@inheritDoc} */ |
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
try { |
||||||
|
this.stream1.close(); |
||||||
|
} finally { |
||||||
|
this.stream2.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue