Class 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.
    • 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)  
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • internalWriter

        private final java.io.Writer internalWriter
    • 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 class java.io.Writer
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • willExceedMaxChars

        private boolean willExceedMaxChars​(int charsToWrite)