-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
317 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 32 additions & 5 deletions
37
big/src/main/kotlin/de/darkatra/bfme2/big/BigArchiveEntry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
package de.darkatra.bfme2.big | ||
|
||
import java.nio.file.Path | ||
import java.io.ByteArrayOutputStream | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
|
||
/** | ||
* Represents a single entry in a [BigArchive]. | ||
* | ||
* @param name The name of the entry. | ||
*/ | ||
class BigArchiveEntry( | ||
val file: Path, | ||
val name: String | ||
val name: String, | ||
internal val archive: BigArchive, | ||
internal var offset: UInt = 0u, | ||
internal var size: UInt = 0u, | ||
internal var hasPendingChanges: Boolean, | ||
internal val pendingOutputStream: ByteArrayOutputStream = ByteArrayOutputStream() | ||
) { | ||
val size: Long | ||
get() = file.toFile().length() | ||
|
||
/** | ||
* Obtains an input stream for this entry. | ||
*/ | ||
fun inputStream(): InputStream { | ||
if (hasPendingChanges) { | ||
return pendingOutputStream.toByteArray().inputStream() | ||
} | ||
|
||
return BigArchiveEntryInputStream(this) | ||
} | ||
|
||
/** | ||
* Obtains an output stream for this entry. | ||
*/ | ||
fun outputStream(): OutputStream { | ||
return BigArchiveEntryOutputStream(this, pendingOutputStream) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
big/src/main/kotlin/de/darkatra/bfme2/big/BigArchiveEntryInputStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.darkatra.bfme2.big | ||
|
||
import de.darkatra.bfme2.SkippingInputStream | ||
import java.io.InputStream | ||
import kotlin.io.path.inputStream | ||
|
||
internal class BigArchiveEntryInputStream( | ||
bigArchiveEntry: BigArchiveEntry | ||
) : InputStream() { | ||
|
||
private val inputStream = SkippingInputStream(bigArchiveEntry.archive.path.inputStream(), bigArchiveEntry.offset.toLong()) | ||
|
||
override fun read(): Int { | ||
return inputStream.read() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
big/src/main/kotlin/de/darkatra/bfme2/big/BigArchiveEntryOutputStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.darkatra.bfme2.big | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.io.FilterOutputStream | ||
|
||
internal class BigArchiveEntryOutputStream( | ||
private val bigArchiveEntry: BigArchiveEntry, | ||
private val outputStream: ByteArrayOutputStream | ||
) : FilterOutputStream(outputStream) { | ||
|
||
override fun flush() { | ||
bigArchiveEntry.hasPendingChanges = true | ||
bigArchiveEntry.size = outputStream.size().toUInt() | ||
outputStream.reset() | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
big/src/test/kotlin/de/darkatra/bfme2/big/BigArchiveTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package de.darkatra.bfme2.big | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.io.TempDir | ||
import java.nio.file.Path | ||
import kotlin.io.path.inputStream | ||
import kotlin.io.path.toPath | ||
|
||
internal class BigArchiveTest { | ||
|
||
private val testFile: Path = javaClass.getResource("/test/hello.txt")!!.toURI().toPath() | ||
private val testArchive: Path = javaClass.getResource("/test/hello.big")!!.toURI().toPath() | ||
|
||
@Test | ||
internal fun shouldWriteBigArchiveWithFilesToDisk(@TempDir tempDir: Path) { | ||
|
||
val tempFile = tempDir.resolve("out.big") | ||
val bigArchive = BigArchive(BigArchiveVersion.BIG_F, tempFile) | ||
|
||
val entry = bigArchive.createEntry("/test/hello.txt") | ||
|
||
assertThat(bigArchive.entries).hasSize(1) | ||
assertThat(bigArchive.entries[0]).isNotNull | ||
assertThat(bigArchive.entries[0].name).isEqualTo("/test/hello.txt") | ||
assertThat(bigArchive.entries[0].archive).isEqualTo(bigArchive) | ||
assertThat(bigArchive.entries[0].offset).isEqualTo(0u) | ||
assertThat(bigArchive.entries[0].size).isEqualTo(0u) | ||
assertThat(bigArchive.entries[0].hasPendingChanges).isTrue | ||
assertThat(bigArchive.entries[0].pendingOutputStream).isNotNull | ||
|
||
// write to the entry | ||
testFile.inputStream().use { input -> | ||
entry.outputStream().use { output -> | ||
input.transferTo(output) | ||
} | ||
} | ||
bigArchive.writeToDisk() | ||
|
||
assertThatExpectedEntryForTestFileExists(bigArchive) | ||
|
||
// write to the entry again (should override and produce the same result) | ||
testFile.inputStream().use { input -> | ||
entry.outputStream().use { output -> | ||
input.transferTo(output) | ||
} | ||
} | ||
bigArchive.writeToDisk() | ||
|
||
assertThatExpectedEntryForTestFileExists(bigArchive) | ||
} | ||
|
||
@Test | ||
internal fun shouldReadBigArchiveWithFiles() { | ||
|
||
val bigArchive = BigArchive.from(testArchive) | ||
|
||
assertThat(bigArchive.entries).hasSize(1) | ||
assertThat(bigArchive.entries[0]).isNotNull | ||
assertThat(bigArchive.entries[0].name).isEqualTo("/test/hello.txt") | ||
assertThat(bigArchive.entries[0].archive).isEqualTo(bigArchive) | ||
assertThat(bigArchive.entries[0].offset).isEqualTo(48u) | ||
assertThat(bigArchive.entries[0].size).isEqualTo(592u) | ||
assertThat(bigArchive.entries[0].hasPendingChanges).isFalse | ||
assertThat(bigArchive.entries[0].pendingOutputStream).isNotNull | ||
|
||
val entryBytes = bigArchive.entries[0].inputStream().use { it.readAllBytes() } | ||
assertThat(entryBytes).isEqualTo(testFile.inputStream().use { it.readAllBytes() }) | ||
} | ||
|
||
private fun assertThatExpectedEntryForTestFileExists(bigArchive: BigArchive) { | ||
assertThat(bigArchive.entries).hasSize(1) | ||
assertThat(bigArchive.entries[0]).isNotNull | ||
assertThat(bigArchive.entries[0].name).isEqualTo("/test/hello.txt") | ||
assertThat(bigArchive.entries[0].archive).isEqualTo(bigArchive) | ||
assertThat(bigArchive.entries[0].offset).isEqualTo(40u) | ||
assertThat(bigArchive.entries[0].size).isEqualTo(592u) | ||
assertThat(bigArchive.entries[0].hasPendingChanges).isFalse | ||
assertThat(bigArchive.entries[0].pendingOutputStream).isNotNull | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. |
Oops, something went wrong.