aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2010-07-09 10:10:12 -0700
committerShawn O. Pearce <spearce@spearce.org>2010-07-09 10:37:47 -0700
commit97311cd3e016679178cb37b32f1d913af57e6320 (patch)
tree28b040a0202ae1c06cbb321fc7beb3d55af001a7 /org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java
parent711bd3e3d035b22e41bda32a9463cef3e2dca7a7 (diff)
downloadjgit-97311cd3e016679178cb37b32f1d913af57e6320.tar.gz
jgit-97311cd3e016679178cb37b32f1d913af57e6320.zip
Allow TemporaryBuffer.Heap to allocate smaller than 8 KiB
If the heap limit was set to something smaller than 8 KiB, we were still allocating the full 8 KiB block size, and accepting up to the amount we allocated by. Instead actually put a hard cap on the limit. Change-Id: Id1da26fde2102e76510b1da4ede8493928a981cc Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java33
1 files changed, 25 insertions, 8 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java
index 101b6056bc..baa45c5c61 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java
@@ -126,7 +126,7 @@ public abstract class TemporaryBuffer extends OutputStream {
blocks.add(s);
}
- final int n = Math.min(Block.SZ - s.count, len);
+ final int n = Math.min(s.buffer.length - s.count, len);
System.arraycopy(b, off, s.buffer, s.count, n);
s.count += n;
len -= n;
@@ -171,7 +171,7 @@ public abstract class TemporaryBuffer extends OutputStream {
blocks.add(s);
}
- final int n = in.read(s.buffer, s.count, Block.SZ - s.count);
+ int n = in.read(s.buffer, s.count, s.buffer.length - s.count);
if (n < 1)
return;
s.count += n;
@@ -192,8 +192,12 @@ public abstract class TemporaryBuffer extends OutputStream {
* @return total length of the buffer, in bytes.
*/
public long length() {
+ return inCoreLength();
+ }
+
+ private long inCoreLength() {
final Block last = last();
- return ((long) blocks.size()) * Block.SZ - (Block.SZ - last.count);
+ return ((long) blocks.size() - 1) * Block.SZ + last.count;
}
/**
@@ -251,8 +255,13 @@ public abstract class TemporaryBuffer extends OutputStream {
if (overflow != null) {
destroy();
}
- blocks = new ArrayList<Block>(inCoreLimit / Block.SZ);
- blocks.add(new Block());
+ if (inCoreLimit < Block.SZ) {
+ blocks = new ArrayList<Block>(1);
+ blocks.add(new Block(inCoreLimit));
+ } else {
+ blocks = new ArrayList<Block>(inCoreLimit / Block.SZ);
+ blocks.add(new Block());
+ }
}
/**
@@ -270,7 +279,7 @@ public abstract class TemporaryBuffer extends OutputStream {
}
private boolean reachedInCoreLimit() throws IOException {
- if (blocks.size() * Block.SZ < inCoreLimit)
+ if (inCoreLength() < inCoreLimit)
return false;
switchToOverflow();
@@ -444,12 +453,20 @@ public abstract class TemporaryBuffer extends OutputStream {
static class Block {
static final int SZ = 8 * 1024;
- final byte[] buffer = new byte[SZ];
+ final byte[] buffer;
int count;
+ Block() {
+ buffer = new byte[SZ];
+ }
+
+ Block(int sz) {
+ buffer = new byte[sz];
+ }
+
boolean isFull() {
- return count == SZ;
+ return count == buffer.length;
}
}
}