1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-14 21:35:03 -05:00
filebot/test/net/sourceforge/tuned/ByteBufferOutputStreamTest.java
Reinhard Pointner 74b6a8c1a9 * improved DownloadTask and ByteBufferOutputStream
* added unit test for ByteBufferOutputStream
2008-11-22 15:30:33 +00:00

48 lines
1.1 KiB
Java

package net.sourceforge.tuned;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import org.junit.Test;
public class ByteBufferOutputStreamTest {
@Test
public void growBufferAsNeeded() throws Exception {
// initial buffer size of 1, increase size by a factor of 2 if a bigger buffer is needed
ByteBufferOutputStream buffer = new ByteBufferOutputStream(1, 1.0f);
buffer.write("asdf".getBytes("utf-8"));
// check content
assertEquals("asdf", Charset.forName("utf-8").decode(buffer.getByteBuffer()).toString());
// check capacity
assertEquals(4, buffer.capacity());
}
@Test
public void transferFrom() throws Exception {
InputStream in = new ByteArrayInputStream("asdf".getBytes("utf-8"));
ByteBufferOutputStream buffer = new ByteBufferOutputStream(4);
int n = buffer.transferFrom(Channels.newChannel(in));
// check number of bytes transfered
assertEquals(4, n);
// check content
assertEquals("asdf", Charset.forName("utf-8").decode(buffer.getByteBuffer()).toString());
}
}