title: JAXB Marshaller.Listener doesn't play nice with OutputStream
body: <p>I want to use <a href="https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Marshaller.Listener.html" rel="nofollow noreferrer">Marshaller.Listener</a> to insert XML comments, for example, inside the first element.</p>
  
  <p>This works great when marshalling to an XMLStreamWriter.</p>
  
  <p>However, if I marshall to an outputstream (which I'd prefer to do for various reasons), the comment text is written to the middle of the namespace declarations on the root element!</p>
  
  <p>This happens with both the Sun/Oracle and MOXy (2.5.2) implementations.</p>
  
  <p>In the Sun/Oracle implementation, it happens because Sun/Oracle write content to a variable called octetBuffer, and only copy octetBuffer to outputstream periodically. See <a href="https://github.com/eclipse-ee4j/jaxb-ri/blob/afa88da958e1cfb953fb4c38f0b4a1cfec608162/jaxb-ri/runtime/impl/src/main/java/com/sun/xml/bind/v2/runtime/output/UTF8XmlOutput.java#L351" rel="nofollow noreferrer">UTF8XmlOutput</a>. </p>
  
  <p>In MOXy (2.5.2), it looks like <a href="https://github.com/eclipse/eclipselink.runtime/blob/339316cb45b2999ea0979c06677699b034d812ff/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/OutputStreamRecord.java#L602" rel="nofollow noreferrer">OutputStreamRecord</a> is using a byte[] buffer in a similar fashion.</p>
  
  <p>I didn't notice any documentation warning me not to touch my outputstream from my listener, but its clear that doing so is problematic unless those internal buffers can first be flushed.  </p>
  
  <p>Any ideas for workarounds?   </p>