home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / Devices / SK8Stream.java < prev    next >
Encoding:
Java Source  |  1997-02-27  |  3.0 KB  |  124 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7.  
  8. import java.io.*;
  9.  
  10.  
  11. public class stream extends Object {
  12.     private file       mFile;
  13.     private String        mIfExists;             //"error", "rename","overwrite" or "append"
  14.     private String        mIfDoesNotExist;     //"error" or "create"
  15.     protected String    mDirection;         //"input", "ouput", or "closed"
  16.     
  17.     
  18.     //private FileInputStream     mInFileStream;
  19.     //private BufferedInputStream    mInBufferedStream;
  20.     protected DataInputStream        mInDataStream;
  21.     
  22.     //private FileOutputStream    mOutFileStream;
  23.     //private BufferedOutputStream mOutBufferedStream;
  24.     protected DataOutputStream    mOutDataStream;
  25.     
  26.     public String ifexists(){
  27.         return mIfExists;
  28.     }
  29.     public void set_ifexists(String s){
  30.         if (s.equals("error") ||
  31.                 s.equals("rename") ||
  32.                 s.equals("overwrite") ||
  33.                 s.equals("append")) {
  34.             mIfExists = s;
  35.         }
  36.     } 
  37.     
  38.     public String ifdoesnotexist(){
  39.         return mIfDoesNotExist;
  40.     }
  41.     public void set_ifdoesnotexist(String s){
  42.         if (s.equals("error") ||
  43.                 s.equals("create")) {
  44.             mIfDoesNotExist = s;
  45.         }
  46.     } 
  47.     
  48.     
  49.     public String direction (){
  50.         return mDirection;
  51.     }
  52.     
  53.     public boolean set_direction (String inVal){
  54.         inVal = inVal.toLowerCase();
  55.         if (!(inVal.equals("input")) && !(inVal.equals("output"))  && !(inVal.equals("closed"))){
  56.             return false;
  57.         }
  58.         
  59.         if (inVal.equals("closed"))
  60.             try { this.close(); } catch (IOException e) {}
  61.         else 
  62.             mDirection = inVal;
  63.         return true;
  64.     }
  65.     
  66.     public file    file(){
  67.         return mFile;
  68.     }
  69.     
  70.     public boolean set_file (file f){
  71.         mFile = f;
  72.         return true;
  73.     }
  74.     
  75.     public void close() throws IOException {
  76.         if ((mDirection.equals( "input")) && (mInDataStream != null)){
  77.             mInDataStream.close();
  78.             
  79.         } else if ((mDirection.equals("output")) && (mOutDataStream != null)){
  80.             mOutDataStream.close();
  81.         }
  82.         
  83.         mDirection = "closed";
  84.     }
  85.     
  86.     public void flush() throws IOException {
  87.         if ((mDirection.equals("output")) && (mOutDataStream != null)){
  88.             mOutDataStream.flush();
  89.         }
  90.     }
  91.     
  92.     
  93.     
  94.  
  95.     
  96.     protected void  setupStreams() throws IOException {
  97.         if (mFile != null) {
  98.             if (mDirection.equals("input")) {
  99.                 if (mFile.fileexists()) {
  100.                 String origName = mFile.physicalname();
  101.                     if (mIfExists.equals("error"))
  102.                         throw new IOException ("Writing to file that already exists");
  103.                     else if (mIfExists.equals("rename")) {
  104.                         for(int i = 1; mFile.fileexists(); i++)
  105.                             mFile.setname(origName + " " + i);
  106.                     }
  107.                 } else {
  108.                     if (mIfExists.equals("error"))
  109.                         throw new IOException ("Writing to file that dosen't exist");
  110.                 };
  111.                 
  112.                 FileInputStream         FIS = new FileInputStream(mFile.file());
  113.                 BufferedInputStream     BIS = new BufferedInputStream(FIS);
  114.                               mInDataStream = new DataInputStream(BIS);
  115.             } else if (mDirection.equals("output")){
  116.                 FileOutputStream         FOS = new FileOutputStream(mFile.file());
  117.                 BufferedOutputStream     BOS = new BufferedOutputStream(FOS);
  118.                              mOutDataStream = new DataOutputStream(BOS);
  119.             }
  120.         }
  121.     }
  122.     
  123.  
  124. }