Skip to content

Commit a3065a2

Browse files
ahornaceVladimir Kotal
authored andcommitted
Use new Java 11 methods to read/write string from/to files
1 parent 7d481d1 commit a3065a2

File tree

2 files changed

+34
-82
lines changed

2 files changed

+34
-82
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/util/IOUtils.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Copyright (c) 2011, Trond Norbye.
2323
* Portions Copyright (c) 2017, 2018, Chris Fraire <[email protected]>.
2424
*/
2525
package org.opengrok.indexer.util;
2626

2727
import java.io.BufferedInputStream;
28-
import java.io.BufferedReader;
2928
import java.io.Closeable;
3029
import java.io.File;
31-
import java.io.FileReader;
3230
import java.io.IOException;
3331
import java.io.InputStream;
3432
import java.io.InputStreamReader;
@@ -42,7 +40,6 @@
4240
import java.nio.file.attribute.BasicFileAttributes;
4341
import java.util.ArrayList;
4442
import java.util.Arrays;
45-
import java.util.HashMap;
4643
import java.util.List;
4744
import java.util.Map;
4845
import java.util.logging.Level;
@@ -220,13 +217,11 @@ public static Reader createBOMStrippedReader(InputStream stream, String defaultC
220217
/**
221218
* Byte-order markers.
222219
*/
223-
private static final Map<String, byte[]> BOMS = new HashMap<>();
224-
225-
static {
226-
BOMS.put("UTF-8", new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
227-
BOMS.put("UTF-16BE", new byte[]{(byte) 0xFE, (byte) 0xFF});
228-
BOMS.put("UTF-16LE", new byte[]{(byte) 0xFF, (byte) 0xFE});
229-
}
220+
private static final Map<String, byte[]> BOMS = Map.of(
221+
"UTF-8", new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF},
222+
"UTF-16BE", new byte[]{(byte) 0xFE, (byte) 0xFF},
223+
"UTF-16LE", new byte[]{(byte) 0xFF, (byte) 0xFE}
224+
);
230225

231226
/**
232227
* Gets a value indicating a UTF encoding if the array starts with a
@@ -278,38 +273,10 @@ public static String getFileContent(File file) {
278273
if (file == null || !file.canRead()) {
279274
return "";
280275
}
281-
FileReader fin = null;
282-
BufferedReader input = null;
283276
try {
284-
fin = new FileReader(file);
285-
input = new BufferedReader(fin);
286-
String line;
287-
StringBuilder contents = new StringBuilder();
288-
String EOL = System.getProperty("line.separator");
289-
while ((line = input.readLine()) != null) {
290-
contents.append(line).append(EOL);
291-
}
292-
return contents.toString();
293-
} catch (java.io.FileNotFoundException e) {
294-
LOGGER.log(Level.WARNING, "failed to find file: {0}",
295-
e.getMessage());
296-
} catch (java.io.IOException e) {
297-
LOGGER.log(Level.WARNING, "failed to read file: {0}",
298-
e.getMessage());
299-
} finally {
300-
if (input != null) {
301-
try {
302-
input.close();
303-
} catch (Exception e) {
304-
// nothing we can do about it
305-
}
306-
} else if (fin != null) {
307-
try {
308-
fin.close();
309-
} catch (Exception e) {
310-
// nothing we can do about it
311-
}
312-
}
277+
return Files.readString(file.toPath(), Charset.defaultCharset());
278+
} catch (IOException e) {
279+
LOGGER.log(Level.WARNING, "failed to read file: {0}", e.getMessage());
313280
}
314281
return "";
315282
}

opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/IncludeFilesTest.java

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.configuration;
2525

26-
import java.io.BufferedWriter;
27-
import java.io.File;
28-
import java.io.FileWriter;
2926
import java.io.IOException;
27+
import java.nio.charset.Charset;
3028
import java.nio.file.Files;
3129
import java.nio.file.Path;
3230
import org.junit.BeforeClass;
@@ -44,72 +42,59 @@ public class IncludeFilesTest {
4442
static final String CONTENT_1 = "foo";
4543
static final String CONTENT_2 = "bar";
4644
static RuntimeEnvironment env = RuntimeEnvironment.getInstance();
47-
static final String LINE_SEP = System.lineSeparator();
48-
45+
4946
@BeforeClass
5047
public static void setUpClass() throws IOException {
5148
includeRoot = Files.createTempDirectory("include_root");
5249
env.setIncludeRoot(includeRoot.toString());
5350
}
54-
55-
private void writeStringToFile(File file, String str) throws IOException {
56-
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
57-
bw.write(str);
58-
}
51+
52+
private void writeStringToFile(Path file, String str) throws IOException {
53+
Files.writeString(file, str, Charset.defaultCharset());
5954
}
60-
55+
6156
@Test
6257
public void testGetHeaderIncludeFileContent() throws IOException {
63-
File file = new File(includeRoot.toFile(), Configuration.HEADER_INCLUDE_FILE);
58+
Path file = includeRoot.resolve(Configuration.HEADER_INCLUDE_FILE);
6459
writeStringToFile(file, CONTENT_1);
65-
assertEquals(CONTENT_1 + LINE_SEP,
66-
env.includeFiles.getHeaderIncludeFileContent(false));
60+
assertEquals(CONTENT_1, env.includeFiles.getHeaderIncludeFileContent(false));
6761
writeStringToFile(file, CONTENT_2);
68-
assertEquals(CONTENT_2 + LINE_SEP,
69-
env.includeFiles.getHeaderIncludeFileContent(true));
62+
assertEquals(CONTENT_2, env.includeFiles.getHeaderIncludeFileContent(true));
7063
}
71-
64+
7265
@Test
7366
public void testGetBodyIncludeFileContent() throws IOException {
74-
File file = new File(includeRoot.toFile(), Configuration.BODY_INCLUDE_FILE);
67+
Path file = includeRoot.resolve(Configuration.BODY_INCLUDE_FILE);
7568
writeStringToFile(file, CONTENT_1);
76-
assertEquals(CONTENT_1 + LINE_SEP,
77-
env.includeFiles.getBodyIncludeFileContent(false));
69+
assertEquals(CONTENT_1, env.includeFiles.getBodyIncludeFileContent(false));
7870
writeStringToFile(file, CONTENT_2);
79-
assertEquals(CONTENT_2 + LINE_SEP,
80-
env.includeFiles.getBodyIncludeFileContent(true));
71+
assertEquals(CONTENT_2, env.includeFiles.getBodyIncludeFileContent(true));
8172
}
82-
73+
8374
@Test
8475
public void testGetFooterIncludeFileContent() throws IOException {
85-
File file = new File(includeRoot.toFile(), Configuration.FOOTER_INCLUDE_FILE);
76+
Path file = includeRoot.resolve(Configuration.FOOTER_INCLUDE_FILE);
8677
writeStringToFile(file, CONTENT_1);
87-
assertEquals(CONTENT_1 + LINE_SEP,
88-
env.includeFiles.getFooterIncludeFileContent(false));
78+
assertEquals(CONTENT_1, env.includeFiles.getFooterIncludeFileContent(false));
8979
writeStringToFile(file, CONTENT_2);
90-
assertEquals(CONTENT_2 + LINE_SEP,
91-
env.includeFiles.getFooterIncludeFileContent(true));
80+
assertEquals(CONTENT_2, env.includeFiles.getFooterIncludeFileContent(true));
9281
}
93-
82+
9483
@Test
9584
public void testGetForbiddenIncludeFileContent() throws IOException {
96-
File file = new File(includeRoot.toFile(), Configuration.E_FORBIDDEN_INCLUDE_FILE);
85+
Path file = includeRoot.resolve(Configuration.E_FORBIDDEN_INCLUDE_FILE);
9786
writeStringToFile(file, CONTENT_1);
98-
assertEquals(CONTENT_1 + LINE_SEP,
99-
env.includeFiles.getForbiddenIncludeFileContent(false));
87+
assertEquals(CONTENT_1, env.includeFiles.getForbiddenIncludeFileContent(false));
10088
writeStringToFile(file, CONTENT_2);
101-
assertEquals(CONTENT_2 + LINE_SEP,
102-
env.includeFiles.getForbiddenIncludeFileContent(true));
89+
assertEquals(CONTENT_2, env.includeFiles.getForbiddenIncludeFileContent(true));
10390
}
10491

10592
@Test
10693
public void testGetHttpHeaderIncludeFileContent() throws IOException {
107-
File file = new File(includeRoot.toFile(), Configuration.HTTP_HEADER_INCLUDE_FILE);
94+
Path file = includeRoot.resolve(Configuration.HTTP_HEADER_INCLUDE_FILE);
10895
writeStringToFile(file, CONTENT_1);
109-
assertEquals(CONTENT_1 + LINE_SEP,
110-
env.includeFiles.getHttpHeaderIncludeFileContent(false));
96+
assertEquals(CONTENT_1, env.includeFiles.getHttpHeaderIncludeFileContent(false));
11197
writeStringToFile(file, CONTENT_2);
112-
assertEquals(CONTENT_2 + LINE_SEP,
113-
env.includeFiles.getHttpHeaderIncludeFileContent(true));
98+
assertEquals(CONTENT_2, env.includeFiles.getHttpHeaderIncludeFileContent(true));
11499
}
115100
}

0 commit comments

Comments
 (0)