@Test public void readMultiByteLengthWithMaxBytes() throws IOException { expectedException.expect(CodecException.class); expectedException.expectMessage("This method only supports arrays up to length 4!"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(-1); for (int i = 0; i < 127; i++) { outputStream.write(-1); } ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); OerLengthSerializer.readLength(inputStream); }
public static int readLength(final InputStream inputStream) throws IOException { Objects.requireNonNull(inputStream); final int length; int initialLengthPrefixOctet = inputStream.read(); if (initialLengthPrefixOctet >= 0 && initialLengthPrefixOctet < 128) { length = initialLengthPrefixOctet; } else { final int lengthOfLength = initialLengthPrefixOctet & 0x7F; byte[] ba = new byte[lengthOfLength]; int read = inputStream.read(ba, 0, lengthOfLength); if (read != lengthOfLength) { throw new IOException("Unable to read " + lengthOfLength + " bytes from stream, only read " + read); } length = toInt(ba); } return length > 0 ? length : 0; }
OerLengthSerializer { public static int readLength(final InputStream inputStream) throws IOException { Objects.requireNonNull(inputStream); final int length; int initialLengthPrefixOctet = inputStream.read(); if (initialLengthPrefixOctet >= 0 && initialLengthPrefixOctet < 128) { length = initialLengthPrefixOctet; } else { final int lengthOfLength = initialLengthPrefixOctet & 0x7F; byte[] ba = new byte[lengthOfLength]; int read = inputStream.read(ba, 0, lengthOfLength); if (read != lengthOfLength) { throw new IOException("Unable to read " + lengthOfLength + " bytes from stream, only read " + read); } length = toInt(ba); } return length > 0 ? length : 0; } }
OerLengthSerializer { public static int readLength(final InputStream inputStream) throws IOException { Objects.requireNonNull(inputStream); final int length; int initialLengthPrefixOctet = inputStream.read(); if (initialLengthPrefixOctet >= 0 && initialLengthPrefixOctet < 128) { length = initialLengthPrefixOctet; } else { final int lengthOfLength = initialLengthPrefixOctet & 0x7F; byte[] ba = new byte[lengthOfLength]; int read = inputStream.read(ba, 0, lengthOfLength); if (read != lengthOfLength) { throw new IOException("Unable to read " + lengthOfLength + " bytes from stream, only read " + read); } length = toInt(ba); } return length > 0 ? length : 0; }  }
OerLengthSerializer { public static int readLength(final InputStream inputStream) throws IOException { Objects.requireNonNull(inputStream); final int length; int initialLengthPrefixOctet = inputStream.read(); if (initialLengthPrefixOctet >= 0 && initialLengthPrefixOctet < 128) { length = initialLengthPrefixOctet; } else { final int lengthOfLength = initialLengthPrefixOctet & 0x7F; byte[] ba = new byte[lengthOfLength]; int read = inputStream.read(ba, 0, lengthOfLength); if (read != lengthOfLength) { throw new IOException("Unable to read " + lengthOfLength + " bytes from stream, only read " + read); } length = toInt(ba); } return length > 0 ? length : 0; }  static int readLength(final InputStream inputStream); static void writeLength(final int length, final OutputStream outputStream); }
OerLengthSerializer { public static int readLength(final InputStream inputStream) throws IOException { Objects.requireNonNull(inputStream); final int length; int initialLengthPrefixOctet = inputStream.read(); if (initialLengthPrefixOctet >= 0 && initialLengthPrefixOctet < 128) { length = initialLengthPrefixOctet; } else { final int lengthOfLength = initialLengthPrefixOctet & 0x7F; byte[] ba = new byte[lengthOfLength]; int read = inputStream.read(ba, 0, lengthOfLength); if (read != lengthOfLength) { throw new IOException("Unable to read " + lengthOfLength + " bytes from stream, only read " + read); } length = toInt(ba); } return length > 0 ? length : 0; }  static int readLength(final InputStream inputStream); static void writeLength(final int length, final OutputStream outputStream);  }