1 26 27 package net.sourceforge.groboutils.util.io.v1; 28 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 34 35 45 public class FileInputStreamGenerator implements IInputStreamGenerator 46 { 47 private String root; 48 private String orig; 49 50 55 public FileInputStreamGenerator( String originalName ) 56 throws IOException 57 { 58 this.orig = originalName; 59 File f = new File ( originalName ); 60 if (!f.isDirectory()) 61 { 62 originalName = f.getParent(); 63 if (originalName == null) 64 { 65 originalName = ""; 66 } 67 } 68 if (originalName.endsWith( File.separator )) 69 { 70 originalName = originalName.substring( 0, 71 originalName.length() - File.separator.length() ); 72 } 73 this.root = originalName; 74 if (this.root.length() <= 0) 75 { 76 this.root = "."; 77 } 78 } 79 80 81 public File getFile( String relativeName ) 82 { 83 if (!this.orig.equals( relativeName )) 84 { 85 if (!relativeName.startsWith( "/" ) && 86 !relativeName.startsWith( File.separator )) 87 { 88 relativeName = this.root + File.separator + relativeName; 90 } 91 } 92 return new File ( relativeName ); 93 } 94 95 96 public String getFullName( String relativeName ) 97 { 98 File f = getFile( relativeName ); 99 try 100 { 101 return f.getCanonicalPath(); 102 } 103 catch (IOException ioe) 104 { 105 return f.getAbsolutePath(); 106 } 107 } 108 109 110 public InputStream createInputStream( String relativeName ) 111 throws IOException 112 { 113 return new FileInputStream ( getFile( relativeName ) ); 114 } 115 } 116 | Popular Tags |