提交 1cab2114 authored 作者: Thomas Mueller's avatar Thomas Mueller

Use 16 KB less memory.

上级 5de35fa8
...@@ -87,13 +87,16 @@ public class SecureFileStore extends FileStore { ...@@ -87,13 +87,16 @@ public class SecureFileStore extends FileStore {
long length = length(); long length = length();
if (newLength > length) { if (newLength > length) {
seek(length); seek(length);
byte[] empty = EMPTY; if (empty == null) {
empty = new byte[16 * 1024];
}
byte[] e = empty;
while (true) { while (true) {
int p = (int) Math.min(newLength - length, EMPTY.length); int p = (int) Math.min(newLength - length, e.length);
if (p <= 0) { if (p <= 0) {
break; break;
} }
write(empty, 0, p); write(e, 0, p);
length += p; length += p;
} }
seek(oldPos); seek(oldPos);
......
...@@ -34,7 +34,7 @@ public class FileStore { ...@@ -34,7 +34,7 @@ public class FileStore {
* An empty buffer to speed up extending the file (it seems that writing 0 * An empty buffer to speed up extending the file (it seems that writing 0
* bytes is faster then calling setLength). * bytes is faster then calling setLength).
*/ */
protected static final byte[] EMPTY = new byte[16 * 1024]; protected static byte[] empty;
/** /**
* The magic file header. * The magic file header.
...@@ -356,13 +356,16 @@ public class FileStore { ...@@ -356,13 +356,16 @@ public class FileStore {
private void extendByWriting(long newLength) throws IOException { private void extendByWriting(long newLength) throws IOException {
long pos = filePos; long pos = filePos;
file.seek(fileLength); file.seek(fileLength);
byte[] empty = EMPTY; if (empty == null) {
empty = new byte[16 * 1024];
}
byte[] e = empty;
while (true) { while (true) {
int p = (int) Math.min(newLength - fileLength, EMPTY.length); int p = (int) Math.min(newLength - fileLength, e.length);
if (p <= 0) { if (p <= 0) {
break; break;
} }
file.write(empty, 0, p); file.write(e, 0, p);
fileLength += p; fileLength += p;
} }
file.seek(pos); file.seek(pos);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论