--- edoc-applet/Upload.java 2003/06/17 10:59:07 1.2 +++ edoc-applet/Upload.java 2003/06/25 11:50:09 1.3 @@ -30,6 +30,7 @@ public class Upload extends JApplet impl static Bundle bundle; static Bundle bundleRef; JButton upload = new JButton("Upload"); + JButton download = new JButton("Download"); JRadioButton proxy = new JRadioButton("Proxy"); static int fileCount = 0; // static UploadInfo dialog = new UploadInfo(); @@ -40,13 +41,6 @@ public class Upload extends JApplet impl static boolean runningAsApplet=true; static boolean debug=false; static String rootNode = "edoc"; - static MessageDigest md ; - static - { - try { - md = MessageDigest.getInstance("MD5"); - } catch(Exception e) { e.printStackTrace();} - } public Upload() { if(!runningAsApplet) @@ -58,8 +52,10 @@ public class Upload extends JApplet impl { this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); this.getContentPane().add(upload); + this.getContentPane().add(download); this.getContentPane().add(proxy); upload.addActionListener(this); + download.addActionListener(this); proxy.addActionListener(this); id = System.currentTimeMillis(); Properties prop = System.getProperties(); @@ -135,7 +131,55 @@ public class Upload extends JApplet impl // proxy.setSelected(false); } - if (e.getActionCommand().equals("Upload")) + if (e.getActionCommand().equals("Download")) + { + + Thread thread = new Thread() + { + public void run() + { + UploadInfo dialog = new UploadInfo(); + + File startFile=new File("."); + dialog.thread=Thread.currentThread(); + //System.out.println("Called run with " + fileChooser.getSelectedFile()); + dialog.setTitle("Download Status Window"); + dialog.setSize(400, 175); + dialog.setLocation(new java.awt.Point(150, 100)); + + long time = System.currentTimeMillis(); + try + { + Connection conGet = null; + if(!runningAsApplet) + conGet=new Connection( new URL("http://core.rz-berlin.mpg.de/edoctest/oi.epl")); + else + conGet=new Connection( new URL(url+"/oi.epl")); + + conGet.ses=ses; + conGet.dialog=dialog; + Bundle bundle = bundleRef; + int fileCount = bundle.documents.size(); + for(int i=0;i< fileCount;++i) + { + Bundle.Document doc=(Bundle.Document)bundle.documents.get(i); + if(doc.fileLink=="") continue; + System.out.println(doc.title); + conGet.url=new URL(doc.fileLink); + conGet.get(); + writeChunked(conGet,new File("./download/"+doc.fileName),i+1, fileCount,doc.size, dialog); + //if(getMD5(conGet.md).equals(doc.md5cs)) System.out.println("download sucess"); + //else + // break; + } + } catch(Exception e5) { e5.printStackTrace();} + long time2 =System.currentTimeMillis(); + System.out.println("Time for download needed "+(time2-time)); + } + }; + thread.start(); + } + if (e.getActionCommand().equals("Upload")) { //url=getParameter("url"); @@ -380,7 +424,7 @@ public class Upload extends JApplet impl conPost.post(file,"new",doc.docID+"",doc.title,doc.locator,doc.comment); // conPost.post(new File("./test2.jpg"),picture.elementAt(i).toString(),artist.elementAt(i).toString(),source.elementAt(i).toString()); - System.out.println(getMD5(md)); + System.out.println(getMD5(conPost.md)); dialog.progress.setValue(100); count++; return count; @@ -395,17 +439,23 @@ public class Upload extends JApplet impl System.out.println(path); mode = "new"; boolean newVersionforAll = false, keepAllFiles = false; - Vector list=new Vector(); + Vector list=new Vector();Vector docs=new Vector();Bundle.Document refDoc=null; for(int k=0;k file is not newer + if(actualTime.getTime()==modified.getTime()) return false; + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] b=new byte[512000]; + int readCount =0; + while (readCount < length) + { + if (readCount + b.length > length) + f.read(b, 0, length - readCount); // mout.flush(); + else + f.read(b); + if (readCount + b.length > length) + { + md.update(b, 0, length - readCount); + } + else + { + md.update(b, 0, b.length); + } + readCount += b.length; + } + isNewFile = !getMD5(md).equals(refDoc.md5cs); + + } + System.out.println("is new file ? "+isNewFile+" length is equal ?"+(length==refDoc.size)); + return isNewFile; + } public static String convertUml(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim()); @@ -620,6 +715,7 @@ public class Upload extends JApplet impl if(fileList==null) list= new JList(new String[]{"click","here","test","bla","world","hello"}); else list = new JList(fileList); + JScrollPane scpane = new JScrollPane(list); scpane.setPreferredSize(new Dimension(150,150)); @@ -829,6 +925,175 @@ public class Upload extends JApplet impl // System.out.println(sb); return sb; } + public static void writeChunked(Connection conTest,File f,int count,int fileCount,int fileSize,UploadInfo dialog) throws Exception + { + InputStream r= conTest.in; + FileOutputStream file_out = new FileOutputStream(f); + BufferedOutputStream fout = new BufferedOutputStream(file_out); + int c,countC=0,limit=conTest.in.available(); + // r.skip(211); + String response = conTest.getResponseMessage(); + String lowerCase= response.toLowerCase(); + int index =lowerCase.indexOf("charset"); + int index2 =lowerCase.indexOf("=",index); + int index3 =lowerCase.indexOf("\r",index2); + String charset=(index>0) ? response.substring(index2+1,index3):""; + charset="";//charset.toUpperCase(); + System.err.println(response); + dialog.title.setText("Downloading to " + f.toString()); + dialog.progress.setValue(0); + MessageDigest md =conTest.md; + md.reset(); + String fileName = f.getName()+ " " + ((fileSize > 1000000) ? (fileSize / 1048576) + " MByte" : fileSize+ " Bytes"); + dialog.file.setText(fileName); + dialog.status.setText(count + " of " + fileCount + ((fileCount > 1) ? " files" : " file")); + //dialog.pack(); + //dialog.validate(); + if (dialog.isVisible()) + dialog.repaint(); + else + dialog.show(); + + if(lowerCase.indexOf("chunked")<0) + { + while(r.available()==0); + System.err.println(r.available()); + while ((c = r.read()) != -1) + { + byte b=(byte)c; + fout.write(b); + //md.update(b, 0, b.length); + if(debug) + System.out.print((char)c); + } + /* + if(charset!="") + fout.write(local.toString(charset)); + else + fout.write(local.toString()); + */ + //if(debug) + // System.out.print(sb); + //return sb; + } + StringBuffer sb = new StringBuffer(); + // read chunk size + while ((c = r.read()) != -1) + { + ++countC; + char ch=(char)c; + + if(Character.digit(ch, 16) != -1) + sb.append((char)c); + if(ch=='\r') + { + ch=(char)r.read(); + if( ch=='\n') + break; + } + // System.out.print((char)c); + } + int bufSize=Integer.parseInt(sb.toString().trim(), 16); + + System.err.println("chunk size "+Integer.parseInt(sb.toString().trim(), 16)+" bytes"+" limit "+limit); + //sb.append((char)r.read()); + // sb.append((char)r.read()); + sb=new StringBuffer();//.delete(0,sb.length()); + byte[] b=new byte[bufSize]; + countC=0; + StringBuffer localBuff=new StringBuffer(); + while (true) + { + // sb.append(" new Chunk "+r.available()+" \r\n "); + int readCount = 0; + int available = 0; + int readSize = b.length; int loop=0; + while (readCount < bufSize) + { + available=r.available(); + + if(available>=bufSize) + readSize=bufSize; + else + readSize=available; + + if(readCount + readSize > bufSize) + { + readSize = r.read(b, readCount, bufSize - readCount); // mout.flush(); + + } + else + { + readSize = r.read(b,readCount,readSize); + } + if(loop>=200) throw new Error("Connection timed out"); + if(readSize==0) + { + long time = System.currentTimeMillis(); + while(System.currentTimeMillis()-time<100); + } else loop=0; + + if(debug) + System.out.println(++loop+" read "+readCount+" bytes"+" readSize "+readSize); + readCount+=readSize; + + } + + + //while(r.available()0) + { + // System.err.println("new chunk size "+Integer.parseInt(localBuff.toString().trim(), 16)+" bytes"); + // System.err.println((char)r.read()); + bufSize=Integer.parseInt(localBuff.toString().trim(), 16); + //localBuff.delete(0,localBuff.length()); + localBuff=new StringBuffer(); + } // else bufSize=0; + ++countC; + // sb.append("new Chunk"+r.available()+"\r\n "); + if(bufSize==0)//(bufSize)) + break; + // System.err.println(" in Chunk "+bufSize+" buffers used "+(countC)+" \r\n "); + // // System.out.print((char)c); + } + // System.out.println(sb); + fout.flush(); + fout.close(); + + file_out.close(); + //return sb; + } + public static Hashtable getMetaData(String url) { Hashtable data=new Hashtable(); @@ -1309,6 +1574,8 @@ public class Upload extends JApplet impl String ses=""; HttpURLConnection con; UploadInfo dialog = new UploadInfo(); + MessageDigest md ; + public Connection(URL url) throws Exception { Properties prop = System.getProperties(); @@ -1344,6 +1611,12 @@ public class Upload extends JApplet impl in = new BufferedInputStream(socket.getInputStream(),2); out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()),true); this.url = url; + try + { + md = MessageDigest.getInstance("MD5"); + + } catch(Exception e) { e.printStackTrace();} + } public String getSessionID(String data) { @@ -1518,9 +1791,7 @@ public class Upload extends JApplet impl System.out.println(sb); if(sb.length()>0) addToBundle(bundleRef,sb); - try - { - + // updating document with edoc Data Bundle.Document newDoc =(Bundle.Document) bundleRef.docsLocator.get(doc.locator); doc.md5cs=newDoc.md5cs; doc.size=newDoc.size; @@ -1528,8 +1799,8 @@ public class Upload extends JApplet impl doc.docID=newDoc.docID; doc.ver=newDoc.ver; doc.fileVer=newDoc.fileVer; - bundle.exportToXML(new File("./meta.xml")); - } catch(Exception e7) { e7.printStackTrace();} + doc.modified = new File(filePath).lastModified(); + } public void sendFile(FileInputStream f,PrintStream out) throws Exception @@ -1537,7 +1808,7 @@ public class Upload extends JApplet impl int length=f.available(); md.reset(); dialog.progress.setValue(0); - byte[] b=new byte[512]; + byte[] b=new byte[16204]; int readCount =0; while (readCount < length) {