Skip to content

Commit

Permalink
Make TextFile internally consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 28, 2021
1 parent 106ace3 commit 98fb885
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 8 additions & 5 deletions core/src/main/java/hudson/util/TextFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public void delete() throws IOException {
}

/**
* Reads the entire contents and returns it.
* Reads the entire contents and returns it. Bytes from the file are decoded into characters
* using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
*/
public String read() throws IOException {
StringWriter out = new StringWriter();
Expand Down Expand Up @@ -107,12 +108,13 @@ public void write(String text) throws IOException {
}

/**
* Reads the first N characters or until we hit EOF.
* Reads the first N characters or until we hit EOF. Bytes from the file are decoded into
* characters using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
*/
public @NonNull String head(int numChars) throws IOException {
char[] buf = new char[numChars];
int read = 0;
try (Reader r = Files.newBufferedReader(Util.fileToPath(file), Charset.defaultCharset())) {
try (Reader r = Files.newBufferedReader(Util.fileToPath(file), StandardCharsets.UTF_8)) {
while (read<numChars) {
int d = r.read(buf,read,buf.length-read);
if (d<0)
Expand Down Expand Up @@ -173,10 +175,11 @@ public void write(String text) throws IOException {
}

/**
* Uses the platform default encoding.
* Bytes from the file are decoded into characters using the {@link StandardCharsets#UTF_8
* UTF-8} {@link Charset charset}.
*/
public @NonNull String fastTail(int numChars) throws IOException {
return fastTail(numChars,Charset.defaultCharset());
return fastTail(numChars, StandardCharsets.UTF_8);
}


Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/hudson/util/TextFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Rule;
Expand All @@ -31,7 +32,7 @@ public void head() throws Exception {
@Test
public void shortHead() throws Exception {
File f = tmp.newFile();
FileUtils.write(f, "hello", Charset.defaultCharset());
FileUtils.write(f, "hello", StandardCharsets.UTF_8);

TextFile t = new TextFile(f);
assertEquals("hello", t.head(35));
Expand All @@ -41,7 +42,7 @@ public void shortHead() throws Exception {
public void tail() throws Exception {
File f = tmp.newFile();
FileUtils.copyURLToFile(getClass().getResource("ascii.txt"), f);
String whole = FileUtils.readFileToString(f, Charset.defaultCharset());
String whole = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
TextFile t = new TextFile(f);
String tailStr = whole.substring(whole.length() - 34);
assertEquals(tailStr, t.fastTail(tailStr.length()));
Expand All @@ -50,7 +51,7 @@ public void tail() throws Exception {
@Test
public void shortTail() throws Exception {
File f = tmp.newFile();
FileUtils.write(f, "hello", Charset.defaultCharset());
FileUtils.write(f, "hello", StandardCharsets.UTF_8);

TextFile t = new TextFile(f);
assertEquals("hello", t.fastTail(35));
Expand Down

0 comments on commit 98fb885

Please sign in to comment.