1
+ /*
2
+ * Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
3
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4
+ */
5
+
6
+ package kotlinx.io.bytestring
7
+
8
+ import org.junit.jupiter.api.Test
9
+ import java.nio.BufferOverflowException
10
+ import java.nio.ByteBuffer
11
+ import java.nio.ReadOnlyBufferException
12
+ import kotlin.test.assertContentEquals
13
+ import kotlin.test.assertEquals
14
+ import kotlin.test.assertFailsWith
15
+ import kotlin.test.assertTrue
16
+
17
+ public class ByteStringByteBufferExtensions {
18
+ @Test
19
+ fun asReadOnlyByteBuffer () {
20
+ val buffer = ByteString (1 , 2 , 3 , 4 ).asReadOnlyByteBuffer()
21
+
22
+ assertTrue(buffer.isReadOnly)
23
+ assertEquals(4 , buffer.remaining())
24
+
25
+ ByteArray (4 ).let {
26
+ buffer.get(it)
27
+ assertContentEquals(byteArrayOf(1 , 2 , 3 , 4 ), it)
28
+ }
29
+ }
30
+
31
+ @Test
32
+ fun getByteString () {
33
+ val bb = ByteBuffer .allocate(8 )
34
+ bb.put(byteArrayOf(1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ))
35
+ bb.flip()
36
+
37
+ assertEquals(ByteString (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ), bb.getByteString())
38
+ bb.flip()
39
+
40
+ assertEquals(ByteString (1 , 2 , 3 , 4 ), bb.getByteString(length = 4 ))
41
+ assertEquals(ByteString (), bb.getByteString(length = 0 ))
42
+ assertFailsWith<IndexOutOfBoundsException > { bb.getByteString(length = - 1 ) }
43
+ val p = bb.position()
44
+ assertFailsWith<IndexOutOfBoundsException > { bb.getByteString(length = 5 ) }
45
+ assertEquals(p, bb.position())
46
+ bb.clear()
47
+
48
+ assertEquals(ByteString (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ), bb.getByteString(at = 0 , length = 8 ))
49
+ assertEquals(0 , bb.position())
50
+
51
+ assertEquals(ByteString (2 , 3 , 4 , 5 ), bb.getByteString(at = 1 , length = 4 ))
52
+ assertEquals(0 , bb.position())
53
+
54
+ assertFailsWith<IndexOutOfBoundsException > { bb.getByteString(at = - 1 , length = 8 ) }
55
+ assertFailsWith<IndexOutOfBoundsException > { bb.getByteString(at = 9 , length = 1 ) }
56
+ assertFailsWith<IndexOutOfBoundsException > { bb.getByteString(at = 7 , length = 2 ) }
57
+ assertFailsWith<IndexOutOfBoundsException > { bb.getByteString(at = 0 , length = - 1 ) }
58
+ }
59
+
60
+ @Test
61
+ fun putString () {
62
+ val bb = ByteBuffer .allocate(8 )
63
+ val string = ByteString (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 )
64
+ val shortString = ByteString (- 1 , - 2 , - 3 )
65
+
66
+ bb.putByteString(string)
67
+ assertEquals(8 , bb.position())
68
+ bb.flip()
69
+ ByteArray (8 ).let {
70
+ bb.get(it)
71
+ assertContentEquals(byteArrayOf(1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ), it)
72
+ }
73
+
74
+ bb.clear()
75
+ bb.position(1 )
76
+ assertFailsWith<BufferOverflowException > { bb.putByteString(string) }
77
+ assertEquals(1 , bb.position())
78
+
79
+ bb.putByteString(at = 0 , string = shortString)
80
+ bb.putByteString(at = 5 , string = shortString)
81
+ assertEquals(1 , bb.position())
82
+ bb.clear()
83
+ ByteArray (8 ).let {
84
+ bb.get(it)
85
+ assertContentEquals(byteArrayOf(- 1 , - 2 , - 3 , 4 , 5 , - 1 , - 2 , - 3 ), it)
86
+ }
87
+
88
+ assertFailsWith<IndexOutOfBoundsException > { bb.putByteString(at = 7 , string = shortString) }
89
+ assertFailsWith<IndexOutOfBoundsException > { bb.putByteString(at = - 1 , string = string) }
90
+ assertFailsWith<IndexOutOfBoundsException > { bb.putByteString(at = 8 , string = string) }
91
+ assertFailsWith<ReadOnlyBufferException > {
92
+ bb.asReadOnlyBuffer().putByteString(string)
93
+ }
94
+ assertFailsWith<ReadOnlyBufferException > {
95
+ bb.asReadOnlyBuffer().putByteString(at = 0 , string = string)
96
+ }
97
+ }
98
+ }
0 commit comments