MemoryDataStore: More efficient overridable growth policy

This commit is contained in:
Lanchon 2017-09-24 21:21:43 -03:00 committed by Ben Gruver
parent b0a69fce34
commit c645b9d546

View File

@ -7,8 +7,8 @@ import java.io.OutputStream;
import java.util.Arrays;
public class MemoryDataStore implements DexDataStore {
protected byte[] buf;
protected int size = 0;
private byte[] buf;
private int size = 0;
public MemoryDataStore() {
this(1024 * 1024);
@ -52,15 +52,21 @@ public class MemoryDataStore implements DexDataStore {
};
}
protected void growBufferIfNeeded(int minSize) {
private void growBufferIfNeeded(int minSize) {
if (minSize > size) {
if (minSize > buf.length) {
buf = Arrays.copyOf(buf, (int)(minSize * 1.2));
int newSize = getNewBufferSize(buf.length, minSize);
if (newSize < minSize) throw new IndexOutOfBoundsException();
buf = Arrays.copyOf(buf, newSize);
}
size = minSize;
}
}
protected int getNewBufferSize(int currentSize, int newMinSize) {
return (int)(newMinSize * 1.2);
}
@Nonnull @Override public InputStream readAt(final int offset) {
return new InputStream() {
private int position = offset;