Package io.pebbletemplates.pebble.utils
Class LimitedSizeWriter
java.lang.Object
java.io.Writer
io.pebbletemplates.pebble.utils.LimitedSizeWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
A Writer that will wrap around the internal writer if the user also provided a limit
on the size of the rendered template. The context is shared between all the writers
used to evaluate a template: the one supplied by the user when calling template.evaluate
as well as any internally created writers e.g. when evaluating a macro.
There will be false positives. For example if a function writes something and its output
is passed to a filter than we count both the output of the function and the output of the
filter, when we should only count the output of the filter. This is fine because the user
can increase the maximum allowable size accordingly. The purpose here is not to be precise
but to protect against abuse.
If the limit is reached a PebbleException will be thrown.
If the limit is negative then no checks will be performed and the original writer used as is.
This is thread-safe if RenderedSizeContext is thread-safe.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
LimitedSizeWriter
(Writer internalWriter, RenderedSizeContext context) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
flush()
static Writer
from
(Writer internalWriter, RenderedSizeContext context) toString()
private boolean
willExceedMaxChars
(int charsToWrite) void
write
(char[] cbuf, int off, int len)
-
Field Details
-
internalWriter
-
context
-
-
Constructor Details
-
LimitedSizeWriter
-
-
Method Details
-
from
-
write
- Specified by:
write
in classWriter
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in classWriter
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
-
toString
-
willExceedMaxChars
private boolean willExceedMaxChars(int charsToWrite)
-