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.Writer
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 Modifier and Type Field Description private RenderedSizeContext
context
private java.io.Writer
internalWriter
-
Constructor Summary
Constructors Modifier Constructor Description private
LimitedSizeWriter(java.io.Writer internalWriter, RenderedSizeContext context)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
static java.io.Writer
from(java.io.Writer internalWriter, RenderedSizeContext context)
java.lang.String
toString()
private boolean
willExceedMaxChars(int charsToWrite)
void
write(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:
write
in classjava.io.Writer
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in classjava.io.Writer
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Writer
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
willExceedMaxChars
private boolean willExceedMaxChars(int charsToWrite)
-
-