Package io.pebbletemplates.pebble.utils
Class LimitedSizeWriter
- java.lang.Object
-
- java.io.Writer
-
- io.pebbletemplates.pebble.utils.LimitedSizeWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.Appendable,java.lang.AutoCloseable
public class LimitedSizeWriter extends java.io.WriterA 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 Modifier and Type Field Description private RenderedSizeContextcontextprivate java.io.WriterinternalWriter
-
Constructor Summary
Constructors Modifier Constructor Description privateLimitedSizeWriter(java.io.Writer internalWriter, RenderedSizeContext context)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()static java.io.Writerfrom(java.io.Writer internalWriter, RenderedSizeContext context)java.lang.StringtoString()private booleanwillExceedMaxChars(int charsToWrite)voidwrite(char[] cbuf, int off, int len)
-
-
-
Field Detail
-
internalWriter
private final java.io.Writer internalWriter
-
context
private final RenderedSizeContext context
-
-
Constructor Detail
-
LimitedSizeWriter
private LimitedSizeWriter(java.io.Writer internalWriter, RenderedSizeContext context)
-
-
Method Detail
-
from
public static java.io.Writer from(java.io.Writer internalWriter, RenderedSizeContext context)
-
write
public void write(char[] cbuf, int off, int len) throws java.io.IOException- Specified by:
writein classjava.io.Writer- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Specified by:
flushin classjava.io.Writer- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Writer- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
willExceedMaxChars
private boolean willExceedMaxChars(int charsToWrite)
-
-