Skip to content

Commit

Permalink
Refactor archive reading
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hannesbraun committed Aug 9, 2024
1 parent 6f84992 commit a8f1d26
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 234 deletions.
8 changes: 7 additions & 1 deletion src/main/java/rv/comm/rcssserver/Logfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.magmaoffenburg.roboviz.util.ArchiveUtilKt;
import rv.comm.drawing.commands.Command;

/**
Expand Down Expand Up @@ -79,7 +80,12 @@ public Logfile(File file, boolean execDrawCmds) throws Exception
*/
private void open() throws IOException
{
br = TarBz2ZipUtil.createBufferedReader(logsrc);
try {
br = ArchiveUtilKt.createBufferedReader(logsrc);
} catch (Exception e) {
LOGGER.error("Unable to open file", e);
br = null;
}
if (br != null) {
curFrameMsg = br.readLine();
if (curFrameMsg != null && curFrameMsg.startsWith("[")) {
Expand Down
233 changes: 0 additions & 233 deletions src/main/java/rv/comm/rcssserver/TarBz2ZipUtil.java

This file was deleted.

80 changes: 80 additions & 0 deletions src/main/kotlin/org/magmaoffenburg/roboviz/util/ArchiveUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.magmaoffenburg.roboviz.util

import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.compressors.CompressorStreamFactory
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.InputStream
import java.io.InputStreamReader
import java.util.zip.ZipFile
import kotlin.math.max

fun createBufferedReader(file: File): BufferedReader {
// Extract suffixes from filename to determine extraction method
val suffixes = file.name.lowercase().split('.').let { it.subList(max(0, it.size - 2), it.size) }.map {
// Fix a few common shortened suffixes
when (it) {
"tbz2" -> listOf("tar", "bz2")
"tgz" -> listOf("tar", "gz")
else -> listOf(it)
}
}.flatten()

var inputStream: InputStream = BufferedInputStream(FileInputStream(file))
for (suffix in suffixes.asReversed()) {
when (suffix) {
"bz2" -> inputStream =
CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2, inputStream)

"gz" -> inputStream =
CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, inputStream)

"tar" -> {
// Only works for the current layout of tar files
val tarStream = TarArchiveInputStream(inputStream)
var entry = tarStream.nextEntry

// Choose first file when not able to navigate deeper in the directory structure
while (entry != null && entry.isDirectory) {
val entries = entry.directoryEntries
entry = if (entries.isNotEmpty()) {
entries[0]
} else {
// empty directory
tarStream.nextEntry
}
}

// Search for proper file
while (entry != null && !entry.name.endsWith("sparkmonitor.log")) {
entry = tarStream.nextEntry
}

if (entry == null) {
throw FileNotFoundException("tar file does not contain logfile")
}

// We have reached the proper position
inputStream = tarStream
break
}

"zip" -> {
val zipFile = ZipFile(file)
if (zipFile.size() != 1) {
zipFile.close()
throw UnsupportedOperationException("Only support single entry zip files")
}
val zipEntry = zipFile.entries().nextElement()
inputStream = zipFile.getInputStream(zipEntry)
break
}

else -> break
}
}
return BufferedReader(InputStreamReader(inputStream))
}

0 comments on commit a8f1d26

Please sign in to comment.