Skip to content

Commit

Permalink
Stop using uncompatible java IO api
Browse files Browse the repository at this point in the history
  • Loading branch information
wanasit committed Jun 19, 2020
1 parent d8847dd commit d84a9cc
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions kotori/src/main/kotlin/com/github/wanasit/kotori/utils/IOUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import java.io.InputStream
import java.io.OutputStream
import java.nio.ByteBuffer

object IO {

/**
*
*/
object String {


}


object Int {

}

}



object IOUtils {

fun writeStringArray(outputStream: OutputStream, value: Array<String>, includeSize: Boolean = true) {
Expand Down Expand Up @@ -76,8 +57,9 @@ object IOUtils {
}

fun readStringArray(inputStream: InputStream, stringLengthArray: IntArray): Array<String> {
val dataInputStream = DataInputStream(inputStream)
val output = Array(stringLengthArray.size) {
val bytes = inputStream.readNBytes(stringLengthArray[it] * 2)
val bytes = dataInputStream.readByteArray(stringLengthArray[it] * 2)
String(bytes, Charsets.UTF_16)
}

Expand All @@ -94,7 +76,7 @@ object IOUtils {
fun readShortArray(inputStream: InputStream, size: Int): ShortArray {
val dataInputStream = DataInputStream(inputStream)
val output = ShortArray(size)
ByteBuffer.wrap(dataInputStream.readNBytes(size * 2)).asShortBuffer().get(output)
ByteBuffer.wrap(dataInputStream.readByteArray(size * 2)).asShortBuffer().get(output)
return output
}

Expand All @@ -107,12 +89,19 @@ object IOUtils {
fun readIntArray(inputStream: InputStream, size: Int): IntArray {
val dataInputStream = DataInputStream(inputStream)
val output = IntArray(size)
ByteBuffer.wrap(dataInputStream.readNBytes(size * 4)).asIntBuffer().get(output)
ByteBuffer.wrap(dataInputStream.readByteArray(size * 4)).asIntBuffer().get(output)
return output
}

fun readInt(inputStream: InputStream): Int {
val dataInputStream = DataInputStream(inputStream)
return dataInputStream.readInt()
}

fun DataInputStream.readByteArray(size: Int): ByteArray {
val byteArray = ByteArray(size)
this.readFully(byteArray)

return byteArray
}
}

0 comments on commit d84a9cc

Please sign in to comment.