### Question:
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);  }### Answer:
@Test public void read() throws Exception { final ByteArrayInputStream inputStream = new ByteArrayInputStream(this.asn1OerBytes); final int actualPayloadLength = OerLengthSerializer.readLength(inputStream); assertThat(actualPayloadLength).isEqualTo(expectedPayloadLength); }
@Test public void readLengthWithCorrectPrefixButNegativeNumber() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(3); for (int i = 0; i < 3; i++) { outputStream.write(-1); } ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); assertThat(OerLengthSerializer.readLength(inputStream)).isEqualTo(3); }
@Test public void readMultiByteLengthWithNegativeValue() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(-127); outputStream.write(0x80); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); assertThat(OerLengthSerializer.readLength(inputStream)).isEqualTo(128); }
@Test public void readMultiByteLengthWith1Byte() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(-127); outputStream.write(2); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); assertThat(OerLengthSerializer.readLength(inputStream)).isEqualTo(2); }
@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); }