Java Source Code: net.sf.borg.common.io.IOHelper


   1: /*
   2: This file is part of BORG.
   3:  
   4:     BORG is free software; you can redistribute it and/or modify
   5:     it under the terms of the GNU General Public License as published by
   6:     the Free Software Foundation; either version 2 of the License, or
   7:     (at your option) any later version.
   8:  
   9:     BORG is distributed in the hope that it will be useful,
  10:     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11:     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12:     GNU General Public License for more details.
  13:  
  14:     You should have received a copy of the GNU General Public License
  15:     along with BORG; if not, write to the Free Software
  16:     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17:  
  18: Copyright 2004 by Mohan Embar - http://www.thisiscool.com/
  19:  */
  20: 
  21: package net.sf.borg.common.io;
  22: 
  23: import java.io.BufferedReader;
  24: import java.io.FileInputStream;
  25: import java.io.FileOutputStream;
  26: import java.io.InputStream;
  27: import java.io.InputStreamReader;
  28: import java.io.OutputStream;
  29: import java.io.StringWriter;
  30: import java.net.URL;
  31: import java.net.URLConnection;
  32: 
  33: import net.sf.borg.common.app.AppHelper;
  34: import net.sf.borg.common.util.Prefs;
  35: 
  36: /**
  37:  * Abstracts the I/O subsystem. Also allows applets to perform I/O
  38:  * using our in-memory scheme.
  39:  * @author Mohan Embar
  40:  */
  41: public class IOHelper
  42:	  {
  43:    public static InputStream openStream(URL url) throws Exception
  44:	      {
  45:        return url.openStream();
  46:    }
  47:
  48:    public static InputStream openStream(String file) throws Exception
  49:	      {
  50:        if (!AppHelper.isApplication() || file.startsWith("mem:"))
  51:            return new MemFileInputStream(file, files);
  52:        else if (AppHelper.isApplication())
  53:            return new FileInputStream(file);
  54:        else
  55:            throw new IllegalArgumentException(AppHelper.getType().toString());
  56:    }
  57:
  58:    public static OutputStream createOutputStream(URL url) throws Exception
  59:	      {
  60:        URLConnection connection = url.openConnection();
  61:        connection.setDoOutput(true);
  62:        return connection.getOutputStream();
  63:    }
  64:
  65:    public static OutputStream createOutputStream(String file) throws Exception
  66:	      {
  67:        if (!AppHelper.isApplication() || file.startsWith("mem:"))
  68:            return new MemFileOutputStream(file, files);
  69:        else if (AppHelper.getType() == AppHelper.APPLICATION)
  70:	          {
  71:            return new FileOutputStream(file);
  72:        }
  73:        else
  74:            throw new IllegalArgumentException(AppHelper.getType().toString());
  75:    }
  76:    
  77:    public static String[] getMemFilesList()
  78:	      {
  79:        return files.list();
  80:    }
  81:
  82:    public static String getMemFilesMemento()
  83:	      {
  84:        // Store our preferences if possible.
  85:        byte[] prefsData = Prefs.getMemento();
  86:        if (prefsData != null)
  87:            files.put(PREFS_FILE, prefsData);
  88:        
  89:        return files.toMemento();
  90:    }
  91:    
  92:    public static boolean isMemFilesDirty()
  93:	      {
  94:        return files.isDirty();
  95:    }
  96:    
  97:    public static String loadMemoryFromURL(String urlst) throws Exception
  98:	      {
  99:        if( urlst == null || urlst.length()==0 )
 100:            return null;
 101:        URL url = new URL(urlst);
 102:        return loadMemoryFromStream(openStream(url));
 103:    }
 104:
 105:    public static String loadMemoryFromStream(InputStream istr)
 106:        throws Exception
 107:	      {
 108:        if (istr == null)
 109:            return null;
 110:    
 111:        BufferedReader rdr = new BufferedReader(new InputStreamReader(istr));
 112:        int ch;
 113:        StringWriter wrtr = new StringWriter();
 114:        while ((ch = rdr.read()) != -1)
 115:	          {
 116:            wrtr.write(ch);
 117:        }
 118:        rdr.close();
 119:        wrtr.close();
 120:        String data = wrtr.toString();
 121:        
 122:        // Try to import this.
 123:        setMemFilesMemento(data);
 124:        return data;
 125:    }
 126:
 127:    public static void setMemFilesMemento(String strData) throws Exception
 128:	      {
 129:        MemFiles newFiles = new MemFiles();
 130:        newFiles.setMemento(strData);
 131:        files = newFiles;
 132:        
 133:        // Update our preferences if possible.
 134:        if (files.contains(PREFS_FILE))
 135:            Prefs.setMemento(files.get(PREFS_FILE));
 136:    }
 137:
 138:    // private //
 139:    private static final String PREFS_FILE = "mem:/prefs.dat";
 140:    
 141:    private static MemFiles files = new MemFiles();
 142:    
 143:    private IOHelper()
 144:	      {
 145:    }
 146:}