-
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.
refactoring read/write code to be more generic
- Loading branch information
Showing
9 changed files
with
286 additions
and
207 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package library | ||
|
||
import library.jvm.ByteBufferBinaryInputStream | ||
import java.nio.ByteBuffer | ||
|
||
public interface BinaryInputStream { | ||
public fun order(order: ByteOrder) | ||
public fun hasRemaining(): Boolean | ||
public fun position(): Long | ||
|
||
public fun readByte(): Byte | ||
public fun readShort(): Short | ||
public fun readInt(): Int | ||
public fun readLong(): Long | ||
|
||
public fun readArray(len: Int): ByteArray | ||
|
||
public companion object { | ||
public fun wrap(array: ByteArray): BinaryInputStream = | ||
ByteBufferBinaryInputStream(ByteBuffer.wrap(array)) | ||
} | ||
} |
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,11 @@ | ||
package library | ||
|
||
public interface BinaryOutputStream { | ||
public fun order(order: ByteOrder) | ||
public fun put(data: ByteArray) | ||
|
||
public fun writeByte(value: Byte) | ||
public fun writeShort(value: Short) | ||
public fun writeInt(value: Int) | ||
public fun writeLong(value: Long) | ||
} |
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,6 @@ | ||
package library | ||
|
||
public enum class ByteOrder { | ||
BIG_ENDIAN, | ||
LITTLE_ENDIAN | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/library/jvm/ByteBufferBinaryInputStream.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,35 @@ | ||
package library.jvm | ||
|
||
import library.BinaryInputStream | ||
import library.ByteOrder | ||
import java.nio.ByteBuffer | ||
|
||
public class ByteBufferBinaryInputStream(private val buffer: ByteBuffer) : BinaryInputStream { | ||
override fun order(order: ByteOrder) { | ||
buffer.order(order.asByteBufferByteOrder()) | ||
} | ||
|
||
override fun hasRemaining(): Boolean = | ||
buffer.hasRemaining() | ||
|
||
override fun position(): Long = | ||
buffer.position().toLong() | ||
|
||
override fun readByte(): Byte = | ||
buffer.get() | ||
|
||
override fun readShort(): Short = | ||
buffer.getShort() | ||
|
||
override fun readInt(): Int = | ||
buffer.getInt() | ||
|
||
override fun readLong(): Long = | ||
buffer.getLong() | ||
|
||
override fun readArray(len: Int): ByteArray { | ||
val data = ByteArray(len) | ||
buffer.get(data) | ||
return data | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/library/jvm/ByteBufferBinaryOutputStream.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,31 @@ | ||
package library.jvm | ||
|
||
import library.BinaryOutputStream | ||
import library.ByteOrder | ||
import java.nio.ByteBuffer | ||
|
||
public class ByteBufferBinaryOutputStream(private val buffer: ByteBuffer) : BinaryOutputStream { | ||
override fun order(order: ByteOrder) { | ||
buffer.order(order.asByteBufferByteOrder()) | ||
} | ||
|
||
override fun put(data: ByteArray) { | ||
buffer.put(data) | ||
} | ||
|
||
override fun writeByte(value: Byte) { | ||
buffer.put(value) | ||
} | ||
|
||
override fun writeShort(value: Short) { | ||
buffer.putShort(value) | ||
} | ||
|
||
override fun writeInt(value: Int) { | ||
buffer.putInt(value) | ||
} | ||
|
||
override fun writeLong(value: Long) { | ||
buffer.putLong(value) | ||
} | ||
} |
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,8 @@ | ||
package library.jvm | ||
|
||
import library.ByteOrder | ||
|
||
public fun ByteOrder.asByteBufferByteOrder(): java.nio.ByteOrder = when (this) { | ||
ByteOrder.LITTLE_ENDIAN -> java.nio.ByteOrder.LITTLE_ENDIAN | ||
ByteOrder.BIG_ENDIAN -> java.nio.ByteOrder.BIG_ENDIAN | ||
} |
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,83 @@ | ||
package library.jvm | ||
|
||
import dltcore.DltMessageParser | ||
import dltcore.DltReadStatus | ||
import library.BinaryInputStream | ||
import library.ByteOrder | ||
import java.nio.channels.FileChannel | ||
import java.nio.file.Path | ||
import java.nio.file.StandardOpenOption | ||
import kotlin.io.path.fileSize | ||
import kotlin.math.min | ||
|
||
public fun DltMessageParser.Companion.parseFile(path: Path): Sequence<DltReadStatus> { | ||
val bis = LargeFileByteBufferInputStream(path) | ||
return parseBuffer(bis, path.fileSize()) | ||
} | ||
|
||
private const val OVERLAP = 10_000_000 | ||
|
||
private class LargeFileByteBufferInputStream(path: Path) : BinaryInputStream { | ||
private lateinit var currentInputStream: BinaryInputStream | ||
|
||
private val fileSize = path.fileSize() | ||
private var fileChannel: FileChannel = FileChannel.open(path, StandardOpenOption.READ) | ||
private var absolutePosition = -1L | ||
private var bufferIndex = 0 | ||
|
||
private val buffer: BinaryInputStream | ||
get() { | ||
if (absolutePosition == -1L) { | ||
absolutePosition = 0 | ||
val buffer = fileChannel.map( | ||
FileChannel.MapMode.READ_ONLY, | ||
absolutePosition, | ||
min(fileSize, Integer.MAX_VALUE.toLong()) | ||
) | ||
currentInputStream = ByteBufferBinaryInputStream(buffer) | ||
bufferIndex = 0 | ||
return currentInputStream | ||
} | ||
val relativePosition = currentInputStream.position() | ||
if (relativePosition >= (Integer.MAX_VALUE - OVERLAP)) { | ||
absolutePosition += relativePosition | ||
val buffer = fileChannel.map( | ||
FileChannel.MapMode.READ_ONLY, | ||
absolutePosition, | ||
min(fileSize - absolutePosition, Integer.MAX_VALUE.toLong()) | ||
) | ||
currentInputStream = ByteBufferBinaryInputStream(buffer) | ||
} | ||
return currentInputStream | ||
} | ||
|
||
override fun order(order: ByteOrder) { | ||
buffer.order(order) | ||
} | ||
|
||
override fun hasRemaining(): Boolean { | ||
val remaining = buffer.hasRemaining() | ||
if (!remaining) { | ||
fileChannel.close() | ||
} | ||
return remaining | ||
} | ||
|
||
override fun position(): Long = | ||
absolutePosition + currentInputStream.position() | ||
|
||
override fun readByte(): Byte = | ||
buffer.readByte() | ||
|
||
override fun readShort(): Short = | ||
buffer.readShort() | ||
|
||
override fun readInt(): Int = | ||
buffer.readInt() | ||
|
||
override fun readLong(): Long = | ||
buffer.readLong() | ||
|
||
override fun readArray(len: Int): ByteArray = | ||
buffer.readArray(len) | ||
} |