Browse Source

Explicitly specify charset when constructing BufferedReader

Replace explicit construction of BufferedReader with calls to the
utility method Files.newBufferedReader, which allows to specify
the charset.

Change-Id: I61b9451dbc8d9cf83fc8a5981292b8fdc713ce37
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.2
David Pursehouse 6 years ago
parent
commit
62012d0bdd
  1. 59
      org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
  2. 6
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java

59
org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java

@ -52,9 +52,9 @@ import static org.junit.Assert.fail;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -474,8 +474,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
assertTrue("submodule should be checked out", hello.exists()); assertTrue("submodule should be checked out", hello.exists());
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"master world", content); "master world", content);
@ -565,8 +565,8 @@ public class RepoCommandTest extends RepositoryTestCase {
// The original file should exist // The original file should exist
File hello = new File(localDb.getWorkTree(), "foo/hello.txt"); File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
assertTrue("The original file should exist", hello.exists()); assertTrue("The original file should exist", hello.exists());
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("The original file should have expected content", assertEquals("The original file should have expected content",
"master world", content); "master world", content);
@ -574,8 +574,8 @@ public class RepoCommandTest extends RepositoryTestCase {
// The dest file should also exist // The dest file should also exist
hello = new File(localDb.getWorkTree(), "Hello"); hello = new File(localDb.getWorkTree(), "Hello");
assertTrue("The destination file should exist", hello.exists()); assertTrue("The destination file should exist", hello.exists());
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("The destination file should have expected content", assertEquals("The destination file should have expected content",
"master world", content); "master world", content);
@ -610,8 +610,8 @@ public class RepoCommandTest extends RepositoryTestCase {
assertTrue("The .gitmodules file should exist", assertTrue("The .gitmodules file should exist",
gitmodules.exists()); gitmodules.exists());
// The first line of .gitmodules file should be expected // The first line of .gitmodules file should be expected
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files
new FileReader(gitmodules))) { .newBufferedReader(gitmodules.toPath(), UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals( assertEquals(
"The first line of .gitmodules file should be as expected", "The first line of .gitmodules file should be as expected",
@ -644,8 +644,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
@ -671,8 +671,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
@ -698,8 +698,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
@ -771,8 +771,8 @@ public class RepoCommandTest extends RepositoryTestCase {
assertFalse("The foo/Hello file should be skipped", assertFalse("The foo/Hello file should be skipped",
foohello.exists()); foohello.exists());
// The content of Hello file should be expected // The content of Hello file should be expected
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("The Hello file should have expected content", assertEquals("The Hello file should have expected content",
"branch world", content); "branch world", content);
@ -829,8 +829,8 @@ public class RepoCommandTest extends RepositoryTestCase {
// The .gitmodules file should have 'submodule "bar"' and shouldn't // The .gitmodules file should have 'submodule "bar"' and shouldn't
// have // have
// 'submodule "foo"' lines. // 'submodule "foo"' lines.
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files
new FileReader(dotmodules))) { .newBufferedReader(dotmodules.toPath(), UTF_8)) {
boolean foo = false; boolean foo = false;
boolean bar = false; boolean bar = false;
while (true) { while (true) {
@ -879,8 +879,8 @@ public class RepoCommandTest extends RepositoryTestCase {
} }
// Check .gitmodules file // Check .gitmodules file
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files
new FileReader(dotmodules))) { .newBufferedReader(dotmodules.toPath(), UTF_8)) {
boolean foo = false; boolean foo = false;
boolean foobar = false; boolean foobar = false;
boolean a = false; boolean a = false;
@ -935,8 +935,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.call(); .call();
File hello = new File(localDb.getWorkTree(), "foo/hello.txt"); File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
assertTrue("submodule should be checked out", hello.exists()); assertTrue("submodule should be checked out", hello.exists());
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"master world", content); "master world", content);
@ -1074,8 +1074,9 @@ public class RepoCommandTest extends RepositoryTestCase {
".gitattributes"); ".gitattributes");
assertTrue("The .gitattributes file should exist", assertTrue("The .gitattributes file should exist",
gitattributes.exists()); gitattributes.exists());
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files
new FileReader(gitattributes));) { .newBufferedReader(gitattributes.toPath(),
UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals(".gitattributes content should be as expected", assertEquals(".gitattributes content should be as expected",
"/test a1 a2", content); "/test a1 a2", content);
@ -1142,8 +1143,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
@ -1169,8 +1170,8 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = Files.newBufferedReader(hello.toPath(),
new FileReader(hello))) { UTF_8)) {
String content = reader.readLine(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);

6
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java

@ -43,6 +43,7 @@
package org.eclipse.jgit.internal.storage.file; package org.eclipse.jgit.internal.storage.file;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX; import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK; import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
@ -50,7 +51,6 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files; import java.nio.file.Files;
@ -1034,8 +1034,8 @@ public class ObjectDirectory extends FileObjectDatabase {
} }
private static BufferedReader open(File f) private static BufferedReader open(File f)
throws FileNotFoundException { throws IOException, FileNotFoundException {
return new BufferedReader(new FileReader(f)); return Files.newBufferedReader(f.toPath(), UTF_8);
} }
private AlternateHandle openAlternate(String location) private AlternateHandle openAlternate(String location)

Loading…
Cancel
Save