RangePartHeaderSerializer implements Serializer<RangePartHeader> { public long getPartHeadersLength(final List<Range> ranges, final String boundary, final String contentType, final long totalLength) { if (ranges.size() < 2) { return 0; } String partHeader = serialize(new RangePartHeader(new Range(0L, 0L), boundary, contentType, 0L)); int partHeaderWithoutDigits = partHeader.length() - RANGE_DIGITS_LENGTH; long partHeadersLength = 0; partHeadersLength += NEW_LINE.length(); for (Range range : ranges) { partHeadersLength += NEW_LINE.length() + partHeaderWithoutDigits + Long.toString(range.getFrom()).length() + Long.toString(range.getTo()).length() + Long.toString(totalLength).length(); } partHeadersLength += serializeLastBoundaryDeliminator(boundary).length(); return partHeadersLength; }  @Override String serialize(final RangePartHeader input); String serializeLastBoundaryDeliminator(final String boundary); long getPartHeadersLength(final List<Range> ranges,
                                     final String boundary,
                                     final String contentType,
                                     final long totalLength);  }
@Test public void shouldReturnLengthForASingeRange() { Range range = new Range(333L, 1234L); String boundary = "BBBOOUUNNNDDAARRYY"; String contentType = "application/pdf"; long totalLength = 12345L; long computedLength = rangePartHeaderSerializer .getPartHeadersLength(Collections.singletonList(range), boundary, contentType, totalLength); assertThat(computedLength, is(0L)); }