Search in sources :

Example 6 with StreamByteBuffer

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

the class StreamByteBufferTest method bufferTest.

private void bufferTest(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();
    byte[] buffer = new byte[testBufferSize];
    InputStream input = streamBuf.getInputStream();
    assertEquals(testBufferSize, input.available());
    int readBytes = input.read(buffer);
    assertEquals(readBytes, testBufferSize);
    for (int i = 0; i < buffer.length; i++) {
        assertEquals((byte) (i % (Byte.MAX_VALUE * 2)), buffer[i]);
    }
    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 7 with StreamByteBuffer

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

the class ResourceAwareTemplateEngine method createTemplate.

@Override
public final Template createTemplate(Reader reader) throws IOException {
    StreamByteBuffer buf = new StreamByteBuffer();
    IOUtils.copy(reader, new OutputStreamWriter(buf.getOutputStream(), GROOVY_SOURCE_CHAR_ENCODING));
    return createTemplate(buf.getInputStream());
}
Also used : StreamByteBuffer(org.grails.buffer.StreamByteBuffer)

Example 8 with StreamByteBuffer

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

the class GroovyPageParser method parse.

public InputStream parse() {
    resolveKeepGeneratedDirectory();
    StreamCharBuffer streamBuffer = new StreamCharBuffer(1024);
    StreamByteBuffer byteOutputBuffer = new StreamByteBuffer(1024, StreamByteBuffer.ReadMode.RETAIN_AFTER_READING);
    try {
        streamBuffer.connectTo(new OutputStreamWriter(byteOutputBuffer.getOutputStream(), GROOVY_SOURCE_CHAR_ENCODING), true);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Grails cannot run unless your environment supports UTF-8!");
    }
    File keepGeneratedFile = null;
    Writer keepGeneratedWriter = null;
    if (keepGeneratedDirectory != null) {
        keepGeneratedFile = new File(keepGeneratedDirectory, className);
        try {
            keepGeneratedWriter = new OutputStreamWriter(new FileOutputStream(keepGeneratedFile), GROOVY_SOURCE_CHAR_ENCODING);
        } catch (IOException e) {
            LOG.warn("Cannot open keepgenerated file for writing. File's absolute path is '" + keepGeneratedFile.getAbsolutePath() + "'");
            keepGeneratedFile = null;
        }
        streamBuffer.connectTo(keepGeneratedWriter, true);
    }
    Writer target = streamBuffer.getWriter();
    try {
        generateGsp(target, false);
        if (LOG.isDebugEnabled()) {
            if (keepGeneratedFile != null) {
                LOG.debug("Compiled GSP into Groovy code. Source is in " + keepGeneratedFile);
            } else {
                LOG.debug("Configure " + CONFIG_PROPERTY_GSP_KEEPGENERATED_DIR + " property to view generated source.");
            }
        }
        return byteOutputBuffer.getInputStream();
    } finally {
        SpringIOUtils.closeQuietly(keepGeneratedWriter);
    }
}
Also used : StreamByteBuffer(org.grails.buffer.StreamByteBuffer) StreamCharBuffer(org.grails.buffer.StreamCharBuffer) FastStringWriter(org.grails.buffer.FastStringWriter)

Example 9 with StreamByteBuffer

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

the class IncludeResponseWrapper method getOutputStream.

@Override
public ServletOutputStream getOutputStream() throws IOException {
    if (usingWriter)
        throw new IllegalStateException("Method getWriter() already called");
    if (!usingStream) {
        usingStream = true;
        byteBuffer = new StreamByteBuffer();
        os = byteBuffer.getOutputStream();
        sos = new ServletOutputStream() {

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                os.write(b, off, len);
            }

            @Override
            public void write(byte[] b) throws IOException {
                os.write(b);
            }

            @Override
            public void write(int b) throws IOException {
                os.write(b);
            }
        };
    }
    return sos;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) StreamByteBuffer(org.grails.buffer.StreamByteBuffer) IOException(java.io.IOException)

Example 10 with StreamByteBuffer

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

the class StreamByteBufferTest method testStreamByteBuffer2.

public void testStreamByteBuffer2() throws Exception {
    StreamByteBuffer streamBuf = new StreamByteBuffer(32000);
    OutputStream output = streamBuf.getOutputStream();
    byte[] bytes = new byte[] { (byte) 1, (byte) 2, (byte) 3 };
    output.write(bytes);
    output.close();
    InputStream input = streamBuf.getInputStream();
    assertEquals(1, input.read());
    assertEquals(2, input.read());
    assertEquals(3, 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)

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