From fefdceb257bf4264a0461151e13f09561b15b8d1 Mon Sep 17 00:00:00 2001 From: msx80 Date: Wed, 29 Dec 2021 21:40:04 +0100 Subject: [PATCH 1/4] More specific exceptions for buffer over/underflow --- .../kryo/io/ByteBufferInput.java | 5 ++- .../kryo/io/ByteBufferOutput.java | 5 ++- src/com/esotericsoftware/kryo/io/Input.java | 7 ++-- .../kryo/io/KryoBufferOverflowException.java | 40 ++++++++++++++++++ .../kryo/io/KryoBufferUnderflowException.java | 40 ++++++++++++++++++ .../kryo/io/KryoIOException.java | 42 +++++++++++++++++++ src/com/esotericsoftware/kryo/io/Output.java | 5 ++- .../kryo/io/ByteBufferInputOutputTest.java | 34 +++++++++++++++ .../kryo/io/InputOutputTest.java | 33 +++++++++++++++ 9 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java create mode 100644 src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java create mode 100644 src/com/esotericsoftware/kryo/io/KryoIOException.java diff --git a/src/com/esotericsoftware/kryo/io/ByteBufferInput.java b/src/com/esotericsoftware/kryo/io/ByteBufferInput.java index 1152a3cde..18e292896 100644 --- a/src/com/esotericsoftware/kryo/io/ByteBufferInput.java +++ b/src/com/esotericsoftware/kryo/io/ByteBufferInput.java @@ -20,6 +20,7 @@ package com.esotericsoftware.kryo.io; import com.esotericsoftware.kryo.KryoException; +import com.esotericsoftware.kryo.io.KryoBufferUnderflowException; import com.esotericsoftware.kryo.util.Util; import java.io.IOException; @@ -169,7 +170,7 @@ protected int require (int required) throws KryoException { // Try to fill the buffer. if (remaining > 0) { count = fill(byteBuffer, limit, capacity - limit); - if (count == -1) throw new KryoException("Buffer underflow."); + if (count == -1) throw new KryoBufferUnderflowException("Buffer underflow."); setBufferPosition(byteBuffer, position); remaining += count; if (remaining >= required) { @@ -188,7 +189,7 @@ protected int require (int required) throws KryoException { count = fill(byteBuffer, remaining, capacity - remaining); if (count == -1) { if (remaining >= required) break; - throw new KryoException("Buffer underflow."); + throw new KryoBufferUnderflowException("Buffer underflow."); } remaining += count; if (remaining >= required) break; // Enough has been read. diff --git a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java index c3cb14769..b5045adf8 100644 --- a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java +++ b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java @@ -20,6 +20,7 @@ package com.esotericsoftware.kryo.io; import com.esotericsoftware.kryo.KryoException; +import com.esotericsoftware.kryo.io.KryoBufferOverflowException; import com.esotericsoftware.kryo.util.Util; import java.io.IOException; @@ -184,8 +185,8 @@ protected boolean require (int required) throws KryoException { if (capacity - position >= required) return true; if (required > maxCapacity - position) { if (required > maxCapacity) - throw new KryoException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); - throw new KryoException("Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); + throw new KryoBufferOverflowException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); + throw new KryoBufferOverflowException("Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); } if (capacity == 0) capacity = 16; do { diff --git a/src/com/esotericsoftware/kryo/io/Input.java b/src/com/esotericsoftware/kryo/io/Input.java index ce29ceb5d..92083a7d9 100644 --- a/src/com/esotericsoftware/kryo/io/Input.java +++ b/src/com/esotericsoftware/kryo/io/Input.java @@ -20,6 +20,7 @@ package com.esotericsoftware.kryo.io; import com.esotericsoftware.kryo.KryoException; +import com.esotericsoftware.kryo.io.KryoBufferUnderflowException; import com.esotericsoftware.kryo.util.Pool.Poolable; import com.esotericsoftware.kryo.util.Util; @@ -189,7 +190,7 @@ protected int fill (byte[] buffer, int offset, int count) throws KryoException { /** Fills the buffer with at least the number of bytes specified. * @param required Must be > 0. * @return The number of bytes remaining in the buffer, which will be at least required bytes. - * @throws KryoException if {@link #fill(byte[], int, int)} is unable to provide more bytes (buffer underflow). */ + * @throws KryoBufferUnderflowException if {@link #fill(byte[], int, int)} is unable to provide more bytes (buffer underflow). */ protected int require (int required) throws KryoException { int remaining = limit - position; if (remaining >= required) return remaining; @@ -199,7 +200,7 @@ protected int require (int required) throws KryoException { // Try to fill the buffer. if (remaining > 0) { count = fill(buffer, limit, capacity - limit); - if (count == -1) throw new KryoException("Buffer underflow."); + if (count == -1) throw new KryoBufferUnderflowException("Buffer underflow."); remaining += count; if (remaining >= required) { limit += count; @@ -216,7 +217,7 @@ protected int require (int required) throws KryoException { count = fill(buffer, remaining, capacity - remaining); if (count == -1) { if (remaining >= required) break; - throw new KryoException("Buffer underflow."); + throw new KryoBufferUnderflowException("Buffer underflow."); } remaining += count; if (remaining >= required) break; // Enough has been read. diff --git a/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java b/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java new file mode 100644 index 000000000..d7a8e1cf9 --- /dev/null +++ b/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java @@ -0,0 +1,40 @@ +/* Copyright (c) 2008-2020, Nathan Sweet + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the distribution. + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +package com.esotericsoftware.kryo.io; + +public class KryoBufferOverflowException extends KryoIOException { + private StringBuffer trace; + + public KryoBufferOverflowException () { + super(); + } + + public KryoBufferOverflowException (String message, Throwable cause) { + super(message, cause); + } + + public KryoBufferOverflowException (String message) { + super(message); + } + + public KryoBufferOverflowException (Throwable cause) { + super(cause); + } +} diff --git a/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java b/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java new file mode 100644 index 000000000..e336434a2 --- /dev/null +++ b/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java @@ -0,0 +1,40 @@ +/* Copyright (c) 2008-2020, Nathan Sweet + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the distribution. + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +package com.esotericsoftware.kryo.io; + +public class KryoBufferUnderflowException extends KryoIOException { + private StringBuffer trace; + + public KryoBufferUnderflowException () { + super(); + } + + public KryoBufferUnderflowException (String message, Throwable cause) { + super(message, cause); + } + + public KryoBufferUnderflowException (String message) { + super(message); + } + + public KryoBufferUnderflowException (Throwable cause) { + super(cause); + } +} diff --git a/src/com/esotericsoftware/kryo/io/KryoIOException.java b/src/com/esotericsoftware/kryo/io/KryoIOException.java new file mode 100644 index 000000000..8006c1fb9 --- /dev/null +++ b/src/com/esotericsoftware/kryo/io/KryoIOException.java @@ -0,0 +1,42 @@ +/* Copyright (c) 2008-2020, Nathan Sweet + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the distribution. + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +package com.esotericsoftware.kryo.io; + +import com.esotericsoftware.kryo.KryoException; + +public class KryoIOException extends KryoException { + private StringBuffer trace; + + public KryoIOException () { + super(); + } + + public KryoIOException (String message, Throwable cause) { + super(message, cause); + } + + public KryoIOException (String message) { + super(message); + } + + public KryoIOException (Throwable cause) { + super(cause); + } +} diff --git a/src/com/esotericsoftware/kryo/io/Output.java b/src/com/esotericsoftware/kryo/io/Output.java index 6a8aa1289..9f9529748 100644 --- a/src/com/esotericsoftware/kryo/io/Output.java +++ b/src/com/esotericsoftware/kryo/io/Output.java @@ -20,6 +20,7 @@ package com.esotericsoftware.kryo.io; import com.esotericsoftware.kryo.KryoException; +import com.esotericsoftware.kryo.io.KryoBufferOverflowException; import com.esotericsoftware.kryo.util.Pool.Poolable; import com.esotericsoftware.kryo.util.Util; @@ -182,8 +183,8 @@ protected boolean require (int required) throws KryoException { if (capacity - position >= required) return true; if (required > maxCapacity - position) { if (required > maxCapacity) - throw new KryoException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); - throw new KryoException("Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); + throw new KryoBufferOverflowException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); + throw new KryoBufferOverflowException("Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); } if (capacity == 0) capacity = 16; do { diff --git a/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java b/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java index c1431b296..695c3aec3 100644 --- a/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java +++ b/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java @@ -22,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.*; import com.esotericsoftware.kryo.KryoTestCase; +import com.esotericsoftware.kryo.io.KryoBufferOverflowException; +import com.esotericsoftware.kryo.io.KryoBufferUnderflowException; import java.io.ByteArrayInputStream; import java.nio.Buffer; @@ -87,4 +89,36 @@ void testByteBufferOutputPosition () { assertEquals(10, inputBuffer.readInt()); assertEquals(9, byteBuffer.position()); } + + @Test + void testOverflow () { + ByteBufferOutput buffer = new ByteBufferOutput(1); + buffer.writeByte(51); + try + { + buffer.writeByte(65); + + fail("exception expected but none thrown"); + } + catch(KryoBufferOverflowException e) + { + assertTrue(e.getMessage().startsWith("Buffer overflow")); + } + } + + @Test + void testUnderflow () { + ByteBufferInput buffer = new ByteBufferInput(1); + try + { + buffer.readBytes(2); + + fail("exception expected but none thrown"); + } + catch(KryoBufferUnderflowException e) + { + assertTrue(e.getMessage().equals("Buffer underflow.")); + } + } + } diff --git a/test/com/esotericsoftware/kryo/io/InputOutputTest.java b/test/com/esotericsoftware/kryo/io/InputOutputTest.java index f064d6db3..ca5b10369 100644 --- a/test/com/esotericsoftware/kryo/io/InputOutputTest.java +++ b/test/com/esotericsoftware/kryo/io/InputOutputTest.java @@ -25,6 +25,8 @@ import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.KryoTestCase; import com.esotericsoftware.kryo.Registration; +import com.esotericsoftware.kryo.io.KryoBufferUnderflowException; +import com.esotericsoftware.kryo.io.KryoBufferOverflowException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -110,6 +112,37 @@ void testWriteBytes () throws IOException { 61, 62, 63, 64, 65}, buffer.toBytes()); } + @Test + void testOverflow () throws IOException { + Output buffer = new Output(1); + buffer.writeByte(51); + try + { + buffer.writeByte(65); + + fail("exception expected but none thrown"); + } + catch(KryoBufferOverflowException e) + { + assertTrue(e.getMessage().startsWith("Buffer overflow")); + } + } + + @Test + void testUnderflow () throws IOException { + Input buffer = new Input(1); + try + { + buffer.readBytes(2); + + fail("exception expected but none thrown"); + } + catch(KryoBufferUnderflowException e) + { + assertTrue(e.getMessage().equals("Buffer underflow.")); + } + } + @Test void testStrings () throws IOException { runStringTest(new Output(4096)); From ca14b3ed2e59cdc576eb01e0eabcb7e8eea2a202 Mon Sep 17 00:00:00 2001 From: msx80 Date: Wed, 29 Dec 2021 21:43:36 +0100 Subject: [PATCH 2/4] cleaning leftovers --- .../esotericsoftware/kryo/io/KryoBufferOverflowException.java | 1 - .../esotericsoftware/kryo/io/KryoBufferUnderflowException.java | 1 - src/com/esotericsoftware/kryo/io/KryoIOException.java | 1 - 3 files changed, 3 deletions(-) diff --git a/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java b/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java index d7a8e1cf9..042dfa0ab 100644 --- a/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java +++ b/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java @@ -20,7 +20,6 @@ package com.esotericsoftware.kryo.io; public class KryoBufferOverflowException extends KryoIOException { - private StringBuffer trace; public KryoBufferOverflowException () { super(); diff --git a/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java b/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java index e336434a2..3f01b0305 100644 --- a/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java +++ b/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java @@ -20,7 +20,6 @@ package com.esotericsoftware.kryo.io; public class KryoBufferUnderflowException extends KryoIOException { - private StringBuffer trace; public KryoBufferUnderflowException () { super(); diff --git a/src/com/esotericsoftware/kryo/io/KryoIOException.java b/src/com/esotericsoftware/kryo/io/KryoIOException.java index 8006c1fb9..dbd665731 100644 --- a/src/com/esotericsoftware/kryo/io/KryoIOException.java +++ b/src/com/esotericsoftware/kryo/io/KryoIOException.java @@ -22,7 +22,6 @@ import com.esotericsoftware.kryo.KryoException; public class KryoIOException extends KryoException { - private StringBuffer trace; public KryoIOException () { super(); From 707416753fc9407ca06f0acc9e682b2fe66fd354 Mon Sep 17 00:00:00 2001 From: msx80 Date: Mon, 3 Jan 2022 16:19:49 +0100 Subject: [PATCH 3/4] formatted code and junit5 style exception tests --- src/com/esotericsoftware/kryo/Kryo.java | 2 +- .../kryo/io/ByteBufferOutput.java | 3 +- src/com/esotericsoftware/kryo/io/Input.java | 3 +- src/com/esotericsoftware/kryo/io/Output.java | 3 +- .../kryo/io/ByteBufferInputOutputTest.java | 35 ++++++++---------- .../kryo/io/InputOutputTest.java | 36 +++++++++---------- 6 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/com/esotericsoftware/kryo/Kryo.java b/src/com/esotericsoftware/kryo/Kryo.java index 8c8264540..8e20325f9 100644 --- a/src/com/esotericsoftware/kryo/Kryo.java +++ b/src/com/esotericsoftware/kryo/Kryo.java @@ -287,7 +287,7 @@ public void addDefaultSerializer (Class type, SerializerFactory serializerFactor /** Instances with the specified class name will use the specified serializer when {@link #register(Class)} or * {@link #register(Class, int)} are called. * @see #setDefaultSerializer(Class) */ - private void addDefaultSerializer(String className, Class serializer) { + private void addDefaultSerializer (String className, Class serializer) { try { addDefaultSerializer(Class.forName(className), serializer); } catch (ClassNotFoundException e) { diff --git a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java index b5045adf8..2d7006fd1 100644 --- a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java +++ b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java @@ -186,7 +186,8 @@ protected boolean require (int required) throws KryoException { if (required > maxCapacity - position) { if (required > maxCapacity) throw new KryoBufferOverflowException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); - throw new KryoBufferOverflowException("Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); + throw new KryoBufferOverflowException( + "Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); } if (capacity == 0) capacity = 16; do { diff --git a/src/com/esotericsoftware/kryo/io/Input.java b/src/com/esotericsoftware/kryo/io/Input.java index 92083a7d9..b674005fd 100644 --- a/src/com/esotericsoftware/kryo/io/Input.java +++ b/src/com/esotericsoftware/kryo/io/Input.java @@ -190,7 +190,8 @@ protected int fill (byte[] buffer, int offset, int count) throws KryoException { /** Fills the buffer with at least the number of bytes specified. * @param required Must be > 0. * @return The number of bytes remaining in the buffer, which will be at least required bytes. - * @throws KryoBufferUnderflowException if {@link #fill(byte[], int, int)} is unable to provide more bytes (buffer underflow). */ + * @throws KryoBufferUnderflowException if {@link #fill(byte[], int, int)} is unable to provide more bytes (buffer + * underflow). */ protected int require (int required) throws KryoException { int remaining = limit - position; if (remaining >= required) return remaining; diff --git a/src/com/esotericsoftware/kryo/io/Output.java b/src/com/esotericsoftware/kryo/io/Output.java index 9f9529748..04be2b3fe 100644 --- a/src/com/esotericsoftware/kryo/io/Output.java +++ b/src/com/esotericsoftware/kryo/io/Output.java @@ -184,7 +184,8 @@ protected boolean require (int required) throws KryoException { if (required > maxCapacity - position) { if (required > maxCapacity) throw new KryoBufferOverflowException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); - throw new KryoBufferOverflowException("Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); + throw new KryoBufferOverflowException( + "Buffer overflow. Available: " + (maxCapacity - position) + ", required: " + required); } if (capacity == 0) capacity = 16; do { diff --git a/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java b/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java index 695c3aec3..dc66e40c7 100644 --- a/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java +++ b/test/com/esotericsoftware/kryo/io/ByteBufferInputOutputTest.java @@ -94,31 +94,26 @@ void testByteBufferOutputPosition () { void testOverflow () { ByteBufferOutput buffer = new ByteBufferOutput(1); buffer.writeByte(51); - try - { - buffer.writeByte(65); - - fail("exception expected but none thrown"); - } - catch(KryoBufferOverflowException e) - { - assertTrue(e.getMessage().startsWith("Buffer overflow")); - } + KryoBufferOverflowException thrown = assertThrows( + KryoBufferOverflowException.class, + () -> buffer.writeByte(65), + "Exception expected but none thrown" + ); + + assertTrue(thrown.getMessage().startsWith("Buffer overflow")); } @Test void testUnderflow () { ByteBufferInput buffer = new ByteBufferInput(1); - try - { - buffer.readBytes(2); - - fail("exception expected but none thrown"); - } - catch(KryoBufferUnderflowException e) - { - assertTrue(e.getMessage().equals("Buffer underflow.")); - } + + KryoBufferUnderflowException thrown = assertThrows( + KryoBufferUnderflowException.class, + () -> buffer.readBytes(2), + "Exception expected but none thrown" + ); + + assertTrue(thrown.getMessage().equals("Buffer underflow.")); } } diff --git a/test/com/esotericsoftware/kryo/io/InputOutputTest.java b/test/com/esotericsoftware/kryo/io/InputOutputTest.java index ca5b10369..25c357ce0 100644 --- a/test/com/esotericsoftware/kryo/io/InputOutputTest.java +++ b/test/com/esotericsoftware/kryo/io/InputOutputTest.java @@ -116,31 +116,27 @@ void testWriteBytes () throws IOException { void testOverflow () throws IOException { Output buffer = new Output(1); buffer.writeByte(51); - try - { - buffer.writeByte(65); - - fail("exception expected but none thrown"); - } - catch(KryoBufferOverflowException e) - { - assertTrue(e.getMessage().startsWith("Buffer overflow")); - } + + KryoBufferOverflowException thrown = assertThrows( + KryoBufferOverflowException.class, + () -> buffer.writeByte(65), + "Exception expected but none thrown" + ); + + assertTrue(thrown.getMessage().startsWith("Buffer overflow")); } @Test void testUnderflow () throws IOException { Input buffer = new Input(1); - try - { - buffer.readBytes(2); - - fail("exception expected but none thrown"); - } - catch(KryoBufferUnderflowException e) - { - assertTrue(e.getMessage().equals("Buffer underflow.")); - } + + KryoBufferUnderflowException thrown = assertThrows( + KryoBufferUnderflowException.class, + () -> buffer.readBytes(2), + "Exception expected but none thrown" + ); + + assertTrue(thrown.getMessage().equals("Buffer underflow.")); } @Test From 6f760a2cbe830f6510532bf9525b89d6f9c583c7 Mon Sep 17 00:00:00 2001 From: msx80 Date: Wed, 19 Jan 2022 11:57:51 +0100 Subject: [PATCH 4/4] Removed KryoIOException --- .../kryo/io/KryoBufferOverflowException.java | 4 +- .../kryo/io/KryoBufferUnderflowException.java | 4 +- .../kryo/io/KryoIOException.java | 41 ------------------- 3 files changed, 6 insertions(+), 43 deletions(-) delete mode 100644 src/com/esotericsoftware/kryo/io/KryoIOException.java diff --git a/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java b/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java index 042dfa0ab..f7c9d0569 100644 --- a/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java +++ b/src/com/esotericsoftware/kryo/io/KryoBufferOverflowException.java @@ -19,7 +19,9 @@ package com.esotericsoftware.kryo.io; -public class KryoBufferOverflowException extends KryoIOException { +import com.esotericsoftware.kryo.KryoException; + +public class KryoBufferOverflowException extends KryoException { public KryoBufferOverflowException () { super(); diff --git a/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java b/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java index 3f01b0305..790518b3c 100644 --- a/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java +++ b/src/com/esotericsoftware/kryo/io/KryoBufferUnderflowException.java @@ -19,7 +19,9 @@ package com.esotericsoftware.kryo.io; -public class KryoBufferUnderflowException extends KryoIOException { +import com.esotericsoftware.kryo.KryoException; + +public class KryoBufferUnderflowException extends KryoException { public KryoBufferUnderflowException () { super(); diff --git a/src/com/esotericsoftware/kryo/io/KryoIOException.java b/src/com/esotericsoftware/kryo/io/KryoIOException.java deleted file mode 100644 index dbd665731..000000000 --- a/src/com/esotericsoftware/kryo/io/KryoIOException.java +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright (c) 2008-2020, Nathan Sweet - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the distribution. - * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - -package com.esotericsoftware.kryo.io; - -import com.esotericsoftware.kryo.KryoException; - -public class KryoIOException extends KryoException { - - public KryoIOException () { - super(); - } - - public KryoIOException (String message, Throwable cause) { - super(message, cause); - } - - public KryoIOException (String message) { - super(message); - } - - public KryoIOException (Throwable cause) { - super(cause); - } -}