* i2psnark: File allocation cleanup to use less heap

This commit is contained in:
zzz
2011-09-19 13:46:52 +00:00
parent 47d2b80aa5
commit 23e262b0b9

View File

@ -762,27 +762,19 @@ public class Storage
openRAF(nr, false); // RW openRAF(nr, false); // RW
// XXX - Is this the best way to make sure we have enough space for // XXX - Is this the best way to make sure we have enough space for
// the whole file? // the whole file?
long remaining = lengths[nr];
if (listener != null) if (listener != null)
listener.storageCreateFile(this, names[nr], lengths[nr]); listener.storageCreateFile(this, names[nr], remaining);
final int ZEROBLOCKSIZE = piece_size; final int ZEROBLOCKSIZE = (int) Math.min(remaining, 32*1024);
byte[] zeros; byte[] zeros = new byte[ZEROBLOCKSIZE];
try { while (remaining > 0) {
zeros = new byte[ZEROBLOCKSIZE]; int size = (int) Math.min(remaining, ZEROBLOCKSIZE);
} catch (OutOfMemoryError oom) { rafs[nr].write(zeros, 0, size);
throw new IOException(oom.toString()); remaining -= size;
} }
int i;
for (i = 0; i < lengths[nr]/ZEROBLOCKSIZE; i++)
{
rafs[nr].write(zeros);
if (listener != null)
listener.storageAllocated(this, ZEROBLOCKSIZE);
}
int size = (int)(lengths[nr] - i*ZEROBLOCKSIZE);
rafs[nr].write(zeros, 0, size);
// caller will close rafs[nr] // caller will close rafs[nr]
if (listener != null) if (listener != null)
listener.storageAllocated(this, size); listener.storageAllocated(this, lengths[nr]);
} }