Buffer output and do not create new temp array streams

This commit is contained in:
Rene Pfeuffer
2019-11-18 11:41:09 +01:00
parent b35d407d96
commit cbee87fa0c
2 changed files with 65 additions and 23 deletions

View File

@@ -37,10 +37,13 @@ import org.eclipse.jgit.util.QuotedString;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffCommandBuilder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
*
* @author Sebastian Sdorra
@@ -73,17 +76,19 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
};
}
private static class DequoteOutputStream extends OutputStream {
static class DequoteOutputStream extends OutputStream {
private final OutputStream target;
private boolean afterNL = false;
private boolean writeToBuffer = false;
private int minusCount = 0;
private int plusCount = 0;
private ByteArrayOutputStream buffer;
private DequoteOutputStream(OutputStream target) {
this.target = target;
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DequoteOutputStream(OutputStream target) {
this.target = new BufferedOutputStream(target);
}
@Override
@@ -105,38 +110,40 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
afterNL = false;
target.write(i);
} else if (i == (int) ' ' && plusCount == 3) {
buffer = new ByteArrayOutputStream();
writeToBuffer = true;
buffer.reset();
afterNL = false;
plusCount = 0;
target.write(i);
} else if (i == (int) ' ' && minusCount == 3) {
minusCount = 0;
afterNL = false;
buffer = new ByteArrayOutputStream();
writeToBuffer = true;
buffer.reset();
target.write(i);
} else if (i == (int) '\n') {
if (buffer != null) {
afterNL = true;
afterNL = true;
if (writeToBuffer) {
byte[] bytes = buffer.toByteArray();
String dequote = QuotedString.GIT_PATH.dequote(bytes, 0, bytes.length);
target.write(dequote.getBytes());
target.write(i);
buffer = null;
} else {
afterNL = true;
target.write(i);
target.write(dequote.getBytes(UTF_8));
writeToBuffer = false;
}
target.write(i);
} else if (writeToBuffer) {
buffer.write(i);
afterNL = false;
} else {
if (buffer != null) {
buffer.write(i);
afterNL = false;
} else {
target.write(i);
afterNL = false;
minusCount = 0;
plusCount = 0;
}
target.write(i);
afterNL = false;
minusCount = 0;
plusCount = 0;
}
}
@Override
public void flush() throws IOException {
target.flush();
}
}
}

View File

@@ -0,0 +1,35 @@
package sonia.scm.repository.spi;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class GitDiffCommand_DequoteOutputStreamTest {
@Test
void shouldDequoteText() throws IOException {
String s = "diff --git \"a/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\" \"b/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\"\n" +
"new file mode 100644\n" +
"index 0000000..8cb0607\n" +
"--- /dev/null\n" +
"+++ \"b/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\"\n" +
"@@ -0,0 +1 @@\n" +
"+rthms";
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
GitDiffCommand.DequoteOutputStream stream = new GitDiffCommand.DequoteOutputStream(buffer);
byte[] bytes = s.getBytes();
stream.write(bytes, 0, bytes.length);
stream.flush();
Assertions.assertThat(buffer.toString()).isEqualTo("diff --git \"a/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\" \"b/\\303\\272\\303\\274\\303\\276\\303\\253\\303\\251\\303\\245\\303\\253\\303\\245\\303\\251 \\303\\245g\\303\\260f\\303\\237\"\n" +
"new file mode 100644\n" +
"index 0000000..8cb0607\n" +
"--- /dev/null\n" +
"+++ b/úüþëéåëåé ågðfß\n" +
"@@ -0,0 +1 @@\n" +
"+rthms");
}
}