Source:
<code>public static <T> GenericIndexed<T> fromIterable(Iterable<T> objectsIterable, ObjectStrategy<T> strategy) { Iterator<T> objects = objectsIterable.iterator(); if (!objects.hasNext()) { final ByteBuffer buffer = ByteBuffer.allocate(4).putInt(0); buffer.flip(); return new GenericIndexed<T>(buffer, strategy, true); } boolean allowReverseLookup = true; int count = 0; ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(); ByteArrayOutputStream valueBytes = new ByteArrayOutputStream(); try { headerBytes.write(Ints.toByteArray(0)); int offset = 0; T prevVal = null; do { count++; T next = objects.next(); if (allowReverseLookup && prevVal != null && !(strategy.compare(prevVal, next) < 0)) { allowReverseLookup = false; } final byte[] bytes = strategy.toBytes(next); offset += 4 + bytes.length; headerBytes.write(Ints.toByteArray(offset)); valueBytes.write(Ints.toByteArray(bytes.length)); valueBytes.write(bytes); if (prevVal instanceof Closeable) { CloseQuietly.close((Closeable) prevVal); } prevVal = next; } while (objects.hasNext()); if (prevVal instanceof Closeable) { CloseQuietly.close((Closeable) prevVal); } } catch (IOException e) { throw new RuntimeException(e); } byte[] header = headerBytes.toByteArray(); byte[] values = valueBytes.toByteArray(); System.arraycopy(Ints.toByteArray(count), 0, header, 0, 4); ByteBuffer theBuffer = ByteBuffer.allocate(header.length + values.length); <START> theBuffer.put(header); <END> theBuffer.put(values); theBuffer.flip(); return new GenericIndexed<T>(theBuffer.asReadOnlyBuffer(), strategy, allowReverseLookup); }</code><technical_language>Instead of overwriting 4 bytes in headerBytes fact, add a theBuffer.putInt(count) this line bother count part of headerBytes</technical_language>

Target:
public static <T> GenericIndexed<T> fromIterable(Iterable<T> objectsIterable, ObjectStrategy<T> strategy) { Iterator<T> objects = objectsIterable.iterator(); if (!objects.hasNext()) { final ByteBuffer buffer = ByteBuffer.allocate(4).putInt(0); buffer.flip(); return new GenericIndexed<T>(buffer, strategy, true); } boolean allowReverseLookup = true; int count = 0; ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(); ByteArrayOutputStream valueBytes = new ByteArrayOutputStream(); try { int offset = 0; T prevVal = null; do { count++; T next = objects.next(); if (allowReverseLookup && prevVal != null && !(strategy.compare(prevVal, next) < 0)) { allowReverseLookup = false; } final byte[] bytes = strategy.toBytes(next); offset += 4 + bytes.length; headerBytes.write(Ints.toByteArray(offset)); valueBytes.write(Ints.toByteArray(bytes.length)); valueBytes.write(bytes); if (prevVal instanceof Closeable) { CloseQuietly.close((Closeable) prevVal); } prevVal = next; } while (objects.hasNext()); if (prevVal instanceof Closeable) { CloseQuietly.close((Closeable) prevVal); } } catch (IOException e) { throw new RuntimeException(e); } ByteBuffer theBuffer = ByteBuffer.allocate(Ints.BYTES + headerBytes.size() + valueBytes.size()); theBuffer.put(Ints.toByteArray(count)); theBuffer.put(headerBytes.toByteArray()); theBuffer.put(valueBytes.toByteArray()); theBuffer.flip(); return new GenericIndexed<T>(theBuffer.asReadOnlyBuffer(), strategy, allowReverseLookup); }