Skip to content

Commit

Permalink
jenkinsci#185 Hash line content with its ordinal number to uniquely i…
Browse files Browse the repository at this point in the history
…dentify it
  • Loading branch information
tszmytka committed Jul 27, 2020
1 parent 15be927 commit a220892
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 50 deletions.
31 changes: 31 additions & 0 deletions src/main/java/hudson/plugins/ansicolor/action/LineIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hudson.plugins.ansicolor.action;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class LineIdentifier implements Serializable {
private static final String ALGORITHM = "SHA-256";
private MessageDigest messageDigest;

private MessageDigest getMessageDigest() {
if (messageDigest == null) {
try {
messageDigest = MessageDigest.getInstance(ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("Cannot get message digest", e);
}
}
return messageDigest;
}

public String hash(String lineContent, long lineNo) {
final String key = String.join("|", lineContent, String.valueOf(lineNo));
return Base64.getEncoder().encodeToString(getMessageDigest().digest(key.getBytes()));
}

public boolean isEqual(String lineContent, long lineNo, String other) {
return hash(lineContent, lineNo).equals(other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -52,10 +51,8 @@ private ActionContext findStartActionAt(File logFile, Collection<String> seriali
currentStartAction = newAction;
}
if (totalRead + read >= shortlogStart) {
// final String s = System.lineSeparator();
// final byte nl = (byte) '\n';
final int startInBuff = shortlogStart > totalRead ? (int) (shortlogStart - totalRead) : 0;
final int eolPos = indexOfEol(buf, /*nl,*/ startInBuff);
final int eolPos = indexOfEol(buf, startInBuff);
if (eolPos != -1) {
return new ActionContext(currentStartAction, new String(partialLine) + new String(buf, startInBuff, eolPos - startInBuff + EOL.length));
} else {
Expand All @@ -68,47 +65,6 @@ private ActionContext findStartActionAt(File logFile, Collection<String> seriali
} catch (IOException e) {
LOGGER.warning("Cannot search log for actions: " + e.getMessage());
}

/* try (
CountingInputStream inputStream = new CountingInputStream(new FileInputStream(logFile));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), BUFFER_SIZE);
) {
String currentStartAction = "";
final long shortlogStart = logFile.length() - bytesFromEnd * 1024L;
int posInBuff = 0;
int i = 0;
if (shortlogStart > 0) {
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(ConsoleNote.PREAMBLE_STR)) {
final Optional<String> startAction = serializedActions.stream().filter(line::contains).findFirst();
if (startAction.isPresent()) {
currentStartAction = startAction.get();
}
}
final long readBytes = inputStream.getByteCount();
if (readBytes >= shortlogStart) {
i++;
final String logLine = line + "\n";
posInBuff += logLine.length();
// final byte[] lineBytes = line.getBytes();
// posInBuff += lineBytes.length + 1; // System.lineSeparator().length();
// !line does NOT have to start at (readBytes - BUFFER_SIZE)!
final long shortlogStartBuff = shortlogStart - (readBytes - BUFFER_SIZE);
if (posInBuff >= shortlogStartBuff) {
return new ActionContext(currentStartAction, logLine.substring((int) (posInBuff - shortlogStartBuff - 21)));
// return new ActionContext(currentStartAction, new String(Arrays.copyOfRange(lineBytes, (int) (readBytes - shortlogStart), lineBytes.length-1)));
}
}
}
}
} catch (IOException e) {
LOGGER.warning("Cannot search log for actions: " + e.getMessage());
}*/
return new ActionContext();
}

Expand All @@ -125,15 +81,11 @@ private String findActionInBuffer(Collection<String> serializedActions, byte[] b
return "";
}


private int indexOfEol(byte[] buf, /*byte needle,*/ int after) {
private int indexOfEol(byte[] buf, int after) {
for (int i = after; i < buf.length; i++) {
if (Arrays.equals(Arrays.copyOfRange(buf, i, i + EOL.length), EOL)) {
return i;
}
// if (buf[i] == needle) {
// return i;
// }
}
return -1;
}
Expand Down

0 comments on commit a220892

Please sign in to comment.