Search in sources :

Example 1 with StreamCharBuffer

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

the class IncludeResponseWrapper method getWriter.

@Override
public PrintWriter getWriter() throws IOException {
    if (usingStream)
        throw new IllegalStateException("Method getOutputStream() already called");
    if (!usingWriter) {
        usingWriter = true;
        charBuffer = new StreamCharBuffer();
        charBuffer.setNotifyParentBuffersEnabled(false);
        pw = GrailsPrintWriterAdapter.newInstance(charBuffer.getWriter());
    }
    return pw;
}
Also used : StreamCharBuffer(org.grails.buffer.StreamCharBuffer)

Example 2 with StreamCharBuffer

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

the class GSPResponseWriter method getInstance.

/**
     * Static factory methdirectWritingod to create the writer.
     * @param response
     * @param max
     * @return  A GSPResponseWriter instance
     */
private static GSPResponseWriter getInstance(final ServletResponse response, final int max) {
    final BoundedCharsAsEncodedBytesCounter bytesCounter = new BoundedCharsAsEncodedBytesCounter();
    final StreamCharBuffer streamBuffer = new StreamCharBuffer(max, 0, max);
    streamBuffer.setChunkMinSize(max / 2);
    streamBuffer.setNotifyParentBuffersEnabled(false);
    final StreamCharBuffer.LazyInitializingWriter lazyResponseWriter = new StreamCharBuffer.LazyInitializingWriter() {

        public Writer getWriter() throws IOException {
            return response.getWriter();
        }
    };
    if (!(response instanceof GrailsContentBufferingResponse)) {
        streamBuffer.connectTo(new StreamCharBuffer.LazyInitializingMultipleWriter() {

            public Writer getWriter() throws IOException {
                return null;
            }

            public LazyInitializingWriter[] initializeMultiple(StreamCharBuffer buffer, boolean autoFlush) throws IOException {
                final StreamCharBuffer.LazyInitializingWriter[] lazyWriters;
                if (CONTENT_LENGTH_COUNTING_ENABLED) {
                    lazyWriters = new StreamCharBuffer.LazyInitializingWriter[] { new StreamCharBuffer.LazyInitializingWriter() {

                        public Writer getWriter() throws IOException {
                            bytesCounter.setCapacity(max * 2);
                            bytesCounter.setEncoding(response.getCharacterEncoding());
                            return bytesCounter.getCountingWriter();
                        }
                    }, lazyResponseWriter };
                } else {
                    lazyWriters = new StreamCharBuffer.LazyInitializingWriter[] { lazyResponseWriter };
                }
                return lazyWriters;
            }
        }, AUTOFLUSH_ENABLED);
    } else {
        streamBuffer.connectTo(lazyResponseWriter);
    }
    if (instantiator != null) {
        GSPResponseWriter instance = (GSPResponseWriter) instantiator.newInstance();
        instance.initialize(streamBuffer, response, bytesCounter);
        return instance;
    } else {
        return new GSPResponseWriter(streamBuffer, response, bytesCounter);
    }
}
Also used : LazyInitializingWriter(org.grails.buffer.StreamCharBuffer.LazyInitializingWriter) BoundedCharsAsEncodedBytesCounter(org.grails.web.util.BoundedCharsAsEncodedBytesCounter) IOException(java.io.IOException) StreamCharBuffer(org.grails.buffer.StreamCharBuffer) LazyInitializingWriter(org.grails.buffer.StreamCharBuffer.LazyInitializingWriter) GrailsContentBufferingResponse(org.grails.web.sitemesh.GrailsContentBufferingResponse) StreamCharBufferWriter(org.grails.buffer.StreamCharBuffer.StreamCharBufferWriter) LazyInitializingWriter(org.grails.buffer.StreamCharBuffer.LazyInitializingWriter) GrailsRoutablePrintWriter(org.grails.web.sitemesh.GrailsRoutablePrintWriter) Writer(java.io.Writer)

Example 3 with StreamCharBuffer

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

the class GSPResponseWriter method getInstance.

/**
     * Static factory method to create the writer.
     *
     * TODO: this can be removed?
     *
     * @param target The target writer to write too
     * @param max
     * @return  A GSPResponseWriter instance
     */
@SuppressWarnings("unused")
private static GSPResponseWriter getInstance(Writer target, int max) {
    if (BUFFERING_ENABLED && !(target instanceof GrailsRoutablePrintWriter) && !(target instanceof StreamCharBufferWriter)) {
        StreamCharBuffer streamBuffer = new StreamCharBuffer(max, 0, max);
        streamBuffer.connectTo(target, false);
        target = streamBuffer.getWriter();
    }
    if (instantiator == null) {
        return new GSPResponseWriter(target);
    }
    GSPResponseWriter instance = (GSPResponseWriter) instantiator.newInstance();
    instance.initialize(target);
    return instance;
}
Also used : GrailsRoutablePrintWriter(org.grails.web.sitemesh.GrailsRoutablePrintWriter) StreamCharBufferWriter(org.grails.buffer.StreamCharBuffer.StreamCharBufferWriter) StreamCharBuffer(org.grails.buffer.StreamCharBuffer)

Example 4 with StreamCharBuffer

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

the class BodyContentImpl method initBuffer.

void initBuffer() {
    streamBuffer = new StreamCharBuffer();
    streamBufferWriter = streamBuffer.getWriter();
}
Also used : StreamCharBuffer(org.grails.buffer.StreamCharBuffer)

Example 5 with StreamCharBuffer

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

the class MockGrailsApplication method testPrintStreamCharBuffer.

@Test
public void testPrintStreamCharBuffer() throws IOException {
    FastStringWriter stringwriter = new FastStringWriter();
    CodecPrintWriter writer = new CodecPrintWriter(stringwriter, getEncoder(new MockGrailsApplication(), HTMLCodec.class), registry);
    StreamCharBuffer buf = new StreamCharBuffer();
    buf.getWriter().write("&&");
    writer.write(buf);
    writer.flush();
    assertEquals("&&", stringwriter.getValue());
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) StreamCharBuffer(org.grails.buffer.StreamCharBuffer) HTMLCodec(org.grails.plugins.codecs.HTMLCodec) Test(org.junit.Test)

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