Skip to content

Commit

Permalink
JENKINS-46420 Match console end of line delimiter
Browse files Browse the repository at this point in the history
Console output defaults to a limited set of end of line delimiters,
such as '\n'. Change to using the Scanner class which allows for
control of the delimiter character to ensure the LogFileReader class
matches between lines and recorded timestamps even when output contains
other newline characters.
  • Loading branch information
electrofelix committed Dec 4, 2017
1 parent 2fed9af commit 996c35a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/hudson/plugins/timestamper/io/LogFileReader.java
Expand Up @@ -25,12 +25,13 @@

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Scanner;

import javax.annotation.CheckForNull;

Expand Down Expand Up @@ -135,7 +136,7 @@ public String toString() {

private final Run<?, ?> build;

@CheckForNull private BufferedReader reader;
@CheckForNull private Scanner reader;

/**
* Create a log file reader for the given build.
Expand All @@ -157,10 +158,13 @@ public Optional<Line> nextLine() throws IOException {
return Optional.absent();
}
if (reader == null) {
reader = new BufferedReader(build.getLogReader());
reader = new Scanner(build.getLogReader());
reader.useDelimiter("\n");
}
String line = reader.readLine();
if (line == null) {
String line;
try {
line = reader.next();
} catch (NoSuchElementException e) {
return Optional.absent();
}
return Optional.of(new Line(line, build));
Expand All @@ -178,8 +182,10 @@ public int lineCount() throws IOException {
return 0;
}
int lineCount = 0;
try (BufferedReader reader = new BufferedReader(build.getLogReader())) {
while (reader.readLine() != null) {
try (Scanner reader = new Scanner(build.getLogReader())) {
reader.useDelimiter("\n");
while (reader.hasNext()) {
reader.next();
lineCount++;
}
}
Expand Down

0 comments on commit 996c35a

Please sign in to comment.