Parser { protected byte[] parseByteArrayField(int length) { if (length == 0) { return new byte[0]; } if (length < 0) { throw new ParserException("Cannot parse field of size " + length); } int nextPointer = pointer + length; if (!enoughBytesLeft(length)) { throw new ParserException("Parsing over the end of the array. Current Pointer:" + pointer + " ToParse Length:" + length + " ArrayLength:" + array.length); } byte[] result = Arrays.copyOfRange(array, pointer, nextPointer); pointer = nextPointer; return result; } Parser(int startposition, byte[] array); int getPointer(); void setPointer(int pointer); int getStartPoint(); abstract T parse();  }
@Test(expected = ParserException.class) public void testParseByteFieldNegative() { parser.parseByteArrayField(-123); }