From 630d6d2fedeb2432bbea655a0102c7f58c657dde Mon Sep 17 00:00:00 2001 From: Minjae Song Date: Sat, 6 Oct 2018 01:16:26 +0900 Subject: [PATCH] new outputstream for bytearray64 --- .../virtualcomputer/tvd/ByteArray64.kt | 86 +++++++++++++++---- work_files/DataFormats/Map data format.txt | 1 + .../graphics/fonts/LatinExtA_variable.psd | 3 - .../graphics/fonts/LatinExtB_variable.psd | 3 - 4 files changed, 72 insertions(+), 21 deletions(-) delete mode 100644 work_files/graphics/fonts/LatinExtA_variable.psd delete mode 100644 work_files/graphics/fonts/LatinExtB_variable.psd diff --git a/src/net/torvald/terrarum/modulecomputers/virtualcomputer/tvd/ByteArray64.kt b/src/net/torvald/terrarum/modulecomputers/virtualcomputer/tvd/ByteArray64.kt index 904560660..fef99bde3 100644 --- a/src/net/torvald/terrarum/modulecomputers/virtualcomputer/tvd/ByteArray64.kt +++ b/src/net/torvald/terrarum/modulecomputers/virtualcomputer/tvd/ByteArray64.kt @@ -1,9 +1,7 @@ package net.torvald.terrarum.modulecomputers.virtualcomputer.tvd -import java.io.BufferedOutputStream -import java.io.File -import java.io.FileOutputStream -import java.io.InputStream +import java.io.* +import java.util.* /** @@ -18,7 +16,7 @@ class ByteArray64(val size: Long) { val bankSize: Int = 8192 } - private val data: Array + internal val __data: Array init { if (size < 0) @@ -26,7 +24,7 @@ class ByteArray64(val size: Long) { val requiredBanks: Int = 1 + ((size - 1) / bankSize).toInt() - data = Array( + __data = Array( requiredBanks, { bankIndex -> kotlin.ByteArray( @@ -48,14 +46,14 @@ class ByteArray64(val size: Long) { if (index < 0 || index >= size) throw ArrayIndexOutOfBoundsException("size $size, index $index") - data[index.toBankNumber()][index.toBankOffset()] = value + __data[index.toBankNumber()][index.toBankOffset()] = value } operator fun get(index: Long): Byte { if (index < 0 || index >= size) throw ArrayIndexOutOfBoundsException("size $size, index $index") - return data[index.toBankNumber()][index.toBankOffset()] + return __data[index.toBankNumber()][index.toBankOffset()] } operator fun iterator(): ByteIterator { @@ -99,7 +97,7 @@ class ByteArray64(val size: Long) { fun forEach(consumer: (Byte) -> Unit) = iterator().forEach { consumer(it) } fun forEachInt32(consumer: (Int) -> Unit) = iteratorChoppedToInt().forEach { consumer(it) } - fun forEachBanks(consumer: (ByteArray) -> Unit) = data.forEach(consumer) + fun forEachBanks(consumer: (ByteArray) -> Unit) = __data.forEach(consumer) fun sliceArray64(range: LongRange): ByteArray64 { val newarr = ByteArray64(range.last - range.first + 1) @@ -126,14 +124,14 @@ class ByteArray64(val size: Long) { fun writeToFile(file: File) { var fos = FileOutputStream(file, false) - fos.write(data[0]) + fos.write(__data[0]) fos.flush() fos.close() - if (data.size > 1) { + if (__data.size > 1) { fos = FileOutputStream(file, true) - for (i in 1..data.lastIndex) { - fos.write(data[i]) + for (i in 1..__data.lastIndex) { + fos.write(__data[i]) fos.flush() } fos.close() @@ -141,8 +139,8 @@ class ByteArray64(val size: Long) { } } -class ByteArray64InputStream(val byteArray64: ByteArray64): InputStream() { - private var readCounter = 0L +open class ByteArray64InputStream(val byteArray64: ByteArray64): InputStream() { + protected open var readCounter = 0L override fun read(): Int { readCounter += 1 @@ -154,4 +152,62 @@ class ByteArray64InputStream(val byteArray64: ByteArray64): InputStream() { -1 } } +} + +/** Static ByteArray OutputStream. Less leeway, more stable. */ +open class ByteArray64OutputStream(val byteArray64: ByteArray64): OutputStream() { + protected open var writeCounter = 0L + + override fun write(b: Int) { + try { + writeCounter += 1 + + byteArray64[writeCounter - 1] = b.toByte() + } + catch (e: ArrayIndexOutOfBoundsException) { + throw IOException(e) + } + } +} + +/** Just like Java's ByteArrayOutputStream, except DON'T TRY TO GROW THE BUFFER */ +open class ByteArray64GrowableOutputStream(val size: Long = ByteArray64.bankSize.toLong()): OutputStream() { + protected open var buf = ByteArray64(size) + protected open var count = 0L + + override fun write(b: Int) { + ensureCapacity(count + 1) + buf[count] = b.toByte() + count += 1 + } + + private fun ensureCapacity(minCapacity: Long) { + // overflow-conscious code + if (minCapacity - buf.size > 0) + grow(minCapacity) + } + + private fun grow(minCapacity: Long) { + // overflow-conscious code + val oldCapacity = buf.size + var newCapacity = oldCapacity shl 1 + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity + // double the capacity + val newBuffer = ByteArray64(buf.size * 2) + buf.__data.forEachIndexed { index, bytes -> + System.arraycopy( + buf.__data[index], 0, + newBuffer.__data[index], 0, buf.__data.size + ) + } + buf = newBuffer + System.gc() + } + + /** Unlike Java's, this does NOT create a copy of the internal buffer; this just returns its internal. */ + @Synchronized + fun toByteArray64(): ByteArray64 { + return buf + } } \ No newline at end of file diff --git a/work_files/DataFormats/Map data format.txt b/work_files/DataFormats/Map data format.txt index 6b39b2e71..a135052d4 100644 --- a/work_files/DataFormats/Map data format.txt +++ b/work_files/DataFormats/Map data format.txt @@ -15,6 +15,7 @@ Ord Hex Description 06 05 Number of payloads 07 01 Compression algorithm, 0 for none, 1 for DEFLATE, 2 for LZMA, otherwise undefined (maybe LZMA2 for the future?) + Value of 01 (DEFLATE) is recommended for its faster compression 08 World width 09 World width diff --git a/work_files/graphics/fonts/LatinExtA_variable.psd b/work_files/graphics/fonts/LatinExtA_variable.psd deleted file mode 100644 index 77eb7edf9..000000000 --- a/work_files/graphics/fonts/LatinExtA_variable.psd +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:fd633b8c4fdb0f434bf73ab9a4698077717749c6f5991b3a3dd689b5ce171286 -size 172690 diff --git a/work_files/graphics/fonts/LatinExtB_variable.psd b/work_files/graphics/fonts/LatinExtB_variable.psd deleted file mode 100644 index 0dd63501d..000000000 --- a/work_files/graphics/fonts/LatinExtB_variable.psd +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:2e28ef7f43cd7fa8b5be072355af86a9caca859d05e49dbabfb75b88f1e48866 -size 267455