Search in sources :

Example 1 with BoundedCharsAsEncodedBytesCounter

use of org.grails.web.util.BoundedCharsAsEncodedBytesCounter 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 2 with BoundedCharsAsEncodedBytesCounter

use of org.grails.web.util.BoundedCharsAsEncodedBytesCounter in project grails-core by grails.

the class BoundedCharsAsEncodedBytesCounterTest method testCalculation.

public void testCalculation() throws Exception {
    BoundedCharsAsEncodedBytesCounter counter = new BoundedCharsAsEncodedBytesCounter(1024, "ISO-8859-1");
    counter.getCountingWriter();
    counter.update(TEST_STRING);
    assertEquals(13, counter.size());
    assertEquals(13, TEST_STRING.getBytes("ISO-8859-1").length);
    counter.update(TEST_STRING);
    assertEquals(26, counter.size());
}
Also used : BoundedCharsAsEncodedBytesCounter(org.grails.web.util.BoundedCharsAsEncodedBytesCounter)

Aggregations

BoundedCharsAsEncodedBytesCounter (org.grails.web.util.BoundedCharsAsEncodedBytesCounter)2 IOException (java.io.IOException)1 Writer (java.io.Writer)1 StreamCharBuffer (org.grails.buffer.StreamCharBuffer)1 LazyInitializingWriter (org.grails.buffer.StreamCharBuffer.LazyInitializingWriter)1 StreamCharBufferWriter (org.grails.buffer.StreamCharBuffer.StreamCharBufferWriter)1 GrailsContentBufferingResponse (org.grails.web.sitemesh.GrailsContentBufferingResponse)1 GrailsRoutablePrintWriter (org.grails.web.sitemesh.GrailsRoutablePrintWriter)1