class TemplateSerializer {

    public static byte[] serialize(final TemplateDTO dto) {
        try {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final BufferedOutputStream bos = new BufferedOutputStream(baos);

            JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
            Marshaller marshaller = context.createMarshaller();
            XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
            XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos));
            marshaller.marshal(dto, writer);

            bos.flush();
            return baos.toByteArray(); //Note: For really large templates this could use a lot of heap space
        } catch (final IOException | JAXBException | XMLStreamException e) {
            throw new FlowSerializationException(e);
        }
    }

}