Search in sources :

Example 6 with StreamCharBuffer

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

the class MockGrailsApplication method testPrintStreamCharBufferWithClosure.

@Test
public void testPrintStreamCharBufferWithClosure() throws IOException {
    FastStringWriter stringwriter = new FastStringWriter();
    CodecPrintWriter writer = new CodecPrintWriter(stringwriter, getEncoder(new MockGrailsApplication(), CodecWithClosureProperties.class), registry);
    StreamCharBuffer buf = new StreamCharBuffer();
    buf.getWriter().write("hola");
    writer.write(buf);
    writer.flush();
    assertEquals("-> hola <-", stringwriter.getValue());
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) StreamCharBuffer(org.grails.buffer.StreamCharBuffer) Test(org.junit.Test)

Example 7 with StreamCharBuffer

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

the class ChainedEncoders method chainMixedEncode.

private static void chainMixedEncode(StreamEncodeable streamEncodeable, EncodedAppender appender, List<Encoder> encoders) throws IOException {
    if (encoders == null || encoders.isEmpty()) {
        streamEncodeable.encodeTo(appender, null);
    } else {
        StreamEncodeable lastStreamEncodeable = streamEncodeable;
        if (encoders.size() > 1) {
            StreamCharBuffer buffer;
            if (streamEncodeable instanceof StreamCharBuffer) {
                buffer = (StreamCharBuffer) streamEncodeable;
            } else {
                buffer = new StreamCharBuffer();
                streamEncodeable.encodeTo(((StreamCharBuffer.StreamCharBufferWriter) buffer.getWriter()).getEncodedAppender(), null);
            }
            for (int i = 0; i < encoders.size() - 1; i++) {
                buffer = buffer.encodeToBuffer(encoders.get(i));
            }
            lastStreamEncodeable = buffer;
        }
        lastStreamEncodeable.encodeTo(appender, encoders.get(encoders.size() - 1));
    }
}
Also used : StreamCharBuffer(org.grails.buffer.StreamCharBuffer)

Example 8 with StreamCharBuffer

use of org.grails.buffer.StreamCharBuffer 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)

Aggregations

StreamCharBuffer (org.grails.buffer.StreamCharBuffer)8 FastStringWriter (org.grails.buffer.FastStringWriter)3 CodecPrintWriter (org.grails.buffer.CodecPrintWriter)2 StreamCharBufferWriter (org.grails.buffer.StreamCharBuffer.StreamCharBufferWriter)2 GrailsRoutablePrintWriter (org.grails.web.sitemesh.GrailsRoutablePrintWriter)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Writer (java.io.Writer)1 StreamByteBuffer (org.grails.buffer.StreamByteBuffer)1 LazyInitializingWriter (org.grails.buffer.StreamCharBuffer.LazyInitializingWriter)1 HTMLCodec (org.grails.plugins.codecs.HTMLCodec)1 GrailsContentBufferingResponse (org.grails.web.sitemesh.GrailsContentBufferingResponse)1 BoundedCharsAsEncodedBytesCounter (org.grails.web.util.BoundedCharsAsEncodedBytesCounter)1