@Test
public void testFindNalUnitWithPrefix() {

  byte[] data = buildTestData();
  // First byte of NAL unit in data1, rest in data2.
  boolean[] prefixFlags = new boolean[3];
  byte[] data1 = Arrays.copyOfRange(data, 0, TEST_NAL_POSITION + 1);
  byte[] data2 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 1, data.length);
  int result = NalUnitUtil.findNalUnit(data1, 0, data1.length, prefixFlags);
  assertThat(result).isEqualTo(data1.length);
  result = NalUnitUtil.findNalUnit(data2, 0, data2.length, prefixFlags);
  assertThat(result).isEqualTo(-1);
  assertPrefixFlagsCleared(prefixFlags);
  // First three bytes of NAL unit in data1, rest in data2.
  prefixFlags = new boolean[3];
  data1 = Arrays.copyOfRange(data, 0, TEST_NAL_POSITION + 3);
  data2 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 3, data.length);
  result = NalUnitUtil.findNalUnit(data1, 0, data1.length, prefixFlags);
  assertThat(result).isEqualTo(data1.length);
  result = NalUnitUtil.findNalUnit(data2, 0, data2.length, prefixFlags);
  assertThat(result).isEqualTo(-3);
  assertPrefixFlagsCleared(prefixFlags);
  // First byte of NAL unit in data1, second byte in data2, rest in data3.
  prefixFlags = new boolean[3];
  data1 = Arrays.copyOfRange(data, 0, TEST_NAL_POSITION + 1);
  data2 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 1, TEST_NAL_POSITION + 2);
  byte[] data3 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 2, data.length);
  result = NalUnitUtil.findNalUnit(data1, 0, data1.length, prefixFlags);
  assertThat(result).isEqualTo(data1.length);
  result = NalUnitUtil.findNalUnit(data2, 0, data2.length, prefixFlags);
  assertThat(result).isEqualTo(data2.length);
  result = NalUnitUtil.findNalUnit(data3, 0, data3.length, prefixFlags);
  assertThat(result).isEqualTo(-2);
  assertPrefixFlagsCleared(prefixFlags);
  // NAL unit split with one byte in four arrays.
  prefixFlags = new boolean[3];
  data1 = Arrays.copyOfRange(data, 0, TEST_NAL_POSITION + 1);
  data2 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 1, TEST_NAL_POSITION + 2);
  data3 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 2, TEST_NAL_POSITION + 3);
  byte[] data4 = Arrays.copyOfRange(data, TEST_NAL_POSITION + 2, data.length);
  result = NalUnitUtil.findNalUnit(data1, 0, data1.length, prefixFlags);
  assertThat(result).isEqualTo(data1.length);
  result = NalUnitUtil.findNalUnit(data2, 0, data2.length, prefixFlags);
  assertThat(result).isEqualTo(data2.length);
  result = NalUnitUtil.findNalUnit(data3, 0, data3.length, prefixFlags);
  assertThat(result).isEqualTo(data3.length);
  result = NalUnitUtil.findNalUnit(data4, 0, data4.length, prefixFlags);
  assertThat(result).isEqualTo(-3);
  assertPrefixFlagsCleared(prefixFlags);
  // NAL unit entirely in data2. data1 ends with partial prefix.
  prefixFlags = new boolean[3];
  data1 = Arrays.copyOfRange(data, 0, TEST_PARTIAL_NAL_POSITION + 2);
  data2 = Arrays.copyOfRange(data, TEST_PARTIAL_NAL_POSITION + 2, data.length);
  result = NalUnitUtil.findNalUnit(data1, 0, data1.length, prefixFlags);
  assertThat(result).isEqualTo(data1.length);
  result = NalUnitUtil.findNalUnit(data2, 0, data2.length, prefixFlags);
  assertThat(result).isEqualTo(4);
  assertPrefixFlagsCleared(prefixFlags);
}