1 21 package proguard.io; 22 23 import proguard.classfile.*; 24 25 import java.io.*; 26 27 28 34 public class DirectoryWriter implements DataEntryWriter 35 { 36 private File baseFile; 37 private boolean isFile; 38 39 private File currentFile; 40 private OutputStream currentOutputStream; 41 private Finisher currentFinisher; 42 43 44 48 public DirectoryWriter(File baseFile, 49 boolean isFile) 50 { 51 this.baseFile = baseFile; 52 this.isFile = isFile; 53 } 54 55 56 58 public OutputStream getOutputStream(DataEntry dataEntry) throws IOException 59 { 60 return getOutputStream(dataEntry, null); 61 } 62 63 64 public OutputStream getOutputStream(DataEntry dataEntry, 65 Finisher finisher) throws IOException 66 { 67 if (!isFile && 69 currentFile != null && 70 !currentFile.equals(getFile(dataEntry))) 71 { 72 closeEntry(); 73 } 74 75 if (currentOutputStream == null) 77 { 78 File file = getFile(dataEntry); 79 80 File parentDirectory = file.getParentFile(); 82 if (parentDirectory != null && 83 !parentDirectory.exists() && 84 !parentDirectory.mkdirs()) 85 { 86 throw new IOException("Can't create directory [" + parentDirectory.getPath() + "]"); 87 } 88 89 currentOutputStream = 91 new BufferedOutputStream( 92 new FileOutputStream(file)); 93 94 currentFinisher = finisher; 95 currentFile = file; 96 } 97 98 return currentOutputStream; 99 } 100 101 102 public void close() throws IOException 103 { 104 closeEntry(); 106 } 107 108 109 111 114 private File getFile(DataEntry dataEntry) 115 { 116 return isFile ? 118 baseFile : 119 new File(baseFile, 120 dataEntry.getName().replace(ClassConstants.INTERNAL_PACKAGE_SEPARATOR, 121 File.separatorChar)); 122 } 123 124 125 128 private void closeEntry() throws IOException 129 { 130 if (currentOutputStream != null) 132 { 133 if (currentFinisher != null) 135 { 136 currentFinisher.finish(); 137 currentFinisher = null; 138 } 139 140 currentOutputStream.close(); 141 currentOutputStream = null; 142 currentFile = null; 143 } 144 } 145 } 146 | Popular Tags |