Class ByteTemplate

java.lang.Object
dev.relism.flash.template.ByteTemplate

public final class ByteTemplate extends Object
Precompiled, allocation-minimal byte template.

Placeholders of the form {{name}} are detected once at construction. Each render(java.lang.String...) call makes exactly one allocation: the output byte[] (plus one byte[] per distinct key-value pair, for its UTF-8 bytes).

Layout: seg[0] slot[0] seg[1] slot[1] … seg[n-1] slot[n-1] seg[n] A slot name can appear more than once (e.g. {{var}} == {{var}}), so the map built at construction maps each name to the (usually single-element) array of every slot index using that name, instead of the nested "scan every slot for every pair" loop this used to do.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    render(String... kvPairs)
    Render with alternating key-value String pairs: k1, v1, k2, v2, … Unmatched slots are rendered as empty.
    int
    renderInto(byte[] buffer, int offset, String... kvPairs)
    Renders into buffer starting at offset, making no allocation beyond the per-pair UTF-8 conversion of kvPairs' values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteTemplate

      public ByteTemplate(String source)
  • Method Details

    • render

      public byte[] render(String... kvPairs)
      Render with alternating key-value String pairs: k1, v1, k2, v2, … Unmatched slots are rendered as empty. Allocates the returned byte[]; for a caller-supplied buffer see renderInto(byte[], int, String...).
    • renderInto

      public int renderInto(byte[] buffer, int offset, String... kvPairs)
      Renders into buffer starting at offset, making no allocation beyond the per-pair UTF-8 conversion of kvPairs' values. Returns the number of bytes written.
      Throws:
      IndexOutOfBoundsException - if buffer does not have enough room from offset