Skip to content

Commit a8f9f01

Browse files
committed
- B ReadBuffer input no longer modifies newlines or adds them at the end
Added StringUtils.ensureEnding Added FileUtils.readFile(file, ensureTrailingNewline = true) Closes #637
1 parent de2dd21 commit a8f9f01

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ __pycache__/
2424
*.bak
2525

2626
.gitmessage
27+
.approval_tests_temp/

approvaltests-util-tests/src/test/java/com/spun/util/StringUtilsTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,12 @@ public void testRepeating()
152152
""";
153153
ParseInput.from(expected).withTypes(String.class, Integer.class).verifyAll((s, i) -> StringUtils.repeat(s, i));
154154
}
155+
@Test
156+
public void testEnsureEnding()
157+
{
158+
String first = "hello";
159+
String second = "hello\n";
160+
assertEquals(StringUtils.ensureEnding(first, "\n"), second);
161+
assertEquals(StringUtils.ensureEnding(second, "\n"), second);
162+
}
155163
}

approvaltests-util-tests/src/test/java/com/spun/util/io/FileUtilsTest.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import org.approvaltests.reporters.UseReporter;
77
import org.junit.jupiter.api.Test;
88

9-
import java.io.BufferedWriter;
10-
import java.io.File;
11-
import java.io.Reader;
12-
import java.io.StringReader;
9+
import java.io.*;
1310
import java.nio.charset.StandardCharsets;
1411
import java.nio.file.Files;
1512

@@ -78,4 +75,30 @@ public void testSaveToFile()
7875
File file = FileUtils.saveToFile("pre_", input);
7976
Approvals.verify(file);
8077
}
78+
@Test
79+
public void testReadBuffer()
80+
{
81+
String input = """
82+
This is line 1
83+
This is line 2
84+
""";
85+
assertReadBuffer(input);
86+
assertReadBuffer(input.trim());
87+
input = """
88+
This is line 1
89+
90+
This is line 2
91+
92+
93+
""";
94+
assertReadBuffer(input);
95+
assertReadBuffer(input.trim());
96+
}
97+
private static void assertReadBuffer(String input)
98+
{
99+
StringReader reader = new StringReader(input);
100+
BufferedReader bufferedReader = new BufferedReader(reader);
101+
String result = FileUtils.readBuffer(bufferedReader);
102+
assertEquals(input, result);
103+
}
81104
}

approvaltests-util/src/main/java/com/spun/util/StringUtils.java

+12
Original file line numberDiff line numberDiff line change
@@ -633,4 +633,16 @@ public static String repeat(String string, int times)
633633
{
634634
return new String(new char[times]).replace("\0", string);
635635
}
636+
public static String ensureEnding(String contents, String desiredEnding)
637+
{
638+
if (contents == null)
639+
{
640+
contents = "";
641+
}
642+
if (!contents.endsWith(desiredEnding))
643+
{
644+
contents += desiredEnding;
645+
}
646+
return contents;
647+
}
636648
}

approvaltests-util/src/main/java/com/spun/util/io/FileUtils.java

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.spun.util.io;
22

3-
import com.spun.util.ArrayUtils;
4-
import com.spun.util.Asserts;
5-
import com.spun.util.FormattedException;
6-
import com.spun.util.ObjectUtils;
3+
import com.spun.util.*;
74

85
import javax.imageio.ImageIO;
96
import java.awt.image.BufferedImage;
@@ -26,6 +23,7 @@
2623
import java.nio.file.*;
2724
import java.util.ArrayList;
2825
import java.util.List;
26+
import java.util.stream.Collectors;
2927
import java.util.stream.Stream;
3028

3129
/**
@@ -225,9 +223,17 @@ public static void writeFile(File file, InputStream data)
225223
}
226224
public static String readFile(String absolutePath)
227225
{
228-
return readFile(new File(absolutePath));
226+
return readFile(absolutePath, true);
227+
}
228+
public static String readFile(String absolutePath, boolean ensureTrailingNewline)
229+
{
230+
return readFile(new File(absolutePath), ensureTrailingNewline);
229231
}
230232
public static String readFile(File file)
233+
{
234+
return readFile(file, true);
235+
}
236+
public static String readFile(File file, boolean ensureTrailingNewline)
231237
{
232238
try
233239
{
@@ -237,7 +243,13 @@ public static String readFile(File file)
237243
decoder.onMalformedInput(CodingErrorAction.IGNORE);
238244
Reader reader = new InputStreamReader(Files.newInputStream(file.toPath()), decoder);
239245
BufferedReader in = new BufferedReader(reader);
240-
return readBuffer(in);
246+
String output = readBuffer(in);
247+
output = output.replaceAll("\r\n", "\n");
248+
if (ensureTrailingNewline)
249+
{
250+
output = StringUtils.ensureEnding(output, "\n");
251+
}
252+
return output;
241253
}
242254
catch (Throwable t)
243255
{
@@ -248,15 +260,13 @@ public static String readBuffer(BufferedReader in)
248260
{
249261
try
250262
{
251-
StringBuffer string = new StringBuffer();
252-
String line;
253-
while ((line = in.readLine()) != null)
263+
StringBuilder sb = new StringBuilder();
264+
int ch;
265+
while ((ch = in.read()) != -1)
254266
{
255-
string.append(line);
256-
string.append("\n");
267+
sb.append((char) ch);
257268
}
258-
in.close();
259-
return string.toString();
269+
return sb.toString();
260270
}
261271
catch (Throwable t)
262272
{

0 commit comments

Comments
 (0)