-
Notifications
You must be signed in to change notification settings - Fork 61
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
1 parent
8647d6a
commit 41e7279
Showing
5 changed files
with
72 additions
and
3 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
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
58 changes: 58 additions & 0 deletions
58
core/config/src/main/java/io/gatehill/imposter/config/util/BinarySerialisationUtil.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,58 @@ | ||
package io.gatehill.imposter.config.util | ||
|
||
import com.esotericsoftware.kryo.Kryo | ||
import com.esotericsoftware.kryo.io.Input | ||
import com.esotericsoftware.kryo.io.Output | ||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.ObjectInputStream | ||
import java.io.ObjectOutputStream | ||
|
||
object BinarySerialisationUtil { | ||
val kryo = Kryo().apply { | ||
isRegistrationRequired = false | ||
references = true | ||
} | ||
|
||
/** | ||
* Serialises an object to a file using Kryo. | ||
*/ | ||
fun serialise(obj: Any, filePath: String) { | ||
File(filePath).outputStream().use { | ||
val output = Output(it) | ||
kryo.writeClassAndObject(output, obj) | ||
output.close() | ||
} | ||
} | ||
|
||
/** | ||
* Deserialises an object from a file using Kryo. | ||
*/ | ||
fun <T> deserialise(filePath: String): T { | ||
return File(filePath).inputStream().use { | ||
val input = Input(it) | ||
val obj = kryo.readClassAndObject(input) as T | ||
input.close() | ||
obj | ||
} | ||
} | ||
|
||
fun serialiseJOS(obj: Any, filePath: String) { | ||
ByteArrayOutputStream().use { byteArrayOutputStream -> | ||
ObjectOutputStream(byteArrayOutputStream).use { objectOutputStream -> | ||
objectOutputStream.writeObject(obj) | ||
java.io.File(filePath).writeBytes(byteArrayOutputStream.toByteArray()) | ||
} | ||
} | ||
} | ||
|
||
fun <T> deserialiseJOS(filePath: String): T { | ||
val bytes = java.io.File(filePath).readBytes() | ||
return ByteArrayInputStream(bytes).use { byteArrayInputStream -> | ||
ObjectInputStream(byteArrayInputStream).use { objectInputStream -> | ||
objectInputStream.readObject() | ||
} | ||
} as T | ||
} | ||
} |
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