title: Getting location from XMLStreamReader
body: <p>I'm working on a streaming SOAP-proxy that will be used for authentication and routing. Proxy must read and validate credentials and routing information from the SOAP header. It also must enrich the SOAP header with additional metadata that is consumed by the target services. </p>
  
  <p>I first wrapped up a quick proof of concept using StAX (XMLStreamReader and XMLStreamWriter). It seemed to work without issues, so concept was proven, but performance was not what I was exepcting. While profiling the prototype I noticed that application spent considerable amount of cpu time on character encoding/decoding (30-80%)</p>
  
  <p>I thought the solution would be simple:</p>
  
  <ul>
  <li>Store data read by XMLStreamReader from InputStream to a buffer</li>
  <li>Get the offset right before SOAP header's end tag (getLocation())</li>
  <li>Send data from buffer until offset is reached</li>
  <li>Send additional data</li>
  <li>Send remaining data from buffer</li>
  <li>Send data from InputStream</li>
  </ul>
  
  <p>This would avoid all the unneccessary encoding/decoding. However to my surprise, I found that standard implementation does not support getLocation() - it returns -1.</p>
  
  <p>Obsolete section - following does NOT actually work because XMLStreamReader consumes ridiculous amounts of memory when reading a byte at time. "I noticed that I can get the offset from InputStream, if I write a InputStreamReader that returns one byte at a time and tracks number of bytes read. This however doesn't feel like the right solution - absurd number of read calls will most likely cause greater performance hit than the extra decoding/encoding." </p>
  
  <p>What are my options here, if writing custom parser is out of the question? Is there a standard compliant XMLStreamReader implementation that is both proven and supports the getLocation()?</p>