Using the RandomAccessSink

The RandomAccessSink is a Sink with the ability to add hooks. To demonstrate its usage we can have a look at a FML (FAQ Markup Language)-page. The simple structure of such a page can be like:

  <faq id="1">
  <faq id="2">

Such structure would be parsed to the following page:

faq["1"].question + link  
faq["2"].question + link  
faq["1"].question + anchor  
faq["2"].question + anchor  

With a Sink you can only append and there's no option to buffer. This would mean that you have to go twice over the structure: once for only the questions and once for the question + answer.

When using the RandomAccesSink we can prepare the structure of the page

  RandomAccessSink randomAccessSink = new RandomAccessSink( sinkFactory, outputStream, encoding );
  Sink questions  = randomAccessSink.addSinkHook();
  Sink qAndA      = randomAccessSink.addSinkHook();

Now you can append the first question to both sinks, and append the answer to the second one. The same can be done with the second faq-entry.