1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-15 22:05:00 -05:00

* better account for large history files

This commit is contained in:
Reinhard Pointner 2013-11-17 19:07:02 +00:00
parent f4070a4c07
commit 68c89ef01a

View File

@ -1,7 +1,5 @@
package net.sourceforge.tuned; package net.sourceforge.tuned;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -9,24 +7,20 @@ import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
public class ByteBufferOutputStream extends OutputStream { public class ByteBufferOutputStream extends OutputStream {
private ByteBuffer buffer; private ByteBuffer buffer;
private final float loadFactor; private final float loadFactor;
public ByteBufferOutputStream(long initialCapacity) { public ByteBufferOutputStream(long initialCapacity) {
this((int) initialCapacity); this((int) initialCapacity);
} }
public ByteBufferOutputStream(int initialCapacity) { public ByteBufferOutputStream(int initialCapacity) {
this(initialCapacity, 1.0f); this(initialCapacity, 1.0f);
} }
public ByteBufferOutputStream(int initialCapacity, float loadFactor) { public ByteBufferOutputStream(int initialCapacity, float loadFactor) {
if (initialCapacity < 0) if (initialCapacity < 0)
throw new IllegalArgumentException("initialCapacity must not be negative"); throw new IllegalArgumentException("initialCapacity must not be negative");
@ -34,38 +28,33 @@ public class ByteBufferOutputStream extends OutputStream {
if (loadFactor <= 0 || Float.isNaN(loadFactor)) if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("loadFactor must be greater than 0"); throw new IllegalArgumentException("loadFactor must be greater than 0");
this.buffer = ByteBuffer.allocate(initialCapacity); this.buffer = ByteBuffer.allocate(initialCapacity + 1);
this.loadFactor = loadFactor; this.loadFactor = loadFactor;
} }
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
ensureCapacity(buffer.position() + 1); ensureCapacity(buffer.position() + 1);
buffer.put((byte) b); buffer.put((byte) b);
} }
@Override @Override
public void write(byte[] src) throws IOException { public void write(byte[] src) throws IOException {
ensureCapacity(buffer.position() + src.length); ensureCapacity(buffer.position() + src.length);
buffer.put(src); buffer.put(src);
} }
public void write(ByteBuffer src) throws IOException { public void write(ByteBuffer src) throws IOException {
ensureCapacity(buffer.position() + src.remaining()); ensureCapacity(buffer.position() + src.remaining());
buffer.put(src); buffer.put(src);
} }
@Override @Override
public void write(byte[] src, int offset, int length) throws IOException { public void write(byte[] src, int offset, int length) throws IOException {
ensureCapacity(buffer.position() + length); ensureCapacity(buffer.position() + length);
buffer.put(src, offset, length); buffer.put(src, offset, length);
} }
public void ensureCapacity(int minCapacity) { public void ensureCapacity(int minCapacity) {
if (minCapacity <= buffer.capacity()) if (minCapacity <= buffer.capacity())
return; return;
@ -87,7 +76,6 @@ public class ByteBufferOutputStream extends OutputStream {
buffer = newBuffer; buffer = newBuffer;
} }
public ByteBuffer getByteBuffer() { public ByteBuffer getByteBuffer() {
ByteBuffer result = buffer.duplicate(); ByteBuffer result = buffer.duplicate();
@ -97,7 +85,6 @@ public class ByteBufferOutputStream extends OutputStream {
return result; return result;
} }
public byte[] getByteArray() { public byte[] getByteArray() {
ByteBuffer data = getByteBuffer(); ByteBuffer data = getByteBuffer();
@ -108,7 +95,6 @@ public class ByteBufferOutputStream extends OutputStream {
return bytes; return bytes;
} }
public int transferFrom(ReadableByteChannel channel) throws IOException { public int transferFrom(ReadableByteChannel channel) throws IOException {
// make sure buffer is not at its limit // make sure buffer is not at its limit
ensureCapacity(buffer.position() + 1); ensureCapacity(buffer.position() + 1);
@ -116,12 +102,10 @@ public class ByteBufferOutputStream extends OutputStream {
return channel.read(buffer); return channel.read(buffer);
} }
public int transferFully(InputStream inputStream) throws IOException { public int transferFully(InputStream inputStream) throws IOException {
return transferFully(Channels.newChannel(inputStream)); return transferFully(Channels.newChannel(inputStream));
} }
public int transferFully(ReadableByteChannel channel) throws IOException { public int transferFully(ReadableByteChannel channel) throws IOException {
int total = 0, read = 0; int total = 0, read = 0;
@ -132,17 +116,14 @@ public class ByteBufferOutputStream extends OutputStream {
return total; return total;
} }
public int position() { public int position() {
return buffer.position(); return buffer.position();
} }
public int capacity() { public int capacity() {
return buffer.capacity(); return buffer.capacity();
} }
public void rewind() { public void rewind() {
buffer.rewind(); buffer.rewind();
} }