Search in sources :

Example 11 with StreamByteBuffer

use of org.grails.buffer.StreamByteBuffer in project grails-core by grails.

the class StreamByteBufferTest method bufferTest2.

private void bufferTest2(int streamByteBufferSize, int testBufferSize) throws IOException {
    StreamByteBuffer streamBuf = new StreamByteBuffer(streamByteBufferSize);
    OutputStream output = streamBuf.getOutputStream();
    for (int i = 0; i < testBufferSize; i++) {
        output.write(i % (Byte.MAX_VALUE * 2));
    }
    output.close();
    InputStream input = streamBuf.getInputStream();
    assertEquals(testBufferSize, input.available());
    for (int i = 0; i < testBufferSize; i++) {
        assertEquals((i % (Byte.MAX_VALUE * 2)), input.read());
    }
    assertEquals(-1, input.read());
    assertEquals(-1, input.read());
    assertEquals(-1, input.read());
    assertEquals(-1, input.read());
    input.close();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamByteBuffer(org.grails.buffer.StreamByteBuffer)

Example 12 with StreamByteBuffer

use of org.grails.buffer.StreamByteBuffer in project grails-core by grails.

the class StreamByteBufferTest method testToInputStream.

public void testToInputStream() throws IOException {
    StreamByteBuffer byteBuffer = createTestInstance();
    InputStream input = byteBuffer.getInputStream();
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(byteBuffer.totalBytesUnread());
    copy(input, bytesOut, 2048);
    byte[] result = bytesOut.toByteArray();
    assertTrue(Arrays.equals(testbuffer, result));
}
Also used : InputStream(java.io.InputStream) StreamByteBuffer(org.grails.buffer.StreamByteBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 13 with StreamByteBuffer

use of org.grails.buffer.StreamByteBuffer in project grails-core by grails.

the class StreamByteBufferTest method testToByteArray.

public void testToByteArray() throws IOException {
    StreamByteBuffer byteBuffer = createTestInstance();
    byte[] result = byteBuffer.readAsByteArray();
    assertTrue(Arrays.equals(testbuffer, result));
}
Also used : StreamByteBuffer(org.grails.buffer.StreamByteBuffer)

Example 14 with StreamByteBuffer

use of org.grails.buffer.StreamByteBuffer in project grails-core by grails.

the class StreamByteBufferTest method testToStringRetain.

public void testToStringRetain() throws IOException {
    StreamByteBuffer byteBuffer = new StreamByteBuffer(1024, StreamByteBuffer.ReadMode.RETAIN_AFTER_READING);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(byteBuffer.getOutputStream(), "UTF-8"));
    pw.print(TEST_STRING);
    pw.close();
    assertEquals(TEST_STRING, byteBuffer.readAsString("UTF-8"));
    byteBuffer.reset();
    // call a second time to test if the RETAIN_AFTER_READING mode works as expected
    assertEquals(TEST_STRING, byteBuffer.readAsString("UTF-8"));
}
Also used : StreamByteBuffer(org.grails.buffer.StreamByteBuffer) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

StreamByteBuffer (org.grails.buffer.StreamByteBuffer)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 IOException (java.io.IOException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 FastStringWriter (org.grails.buffer.FastStringWriter)1 StreamCharBuffer (org.grails.buffer.StreamCharBuffer)1