KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > io > DirectoryWriter


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.io;
22
23 import proguard.classfile.*;
24
25 import java.io.*;
26
27
28 /**
29  * This DataEntryWriter writes data entries to individual files in a given
30  * directory.
31  *
32  * @author Eric Lafortune
33  */

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     /**
45      * Creates a new DirectoryWriter.
46      * @param baseFile the base directory to which all files will be written.
47      */

48     public DirectoryWriter(File baseFile,
49                            boolean isFile)
50     {
51         this.baseFile = baseFile;
52         this.isFile = isFile;
53     }
54
55
56     // Implementations for DataEntryWriter.
57

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         // Should we close the current file?
68
if (!isFile &&
69             currentFile != null &&
70             !currentFile.equals(getFile(dataEntry)))
71         {
72             closeEntry();
73         }
74
75         // Do we need a new stream?
76
if (currentOutputStream == null)
77         {
78             File file = getFile(dataEntry);
79
80             // Make sure the parent directories exist.
81
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             // Open a new output stream for writing to the file.
90
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         // Close the file stream, if any.
105
closeEntry();
106     }
107
108
109     // Small utility methods.
110

111     /**
112      * Returns the file for the given data entry.
113      */

114     private File getFile(DataEntry dataEntry)
115     {
116         // Use the specified file, or construct a new file.
117
return isFile ?
118             baseFile :
119             new File(baseFile,
120                      dataEntry.getName().replace(ClassConstants.INTERNAL_PACKAGE_SEPARATOR,
121                                                  File.separatorChar));
122     }
123
124
125     /**
126      * Closes the previous file, if any.
127      */

128     private void closeEntry() throws IOException
129     {
130         // Close the file stream, if any.
131
if (currentOutputStream != null)
132         {
133             // Let any finisher finish up first.
134
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