Skip to content

Commit a8f1d26

Browse files
committed
Refactor archive reading
Writing archives is no longer included as this is not required for RoboViz. This also fixes a regression introduced with 0658dff where zip files could no longer be read because the file was immediately closed again through the try-with-resources block.
1 parent 6f84992 commit a8f1d26

File tree

3 files changed

+87
-234
lines changed

3 files changed

+87
-234
lines changed

src/main/java/rv/comm/rcssserver/Logfile.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import org.apache.logging.log4j.LogManager;
2626
import org.apache.logging.log4j.Logger;
27+
import org.magmaoffenburg.roboviz.util.ArchiveUtilKt;
2728
import rv.comm.drawing.commands.Command;
2829

2930
/**
@@ -79,7 +80,12 @@ public Logfile(File file, boolean execDrawCmds) throws Exception
7980
*/
8081
private void open() throws IOException
8182
{
82-
br = TarBz2ZipUtil.createBufferedReader(logsrc);
83+
try {
84+
br = ArchiveUtilKt.createBufferedReader(logsrc);
85+
} catch (Exception e) {
86+
LOGGER.error("Unable to open file", e);
87+
br = null;
88+
}
8389
if (br != null) {
8490
curFrameMsg = br.readLine();
8591
if (curFrameMsg != null && curFrameMsg.startsWith("[")) {

src/main/java/rv/comm/rcssserver/TarBz2ZipUtil.java

-233
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.magmaoffenburg.roboviz.util
2+
3+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
4+
import org.apache.commons.compress.compressors.CompressorStreamFactory
5+
import java.io.BufferedInputStream
6+
import java.io.BufferedReader
7+
import java.io.File
8+
import java.io.FileInputStream
9+
import java.io.FileNotFoundException
10+
import java.io.InputStream
11+
import java.io.InputStreamReader
12+
import java.util.zip.ZipFile
13+
import kotlin.math.max
14+
15+
fun createBufferedReader(file: File): BufferedReader {
16+
// Extract suffixes from filename to determine extraction method
17+
val suffixes = file.name.lowercase().split('.').let { it.subList(max(0, it.size - 2), it.size) }.map {
18+
// Fix a few common shortened suffixes
19+
when (it) {
20+
"tbz2" -> listOf("tar", "bz2")
21+
"tgz" -> listOf("tar", "gz")
22+
else -> listOf(it)
23+
}
24+
}.flatten()
25+
26+
var inputStream: InputStream = BufferedInputStream(FileInputStream(file))
27+
for (suffix in suffixes.asReversed()) {
28+
when (suffix) {
29+
"bz2" -> inputStream =
30+
CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2, inputStream)
31+
32+
"gz" -> inputStream =
33+
CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, inputStream)
34+
35+
"tar" -> {
36+
// Only works for the current layout of tar files
37+
val tarStream = TarArchiveInputStream(inputStream)
38+
var entry = tarStream.nextEntry
39+
40+
// Choose first file when not able to navigate deeper in the directory structure
41+
while (entry != null && entry.isDirectory) {
42+
val entries = entry.directoryEntries
43+
entry = if (entries.isNotEmpty()) {
44+
entries[0]
45+
} else {
46+
// empty directory
47+
tarStream.nextEntry
48+
}
49+
}
50+
51+
// Search for proper file
52+
while (entry != null && !entry.name.endsWith("sparkmonitor.log")) {
53+
entry = tarStream.nextEntry
54+
}
55+
56+
if (entry == null) {
57+
throw FileNotFoundException("tar file does not contain logfile")
58+
}
59+
60+
// We have reached the proper position
61+
inputStream = tarStream
62+
break
63+
}
64+
65+
"zip" -> {
66+
val zipFile = ZipFile(file)
67+
if (zipFile.size() != 1) {
68+
zipFile.close()
69+
throw UnsupportedOperationException("Only support single entry zip files")
70+
}
71+
val zipEntry = zipFile.entries().nextElement()
72+
inputStream = zipFile.getInputStream(zipEntry)
73+
break
74+
}
75+
76+
else -> break
77+
}
78+
}
79+
return BufferedReader(InputStreamReader(inputStream))
80+
}

0 commit comments

Comments
 (0)