Skip to content

Commit 88f5875

Browse files
committed
Fix positioning and order updates in ByteBufferInput and ByteBufferOutput
- ByteBufferInput setBuffer did not take the byteOrder from ByteBuffer setPosition did not set the position in ByteBuffer skip did not skip bytes in ByteBuffer setLimit did not set limit in ByteBuffer - ByteBufferOutput order did not set order in ByteBuffer setPosition did not set position in ByteBuffer
1 parent 4665a5d commit 88f5875

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

src/com/esotericsoftware/kryo/io/ByteBufferInput.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1616
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1717
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
18-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19-
18+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19+
2020
package com.esotericsoftware.kryo.io;
2121

2222
import com.esotericsoftware.kryo.KryoException;
@@ -109,6 +109,7 @@ public void setBuffer (ByteBuffer buffer) {
109109
position = buffer.position();
110110
limit = buffer.limit();
111111
capacity = buffer.capacity();
112+
byteOrder = buffer.order();
112113
total = 0;
113114
inputStream = null;
114115
}
@@ -295,6 +296,22 @@ public int read (byte[] bytes, int offset, int count) throws KryoException {
295296
return startingCount - count;
296297
}
297298

299+
public void setPosition (int position) {
300+
this.position = position;
301+
this.niobuffer.position(position);
302+
}
303+
304+
/** Sets the limit in the buffer. */
305+
public void setLimit (int limit) {
306+
this.limit = limit;
307+
this.niobuffer.limit(limit);
308+
}
309+
310+
public void skip(int count) throws KryoException {
311+
super.skip(count);
312+
niobuffer.position(this.position());
313+
}
314+
298315
/** Discards the specified number of bytes. */
299316
public long skip (long count) throws KryoException {
300317
long remaining = count;
@@ -957,4 +974,4 @@ public boolean getVarIntsEnabled () {
957974
public void setVarIntsEnabled (boolean varIntsEnabled) {
958975
this.varIntsEnabled = varIntsEnabled;
959976
}
960-
}
977+
}

src/com/esotericsoftware/kryo/io/ByteBufferOutput.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1616
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1717
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
18-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19-
18+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19+
2020
package com.esotericsoftware.kryo.io;
2121

2222
import com.esotericsoftware.kryo.KryoException;
@@ -134,6 +134,7 @@ public ByteOrder order () {
134134

135135
public void order (ByteOrder byteOrder) {
136136
this.byteOrder = byteOrder;
137+
this.niobuffer.order(byteOrder);
137138
}
138139

139140
public OutputStream getOutputStream () {
@@ -186,6 +187,7 @@ public byte[] toBytes () {
186187
/** Sets the current position in the buffer. */
187188
public void setPosition (int position) {
188189
this.position = position;
190+
this.niobuffer.position(position);
189191
}
190192

191193
/** Sets the position and total to zero. */
@@ -937,4 +939,4 @@ public boolean getVarIntsEnabled () {
937939
public void setVarIntsEnabled (boolean varIntsEnabled) {
938940
this.varIntsEnabled = varIntsEnabled;
939941
}
940-
}
942+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)