title: JAX-WS WebService Client - Streaming large attachements - different behavior, depending on JRE version
body: <p>I created a WebService client from a provided WSDL-File, using the already in the JRE bundled JAX-WS API.
  I generated the proxy classes with the <strong>wsimport</strong> tool of JDK6 Update 37.</p>
  
  <p>The client should be able to download large files/data, using MTOM and streaming.</p>
  
  <p>I followed the instructions provided in the <a href="http://metro.java.net/guide/ch06.html#large-attachments" rel="nofollow">Metro user guide</a>.</p>
  
  <p>The proxy method I'm calling returns a <code>DateHandler</code> object.</p>
  
  <pre class="lang-java prettyprint-override"><code>package de.christopherhuebner.webservicetest.client;                                                   
  
  import java.io.FileOutputStream;                                                                       
  import java.io.OutputStream;                                                                           
  import java.net.URL;                                                                                   
  
  import javax.activation.DataHandler;                                                                   
  import javax.xml.namespace.QName;                                                                      
  
  import de.christopherhuebner.webservicetest.ImageServer;                                               
  import de.christopherhuebner.webservicetest.ImageServerService;                                        
  
  public class ImageServiceClient {                                                                      
  
      public static void main(String[] args) throws Exception {                                          
          URL url = new URL("http://localhost:8080/ImageWebService?wsdl");                               
          QName qname = new QName("http://webservicetest.christopherhuebner.de/", "ImageServerService"); 
          ImageServerService is = new ImageServerService(url, qname);                                    
          MTOMFeature mtom = new MTOMFeature();
          StreamingAttachmentFeature stf = new StreamingAttachmentFeature(null, true, 4000000L);
          ImageServer port = is.getImageServerPort(mtom, stf);                                                  
          DataHandler dh = port.getImage();                                                              
          System.out.println("Java-Version: " + System.getProperty("java.version"));                     
          System.out.println("DataHandler: " + dh.getClass());                                           
          System.out.println("DataSource: " + dh.getDataSource().getClass());                            
          OutputStream out = new FileOutputStream("test.jpg");                                           
          dh.writeTo(out);                                                                               
          out.close();                                                                                   
      }                                                                                                  
  }                                                                                                      
  </code></pre>
  
  <p>When running this code with JRE6, the following output is printed to the console:</p>
  
  <pre><code>Java-Version: 1.6.0_37
  DataHandler: class javax.activation.DataHandler
  DataSource: class com.sun.istack.internal.ByteArrayDataSource
  </code></pre>
  
  <p>Unfortunately there is no streaming possible, as the <code>ByteArrayDataSource</code> is filled in the clients memory which causes an <code>OutOfMemoryException</code> when receiving data larger than the max heap size. When I dare to cast this <code>DataHandler</code> to a <code>StreamingDataHandler</code> as suggested in the already mentioned <a href="http://metro.java.net/guide/ch06.html#large-attachments" rel="nofollow">user guide</a>, a <code>ClassCastException</code> is thrown.</p>
  
  <p>When running the client with JRE7, everything is fine, as described in the <a href="http://metro.java.net/guide/ch06.html#large-attachments" rel="nofollow">user guide</a>:</p>
  
  <pre><code>Java-Version: 1.7.0_10
  DataHandler: class com.sun.xml.internal.ws.encoding.MIMEPartStreamingDataHandler
  DataSource: class com.sun.xml.internal.ws.encoding.MIMEPartStreamingDataHandler$StreamingDataSource
  </code></pre>
  
  <p>Is there any possibility to get a <code>StreamingDataHandler</code> back, when using <strong>JRE6</strong>? 
  Or am I really forced to use a newer <strong>JAX-WS RI</strong> version (Metro, which seems to be included in the JRE) via the <code>-Djava.endorsed.dirs=path_to_newer_jaxws_libs</code> mechanism?</p>
  
  <p>When are the temp files, which are generated during streaming (<em>MIMExxxxx.tmp</em>, see <code>StreamingAttachmentFeature</code> for configuration), deleted?</p>