/** * Returns the number of bytes required to hold the Extra Fields. * * @return The length of the Extra Fields in bytes. * May be {@code 0}. * @see #getExtra */ int getExtraLength() { final Map<Integer, ExtraField> extra = this.extra; if (extra.isEmpty()) return 0; int l = 0; for (ExtraField ef : extra.values()) l += 4 + ef.getDataSize(); return l; }
/** * Returns a protective copy of the data block. * * @see #getDataSize */ final byte[] getDataBlock() { final int size = getDataSize(); assert UShort.check(size); if (0 == size) return EMPTY; final byte[] data = new byte[size]; writeTo(data, 0); return data; }
/** * Serializes a list of Extra Fields of {@link #getExtraLength} bytes to the * byte array {@code data} at the zero based offset {@code off}. * Upon return, this collection shall not access {@code data} * subsequently. * * @param data The byte array to write the list of Extra Fields to. * @param off The zero based offset in the byte array where the first byte * of the list of Extra Fields is written to. * @throws IndexOutOfBoundsException If the byte array * {@code data} does not hold at least {@link #getExtraLength} * bytes at the zero based offset {@code off}. * @see #getExtraLength */ void writeTo(final byte[] data, int off) throws IndexOutOfBoundsException { for (final ExtraField ef : extra.values()) { writeShort(ef.getHeaderId(), data, off); off += 2; writeShort(ef.getDataSize(), data, off); off += 2; ef.writeTo(data, off); off += ef.getDataSize(); } } }
@Test public void testCollection1() { assertEquals(0, fields.getExtra().length); final ExtraField ef = new DefaultExtraField(ExtraField.ZIP64_HEADER_ID); assertNull(fields.get(ExtraField.ZIP64_HEADER_ID)); assertNull(fields.add(ef)); byte[] got = fields.getExtra(); assertEquals(4 + ef.getDataSize(), got.length); assertSame(ef, fields.remove(ExtraField.ZIP64_HEADER_ID)); assertEquals(0, fields.getExtra().length); } }