Skip to content
Permalink
Browse files

JENKINS-46420 Match console end of line delimiter

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 996c35a1e49f3ed914b32e105b16e57b66cbff15
Showing with 13 additions and 7 deletions.
  1. +13 −7 src/main/java/hudson/plugins/timestamper/io/LogFileReader.java
@@ -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;

@@ -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.
@@ -157,10 +158,13 @@ public LogFileReader(Run<?, ?> build) {
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));
@@ -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++;
}
}

0 comments on commit 996c35a

Please sign in to comment.
You can’t perform that action at this time.