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