Skip to content

Instantly share code, notes, and snippets.

@eutkin
Created January 25, 2022 13:12
Show Gist options
  • Save eutkin/b4c70bc45c65d72f2a008885412f2212 to your computer and use it in GitHub Desktop.
Save eutkin/b4c70bc45c65d72f2a008885412f2212 to your computer and use it in GitHub Desktop.
import zone.bi.iso8583.client.MessageType
import zone.bi.iso8583.client.Unpacker
import zone.bi.iso8583.serialize.serialize
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.channels.FileChannel
import java.nio.channels.SocketChannel
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption.*
fun main(args: Array<String>) {
val (dir) = args
Files
.list(Path.of(dir))
.use { paths ->
paths.forEach { path ->
SocketChannel.open(InetSocketAddress(9000)).use { socketChannel ->
FileChannel.open(path, READ)
.use { inputFileChannel ->
FileChannel.open(
Path.of(path.toFile().name.replaceFirst("in", "out")),
CREATE,
APPEND,
WRITE
)
.use { outputChannel ->
val start = System.currentTimeMillis()
var counter = 0
val buffer = ByteBuffer.allocate(2)
while (true) {
val position = inputFileChannel.position()
val readBytes = inputFileChannel.read(buffer)
if (readBytes < buffer.capacity()) {
break
}
val size = buffer.flip().order(ByteOrder.BIG_ENDIAN).short.toLong()
buffer.clear()
val transferBytes =
inputFileChannel.transferTo(position, size + readBytes, socketChannel)
inputFileChannel.position(position + transferBytes)
if (transferBytes < size) {
break
}
val readResponseBytes = socketChannel.read(buffer)
if (readResponseBytes < buffer.capacity()) {
break
}
val outputPosition = outputChannel.position()
val responseSize = buffer.flip().order(ByteOrder.BIG_ENDIAN).short.toLong()
buffer.clear()
val transferResponseBytes = outputChannel.transferFrom(
socketChannel,
outputChannel.position(),
responseSize.toLong()
)
outputChannel.position(outputPosition + transferResponseBytes)
counter++
}
println("${System.currentTimeMillis() - start} ms for $counter transactions")
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment