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();
}
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());
}
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);
}
}
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;
}
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();
}
Aggregations