Skip to content

Commit

Permalink
Use stream api instead of while loop to read tail of file
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashinsch committed Mar 29, 2015
1 parent d693556 commit a07dd87
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/lib.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
compile("org.ocpsoft.prettytime:prettytime:3.2.7.Final")
compile("commons-io:commons-io:2.4")
compile("org.apache.commons:commons-compress:1.9")
compile("com.codepoetics:protonpack:1.4")

testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.hinsch.spring.boot.actuator.logview;

import com.codepoetics.protonpack.StreamUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReversedLinesFileReader;

Expand All @@ -11,9 +12,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

/**
* Created by lh on 28/02/15.
Expand Down Expand Up @@ -74,14 +76,20 @@ private File getFile(Path folder, String filename) {
@Override
public void tailContent(Path folder, String filename, OutputStream stream, int lines) throws IOException {
try (ReversedLinesFileReader reader = new ReversedLinesFileReader(getFile(folder, filename))) {
int i = 0;
String line;
List<String> content = new ArrayList<>();
while ((line = reader.readLine()) != null && i++ < lines) {
content.add(line);
}
Collections.reverse(content);
List<String> content = StreamUtils.takeWhile(
Stream.generate(() -> readLine(reader)),
line -> line != null)
.limit(lines)
.collect(LinkedList::new, LinkedList::addFirst, LinkedList::addAll);
IOUtils.writeLines(content, System.lineSeparator(), stream);
}
}

private String readLine(ReversedLinesFileReader reader) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException("cannot read line", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,20 @@ public void shouldTailViewOnlyLastLine() throws Exception {
assertThat(new String(outputStream.toByteArray()), containsString("line2"));
}

@Test
public void shouldTailHandleMoreRequestedLinesThanExist() throws Exception {
// given
createFile("file.log", "line1" + System.lineSeparator() + "line2" + System.lineSeparator(), now);
ByteArrayServletOutputStream outputStream = mockResponseOutputStream();

// when
logViewEndpoint.view("file.log", null, 3, response);

// then
assertThat(new String(outputStream.toByteArray()), containsString("line1"));
assertThat(new String(outputStream.toByteArray()), containsString("line2"));
}

@Test
public void shouldSearchInFiles() throws Exception {
// given
Expand Down

0 comments on commit a07dd87

Please sign in to comment.