1
+ package com .esotericsoftware .kryo ;
2
+
3
+ import com .esotericsoftware .kryo .io .ByteBufferInput ;
4
+ import com .esotericsoftware .kryo .io .ByteBufferOutput ;
5
+
6
+ import java .nio .ByteBuffer ;
7
+ import java .nio .ByteOrder ;
8
+
9
+ public class ByteBufferInputOutputTest extends KryoTestCase {
10
+
11
+ public void testByteBufferInputPosition () {
12
+ ByteBuffer byteBuffer = ByteBuffer .allocateDirect (4096 );
13
+ ByteBufferInput inputBuffer = new ByteBufferInput (byteBuffer );
14
+ assertEquals (0 , inputBuffer .position ());
15
+ assertEquals (0 , inputBuffer .getByteBuffer ().position ());
16
+ inputBuffer .setPosition (5 );
17
+ assertEquals (5 , inputBuffer .position ());
18
+ assertEquals (5 , inputBuffer .getByteBuffer ().position ());
19
+ }
20
+
21
+ public void testByteBufferInputLimit () {
22
+ ByteBuffer byteBuffer = ByteBuffer .allocateDirect (4096 );
23
+ ByteBufferInput inputBuffer = new ByteBufferInput (byteBuffer );
24
+ assertEquals (4096 , inputBuffer .limit ());
25
+ assertEquals (4096 , inputBuffer .getByteBuffer ().limit ());
26
+ inputBuffer .setLimit (1000 );
27
+ assertEquals (1000 , inputBuffer .limit ());
28
+ assertEquals (1000 , inputBuffer .getByteBuffer ().limit ());
29
+ }
30
+
31
+ public void testByteBufferInputSetBufferEndianness () {
32
+ ByteBufferInput inputBuffer = new ByteBufferInput ();
33
+ assertEquals (ByteOrder .BIG_ENDIAN , inputBuffer .order ());
34
+
35
+ ByteBuffer byteBuffer = ByteBuffer .allocateDirect (4096 );
36
+ assertEquals (ByteOrder .BIG_ENDIAN , byteBuffer .order ());
37
+ byteBuffer .order (ByteOrder .LITTLE_ENDIAN );
38
+ assertEquals (ByteOrder .LITTLE_ENDIAN , byteBuffer .order ());
39
+
40
+ inputBuffer .setBuffer (byteBuffer );
41
+ assertEquals (byteBuffer .order (), inputBuffer .order ());
42
+ }
43
+
44
+ public void testByteBufferInputSkip () {
45
+ ByteBuffer buffer = ByteBuffer .allocateDirect (4096 );
46
+ ByteBufferInput inputBuffer = new ByteBufferInput (buffer );
47
+ assertEquals (0 , inputBuffer .getByteBuffer ().position ());
48
+ inputBuffer .skip (5 );
49
+ assertEquals (5 , inputBuffer .getByteBuffer ().position ());
50
+ }
51
+
52
+ public void testByteBufferOutputPosition () {
53
+ ByteBufferOutput outputBuffer = new ByteBufferOutput (4096 );
54
+ assertEquals (0 , outputBuffer .position ());
55
+ assertEquals (0 , outputBuffer .getByteBuffer ().position ());
56
+ outputBuffer .setPosition (5 );
57
+ assertEquals (5 , outputBuffer .position ());
58
+ outputBuffer .writeInt (10 );
59
+
60
+ ByteBuffer byteBuffer = outputBuffer .getByteBuffer ().duplicate ();
61
+ byteBuffer .flip ();
62
+
63
+ ByteBufferInput inputBuffer = new ByteBufferInput (byteBuffer );
64
+ inputBuffer .skip (5 );
65
+ assertEquals (5 , byteBuffer .position ());
66
+ assertEquals (10 , inputBuffer .readInt ());
67
+ assertEquals (9 , byteBuffer .position ());
68
+ }
69
+
70
+ public void testByteBufferOutputSetOrder () {
71
+ ByteBufferOutput outputBuffer = new ByteBufferOutput (4096 );
72
+ assertEquals (ByteOrder .BIG_ENDIAN , outputBuffer .order ());
73
+ assertEquals (ByteOrder .BIG_ENDIAN , outputBuffer .getByteBuffer ().order ());
74
+
75
+ outputBuffer .order (ByteOrder .LITTLE_ENDIAN );
76
+ assertEquals (ByteOrder .LITTLE_ENDIAN , outputBuffer .order ());
77
+ assertEquals (ByteOrder .LITTLE_ENDIAN , outputBuffer .getByteBuffer ().order ());
78
+ }
79
+ }
0 commit comments