Commit c213e137 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Fix SimulinkDecoder's read function to comply with the InputStream contract.

When out of bytes, don't return EOF.  Instead, return -1 as specified in the
InputStream contract.  Otherwise MatFileReader is unable to use the decoder,
as expected.
parent be15f0cd
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ public class SimulinkDecoder extends InputStream
                    break;
                }
                case -1: { // We need to explicitly deal with the -1 case, as -1 % 4 in Java is -1.
                    throw new EOFException("No more bytes!");
                    return -1;
                }
                default:
                    throw new RuntimeException("Case " + (pos % 4));
@@ -60,7 +60,7 @@ public class SimulinkDecoder extends InputStream
            return ret;
        } catch (ArrayIndexOutOfBoundsException ex) {
            pos = -1; // Ensure that the exception will be re-thrown, avoiding a buffer overflow.
            throw new EOFException("No more bytes!");
            return -1;
        }
    }

+3 −14
Original line number Diff line number Diff line
@@ -86,22 +86,11 @@ public class SimulinkMatTest

        assertThat(read, is(20144));

        boolean atEnd = false;
        try {
            decoder.read();
        } catch (EOFException ex) {
            atEnd = true;
        }
        assertThat(atEnd, is(true));
        // Verify we are at the end of the stream
        assertThat(decoder.read(), is(-1));

        // Double check that EOF will be re-thrown on each extra call to read.
        atEnd = false;
        try {
            decoder.read();
        } catch (EOFException ex) {
            atEnd = true;
        }
        assertThat(atEnd, is(true));
        assertThat(decoder.read(), is(-1));

        byte[] expectedBuf = new byte[20144];
        SimulinkMatTest.class.getResourceAsStream("/simulink_tet_out.mat").read(expectedBuf);