VisClient/org/hfbk/vis/FileUtils.java

Go to the documentation of this file.
00001 package org.hfbk.vis;
00002 
00003 //import java.awt.Desktop;
00004 import java.awt.Desktop;
00005 import java.io.BufferedReader;
00006 import java.io.File;
00007 import java.io.FileOutputStream;
00008 import java.io.IOException;
00009 import java.io.InputStream;
00010 import java.io.InputStreamReader;
00011 import java.io.OutputStream;
00012 import java.net.URI;
00013 import java.net.URL;
00014 
00015 public class FileUtils {
00016         
00026         public static File getNextFreeFile(String path) throws IOException{
00027                 String suffix,prefix;
00028                 int point=path.lastIndexOf(".");
00029                 if (point>0){
00030                         prefix=path.substring(0,point);
00031                         suffix=path.substring(point);
00032                 }else{
00033                         prefix=path; 
00034                         suffix="";
00035                 }
00036                 int i=0;
00037                 File f;
00038                 do{
00039                         f= new File(prefix+i+suffix);
00040                         i++;
00041                 }while (!f.createNewFile());
00042                 
00043                 return f;
00044         }
00045         
00052         public static void saveLocal(String url){
00053                 try {
00054                         
00055                         String sourceName=new URL(url).getPath();
00056                         int slash=sourceName.lastIndexOf("/");
00057                         if (slash>0)
00058                                 sourceName=sourceName.substring(slash+1);                       
00059                         
00060                         InputStream is=new URL(url).openStream();
00061                         OutputStream os=new FileOutputStream(getNextFreeFile(System.getProperty("user.home")+"/Vis_"+sourceName));
00062                         
00063                         byte[] buffer=new byte[0x4000];
00064                         int bytesRead;
00065                         while(true){
00066                                 bytesRead=is.read(buffer);
00067                                 if (bytesRead<=0)               break;
00068                                 os.write(buffer,0,bytesRead);
00069                         }
00070                         is.close(); 
00071                         os.close();
00072                         System.out.println(url+ " saved.");                     
00073                 } catch (Exception e) {
00074                         System.out.println("VisHUD.saveLocal:"+e);              
00075                 }
00076         }
00077         
00079         public static InputStream getStream(String ressource) {
00080                 try {
00081                         if (!ressource.matches("http://.*|file:.*"))
00082                                 ressource = "file:" + ressource;
00083                         URL url = new URL(ressource);
00084                         return url.openStream();
00085                 } catch (Exception e) {
00086                         throw (new RuntimeException(e));
00087                 }
00088         }
00089 
00091         public static String read(String path) {
00092                 try {
00093                         BufferedReader ir = new BufferedReader(new InputStreamReader(
00094                                         getStream(path)));
00095                         StringBuffer text = new StringBuffer();
00096                         String line;
00097 
00098                         while ((line = ir.readLine()) != null) {
00099                                 text.append(line);
00100                                 text.append('\n');
00101                         }
00102                         return text.toString();
00103                 } catch (IOException e) {
00104                         return "";
00105                 }
00106         }
00107         
00114         public static void browse(String url) {
00115                 try {
00116                         Desktop.getDesktop().browse(new URI(url));
00117                 } catch (Throwable e) {
00118                         System.out.println("Cannot browse:"+e);
00119                 }       
00120         }
00121 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1