1
1
package me.shedaniel.linkie
2
2
3
- import org.boon.primitive.ByteBuf
4
- import org.boon.primitive.InputByteArray
5
- import java.io.ByteArrayInputStream
6
- import java.io.ByteArrayOutputStream
7
- import java.util.zip.GZIPInputStream
8
- import java.util.zip.GZIPOutputStream
3
+ import okio.Buffer
4
+ import okio.BufferedSink
5
+ import okio.BufferedSource
6
+ import kotlin.properties.Delegates
9
7
10
8
fun ByteBuffer.writeMappingsContainer (mappingsContainer : MappingsContainer ) {
11
9
writeNotNullString(mappingsContainer.version)
@@ -155,73 +153,53 @@ fun ByteBuffer.readMagic(original: String): String? {
155
153
}
156
154
}
157
155
158
- fun inputBuffer (capacity : Int = 2048): ByteBuffer = ByteBuffer (input = ByteBuf .create(capacity))
159
- fun outputBuffer (byteArray : ByteArray ): ByteBuffer = ByteBuffer (output = InputByteArray (byteArray))
160
- fun outputCompressedBuffer (byteArray : ByteArray ): ByteBuffer = outputBuffer(GZIPInputStream (ByteArrayInputStream (byteArray)).use { it.readBytes() })
156
+ fun inputBuffer (): ByteBuffer = ByteBuffer (Buffer () as BufferedSink )
157
+ fun outputBuffer (byteArray : ByteArray ): ByteBuffer = ByteBuffer (Buffer () as BufferedSource ).apply { writeByteArray(byteArray) }
158
+ @JvmName(" outputBuffer_" )
159
+ fun ByteArray.outputBuffer (): ByteBuffer = outputBuffer(this )
161
160
162
161
@OptIn(ExperimentalUnsignedTypes ::class )
163
162
@Suppress(" unused" )
164
- class ByteBuffer (private val input : ByteBuf ? = null , private val output : InputByteArray ? = null ) {
165
- fun writeByte (byte : Byte ) {
166
- input!! .add(byte)
167
- }
168
-
169
- fun writeByteArray (array : ByteArray ) {
170
- input!! .add(array)
171
- }
172
-
173
- fun writeBoolean (boolean : Boolean ) {
174
- input!! .writeBoolean(boolean)
175
- }
176
-
177
- fun writeShort (short : Short ) {
178
- input!! .add(short)
179
- }
180
-
181
- fun writeUnsignedShort (short : UShort ) {
182
- input!! .add(short.toShort())
183
- }
184
-
185
- fun writeInt (int : Int ) {
186
- input!! .add(int)
187
- }
188
-
189
- fun writeLong (long : Long ) {
190
- input!! .add(long)
191
- }
163
+ class ByteBuffer {
164
+ var source by Delegates .notNull<BufferedSource >()
165
+ var sink by Delegates .notNull<BufferedSink >()
192
166
193
- fun writeFloat ( float : Float ) {
194
- input !! .add(float)
167
+ constructor (source : BufferedSource ) {
168
+ this .source = source
195
169
}
196
170
197
- fun writeDouble ( double : Double ) {
198
- input !! .add(double)
171
+ constructor (sink : BufferedSink ) {
172
+ this .sink = sink
199
173
}
200
174
201
- fun writeChar (char : Char ) {
202
- input!! .add(char)
203
- }
175
+ fun writeByte (byte : Byte ) = sink.writeByte(byte.toInt())
176
+ fun writeByteArray (array : ByteArray ) = sink.write(array)
177
+ fun writeBoolean (boolean : Boolean ) = sink.writeByte(if (boolean) 1 else 0 )
178
+ fun writeShort (short : Short ) = sink.writeShort(short.toInt())
179
+ fun writeUnsignedShort (short : UShort ) = writeShort(short.toShort())
180
+ fun writeInt (int : Int ) = sink.writeInt(int)
181
+ fun writeLong (long : Long ) = sink.writeLong(long)
182
+ fun writeFloat (float : Float ) = writeInt(float.toBits())
183
+ fun writeDouble (double : Double ) = writeLong(double.toBits())
184
+ fun writeChar (char : Char ) = writeByte(char.toByte())
204
185
205
186
inline fun <T > writeCollection (collection : Collection <T >, crossinline writer : ByteBuffer .(T ) -> Unit ) {
206
187
writeInt(collection.size)
207
188
collection.forEach { writer(this , it) }
208
189
}
209
190
210
- fun toByteArray (): ByteArray = input!! .toBytes()
211
- fun toCompressedByteArray (): ByteArray = ByteArrayOutputStream ().also { outputStream ->
212
- GZIPOutputStream (outputStream).use { it.write(toByteArray()) }
213
- }.toByteArray()
214
-
215
- fun readByte (): Byte = output!! .readByte()
216
- fun readByteArray (length : Int ): ByteArray = output!! .readBytes(length)
217
- fun readBoolean (): Boolean = output!! .readBoolean()
218
- fun readShort (): Short = output!! .readShort()
219
- fun readUnsignedShort (): UShort = output!! .readShort().toUShort()
220
- fun readInt (): Int = output!! .readInt()
221
- fun readLong (): Long = output!! .readLong()
222
- fun readFloat (): Float = output!! .readFloat()
223
- fun readDouble (): Double = output!! .readDouble()
224
- fun readChar (): Char = output!! .readChar()
191
+ fun toByteArray (): ByteArray = source.readByteArray()
192
+
193
+ fun readByte (): Byte = source.readByte()
194
+ fun readByteArray (length : Int ): ByteArray = source.readByteArray(length.toLong())
195
+ fun readBoolean (): Boolean = source.readByte().toInt() == 1
196
+ fun readShort (): Short = source.readShort()
197
+ fun readUnsignedShort (): UShort = source.readShort().toUShort()
198
+ fun readInt (): Int = source.readInt()
199
+ fun readLong (): Long = source.readLong()
200
+ fun readFloat (): Float = Float .fromBits(source.readInt())
201
+ fun readDouble (): Double = Double .fromBits(source.readLong())
202
+ fun readChar (): Char = source.readByte().toChar()
225
203
226
204
inline fun <T > readCollection (crossinline reader : ByteBuffer .() -> T ): List <T > {
227
205
val size = readInt()
@@ -242,14 +220,14 @@ class ByteBuffer(private val input: ByteBuf? = null, private val output: InputBy
242
220
243
221
fun writeNotNullString (string : String ) {
244
222
writeUnsignedShort((string.length + 1 ).toUShort())
245
- writeByteArray(string.toByteArray() )
223
+ sink.writeUtf8(string )
246
224
}
247
225
248
226
fun readStringOrNull (): String? {
249
- val length = readUnsignedShort().toInt ()
250
- if (length == 0 ) return null
251
- return readByteArray (length - 1 ).toString( Charsets . UTF_8 )
227
+ val length = readUnsignedShort().toLong ()
228
+ if (length == 0L ) return null
229
+ return source.readUtf8 (length - 1 )
252
230
}
253
231
254
232
fun readNotNullString (): String = readStringOrNull()!!
255
- }
233
+ }
0 commit comments