}
}
void writeTo(final OutputStream os) throws IOException {
final MessageDigest foot = Constants.newMessageDigest();
final DigestOutputStream dos = new DigestOutputStream(os, foot);
boolean extended = false;
for (int i = 0; i < entryCnt; i++)
extended |= sortedEntries[i].isExtended();
// Write the header.
//
final byte[] tmp = new byte[128];
System.arraycopy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.length);
NB.encodeInt32(tmp, 4, extended ? 3 : 2);
NB.encodeInt32(tmp, 8, entryCnt);
dos.write(tmp, 0, 12);
// Write the individual file entries.
//
if (snapshot == null) {
// Write a new index, as no entries require smudging.
//
for (int i = 0; i < entryCnt; i++)
sortedEntries[i].write(dos);
} else {
final int smudge_s = (int) (snapshot.lastModified() / 1000);
final int smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000;
for (int i = 0; i < entryCnt; i++) {
final DirCacheEntry e = sortedEntries[i];
if (e.mightBeRacilyClean(smudge_s, smudge_ns))
e.smudgeRacilyClean();
e.write(dos);
}
}
if (tree != null) {
final TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
tree.write(tmp, bb);
bb.close();
NB.encodeInt32(tmp, 0, EXT_TREE);
NB.encodeInt32(tmp, 4, (int) bb.length());
dos.write(tmp, 0, 8);
bb.writeTo(dos, null);
}
writeIndexChecksum = foot.digest();
os.write(writeIndexChecksum);
os.close();