-
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
10 changed files
with
277 additions
and
205 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
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,21 @@ | ||
package library | ||
|
||
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,33 @@ | ||
package library | ||
|
||
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 | ||
} | ||
} |
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,29 @@ | ||
package library | ||
|
||
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,6 @@ | ||
package library | ||
|
||
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,6 @@ | ||
package library | ||
|
||
public enum class ByteOrder { | ||
BIG_ENDIAN, | ||
LITTLE_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,74 @@ | ||
package library | ||
|
||
import java.nio.channels.FileChannel | ||
import java.nio.file.Path | ||
import java.nio.file.StandardOpenOption | ||
import kotlin.io.path.fileSize | ||
import kotlin.math.min | ||
|
||
private const val OVERLAP = 10_000_000 | ||
|
||
internal 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) | ||
} |