Author: batosai
Date: 2008-06-16 20:07:34 +0000 (Mon, 16 Jun 2008)
New Revision: 20379

Modified:
   trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java
Log:
Workaround the fact that the InputStream dies with the request. Thanks to Bombe 
for pointing it to me.

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java 2008-06-16 18:34:26 UTC 
(rev 20378)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java 2008-06-16 20:07:34 UTC 
(rev 20379)
@@ -1,5 +1,8 @@
 package plugins.WoT;

+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Iterator;
@@ -55,7 +58,6 @@
        private FcpConnection fcp;

        private boolean fileFetched;
-       private InputStream pis;
        private String identifier = "WoTfetcher"; 

        /**
@@ -76,8 +78,9 @@
                        this.wait();
                }
                if(fileFetched) {
-                       //identity.fromXML(pis);
-                       System.out.println(pis.available());
+                       File tempfile = new File("tempfile");
+                       identity.fromXML(new FileInputStream(tempfile));
+                       tempfile.delete();
                }
        }

@@ -203,15 +206,26 @@
        public void receivedAllData(FcpConnection fcpConnection, AllData 
allData) {

                if(allData.getIdentifier().equals(identifier)) {
-                       fileFetched = true;
-                       this.pis = allData.getPayloadInputStream();

+                       // Ugly workaround, as the InputStream only lives as 
long as the request.
+                       // TODO Clean this when the high-level API comes out !
                        try {
-                               System.out.println(pis.available());
+                           final int bufferSize = 1000;
+                           FileOutputStream fout = new FileOutputStream(new 
File("tempfile"));
+                           byte[] buffer = new byte[bufferSize];
+                           int readCount = 0;
+                           while ((readCount = 
allData.getPayloadInputStream().read(buffer)) != -1) { 
+                             if (readCount < bufferSize) {
+                               fout.write(buffer, 0, readCount);
+                             } else {
+                               fout.write(buffer);
+                             }
+                           }
                        }
                        catch (Exception e) {
                                e.printStackTrace();
                        }
+                   fileFetched = true;
                }

                synchronized (this) {


Reply via email to