diff options
author | olaaun <olaa@oath.com> | 2019-03-13 16:15:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-13 16:15:12 +0100 |
commit | 8e00586fc4f9828690ff50d31c05c036a0231005 (patch) | |
tree | 183b7965f4c935dd0e3e67f3cca98516c90449b0 /container-core/src/main | |
parent | b552a4ee99e256804829e062cd75280d5df3319c (diff) |
Check if log file is zipped (#8773)
* Check if log file is zipped
* Check that outputStream isn't already a GZIPOutputStream
* No longer depend on file type order
Diffstat (limited to 'container-core/src/main')
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/handler/LogReader.java | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java index b53290a1a08..95a0e9a6766 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java +++ b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java @@ -6,6 +6,7 @@ import com.yahoo.vespa.defaults.Defaults; import org.json.JSONException; import org.json.JSONObject; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UncheckedIOException; @@ -23,6 +24,7 @@ import java.util.LinkedList; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; class LogReader { @@ -46,16 +48,24 @@ class LogReader { for (Path file : getMatchingFiles(earliestLogThreshold, latestLogThreshold)) { StringBuilder filenameBuilder = new StringBuilder(); logDirectory.relativize(file).iterator().forEachRemaining(p -> filenameBuilder.append("-").append(p.getFileName().toString())); - json.put(filenameBuilder.substring(1), Base64.getEncoder().encodeToString(Files.readAllBytes(file))); + byte[] fileData = file.toString().endsWith(".gz") ? new GZIPInputStream(new ByteArrayInputStream(Files.readAllBytes(file))).readAllBytes() : Files.readAllBytes(file); + json.put(filenameBuilder.substring(1), Base64.getEncoder().encodeToString(fileData)); } return json; } void writeLogs(OutputStream outputStream, Instant earliestLogThreshold, Instant latestLogThreshold) { - try (OutputStream bos = new GZIPOutputStream(outputStream)) { + try { for (Path file : getMatchingFiles(earliestLogThreshold, latestLogThreshold)) { - Files.copy(file, bos); + if (file.toString().endsWith(".gz")) { + Files.copy(file, outputStream); + } else { + OutputStream zip = new GZIPOutputStream(outputStream); + Files.copy(file, zip); + zip.close(); + } } + outputStream.close(); } catch (IOException e) { throw new UncheckedIOException(e); } |