use of org.apache.commons.io.output.StringBuilderWriter in project uPortal by Jasig.
the class LimitingTeeWriterTest method testContentExceedsResetBuffer.
@Test
public void testContentExceedsResetBuffer() throws IOException {
final String content = "<p>Simple content</p>";
StringBuilderWriter stringWriter = new StringBuilderWriter();
LimitingTeeWriter writer = new LimitingTeeWriter(content.length() - 1, NullWriter.NULL_WRITER, stringWriter);
writer.write(content);
Assert.assertTrue(writer.isLimitReached());
Assert.assertEquals("", stringWriter.toString());
writer.resetByteCount();
// try to write more and see results
writer.write("a");
Assert.assertEquals("a", stringWriter.toString());
}
use of org.apache.commons.io.output.StringBuilderWriter in project uPortal by Jasig.
the class LimitingTeeWriterTest method testContentExceedsClearBuffer.
@Test
public void testContentExceedsClearBuffer() throws IOException {
final String content = "<p>Simple content</p>";
final StringBuilderWriter stringWriter = new StringBuilderWriter();
LimitingTeeWriter writer = new LimitingTeeWriter(content.length() - 1, NullWriter.NULL_WRITER, stringWriter, new Function<LimitingTeeWriter, Object>() {
@Override
public Object apply(LimitingTeeWriter input) {
final StringBuilder builder = stringWriter.getBuilder();
builder.delete(0, builder.length());
return null;
}
});
// write the first few chars
writer.write(content.substring(0, 5));
// verify content successfully buffered
Assert.assertFalse(writer.isLimitReached());
Assert.assertEquals(content.substring(0, 5), stringWriter.toString());
// now write the remainder
writer.write(content.substring(5, content.length()));
Assert.assertTrue(writer.isLimitReached());
Assert.assertEquals("", stringWriter.toString());
// try to write more and see no results
writer.write("a");
Assert.assertEquals("", stringWriter.toString());
}
use of org.apache.commons.io.output.StringBuilderWriter in project uPortal by Jasig.
the class LimitingTeeWriterTest method testContentExceedsThreshold.
@Test
public void testContentExceedsThreshold() throws IOException {
final String content = "<p>Simple content</p>";
StringBuilderWriter stringWriter = new StringBuilderWriter();
LimitingTeeWriter writer = new LimitingTeeWriter(content.length() - 1, NullWriter.NULL_WRITER, stringWriter);
writer.write(content);
Assert.assertTrue(writer.isLimitReached());
Assert.assertEquals("", stringWriter.toString());
// try to write more and see no results
writer.write("a");
Assert.assertEquals("", stringWriter.toString());
}
use of org.apache.commons.io.output.StringBuilderWriter in project uPortal by Jasig.
the class LimitingTeeWriterTest method testControl.
@Test
public void testControl() throws IOException {
final String content = "<p>Simple content</p>";
StringBuilderWriter stringWriter = new StringBuilderWriter();
LimitingTeeWriter writer = new LimitingTeeWriter(content.length(), NullWriter.NULL_WRITER, stringWriter);
writer.write(content);
Assert.assertFalse(writer.isLimitReached());
Assert.assertEquals(content, stringWriter.toString());
}
use of org.apache.commons.io.output.StringBuilderWriter in project uPortal by Jasig.
the class CachingPortletOutputHandler method getPrintWriter.
@Override
public PrintWriter getPrintWriter() throws IOException {
if (teeStream != null) {
throw new IllegalStateException("getOutputStream() has already been called");
}
if (this.printWriter == null) {
final PrintWriter delegateWriter = this.portletOutputHandler.getPrintWriter();
this.cachingWriter = new StringBuilderWriter();
//Create the limiting tee writer to write to the actual PrintWriter and the cachingWriter
this.teeWriter = new LimitingTeeWriter(this.maximumSize, delegateWriter, this.cachingWriter, new Function<LimitingTeeWriter, Object>() {
@Override
public Object apply(LimitingTeeWriter input) {
//Limit hit, clear the cache
clearCachedWriter();
return null;
}
});
//Wrap the limiting tee writer in a PrintWriter to return to the caller
this.printWriter = new PrintWriter(this.teeWriter);
}
return this.printWriter;
}
Aggregations