Interface Template.EncodedTemplate<T>

Type Parameters:
T - the model type
All Superinterfaces:
Renderer<T>, Template<T>, TemplateInfo
All Known Implementing Classes:
ExampleModelRenderer, HelloModelAndViewView, HelloModelView, HelloModelView, MessagePageRenderer, MessagePageRenderer
Enclosing interface:
Template<T>

public static interface Template.EncodedTemplate<T> extends Template<T>
EXPERIMENTAL support of pre-encoded templates that have the static parts of the template already encoded into bytes. By default all JStacheType.JSTACHIO templates that are generated are of this type. To disable see JStacheFlags.Flag.PRE_ENCODE_DISABLE.

This interface is to support Rocker style near zero-copy rendering where the static template parts are stored as byte arrays.

The passed in OutputStream will only have OutputStream.write(byte[]) called thus an array or list of byte arrays can be accumulated to support almost zero-copy. Consequently absolutely no mutation of the byte arrays should happen as they could be reusable static parts of the template!

Overall it is recommended that you do not use this interface unless you have an intimate knowledge of how your platform buffers data and have byte like access as current JMH benchmarking indicates that String.getBytes(java.nio.charset.Charset) is generally much faster for raw byte conversion albeit at the possible cost of increased memory. One should peform their own benchmarking to confirm using this interface is worth it.

Author:
agentgt
See Also:
API Note
The passed in OutputStream will only have OutputStream.write(byte[]) called and no mutation of the passed in byte array should happen downstream.
  • Method Details

    • write

      default void write(T model, OutputStream outputStream) throws IOException
      Renders the passed in model directly to a binary stream leveraging pre-encoded parts of the template. This may improve performance when rendering UTF-8 to an OutputStream as some of the encoding is done in advance. Because the encoding is done statically you cannot pass the charset in. The chosen charset comes from JStacheConfig.charset().
      Specified by:
      write in interface Template<T>
      Parameters:
      model - a model assumed never to be null.
      outputStream - to write to.
      Throws:
      IOException - if an error occurs while writing to the outputStream
      See Also:
      API Note
      The stream will not be closed or flushed by this call.
    • write

      <A extends Output.EncodedOutput<E>, E extends Exception> A write(T model, A output) throws E
      Renders the passed in model directly to a binary stream leveraging pre-encoded parts of the template. This may improve performance when rendering UTF-8 to an OutputStream as some of the encoding is done in advance. Because the encoding is done statically you cannot pass the charset in. The chosen charset comes from JStacheConfig.charset().
      Specified by:
      write in interface Template<T>
      Type Parameters:
      A - output type
      E - error type
      Parameters:
      model - a model assumed never to be null.
      output - to write to.
      Returns:
      the passed in output for convenience
      Throws:
      E - if an error occurs while writing to output
    • model

      default TemplateModel model(T model)
      Description copied from interface: Template
      Creates a template model pair.
      Specified by:
      model in interface Template<T>
      Parameters:
      model - never null model.
      Returns:
      executable template model pair.