31 import java.io.IOException; |
31 import java.io.IOException; |
32 import java.io.InputStream; |
32 import java.io.InputStream; |
33 import java.io.OutputStream; |
33 import java.io.OutputStream; |
34 import java.io.OutputStreamWriter; |
34 import java.io.OutputStreamWriter; |
35 import java.io.Writer; |
35 import java.io.Writer; |
|
36 import java.lang.ref.Reference; |
|
37 import java.lang.ref.SoftReference; |
36 import java.net.URI; |
38 import java.net.URI; |
37 import java.nio.ByteBuffer; |
39 import java.nio.ByteBuffer; |
38 import java.nio.CharBuffer; |
40 import java.nio.CharBuffer; |
39 import java.nio.charset.CharsetDecoder; |
41 import java.nio.charset.CharsetDecoder; |
40 import javax.tools.JavaFileObject; |
42 import javax.tools.JavaFileObject; |
51 |
53 |
52 /** Have the parent directories been created? |
54 /** Have the parent directories been created? |
53 */ |
55 */ |
54 private boolean hasParents = false; |
56 private boolean hasParents = false; |
55 private String name; |
57 private String name; |
56 final File f; |
58 final File file; |
|
59 private Reference<File> absFileRef; |
57 |
60 |
58 public RegularFileObject(JavacFileManager fileManager, File f) { |
61 public RegularFileObject(JavacFileManager fileManager, File f) { |
59 this(fileManager, f.getName(), f); |
62 this(fileManager, f.getName(), f); |
60 } |
63 } |
61 |
64 |
63 super(fileManager); |
66 super(fileManager); |
64 if (f.isDirectory()) { |
67 if (f.isDirectory()) { |
65 throw new IllegalArgumentException("directories not supported"); |
68 throw new IllegalArgumentException("directories not supported"); |
66 } |
69 } |
67 this.name = name; |
70 this.name = name; |
68 this.f = f; |
71 this.file = f; |
69 } |
72 } |
70 |
73 |
71 @Override |
74 @Override |
72 public URI toUri() { |
75 public URI toUri() { |
73 return f.toURI().normalize(); |
76 return file.toURI().normalize(); |
74 } |
77 } |
75 |
78 |
76 @Override |
79 @Override |
77 public String getName() { |
80 public String getName() { |
78 return f.getPath(); |
81 return file.getPath(); |
79 } |
82 } |
80 |
83 |
81 @Override |
84 @Override |
82 public String getShortName() { |
85 public String getShortName() { |
83 return name; |
86 return name; |
88 return getKind(name); |
91 return getKind(name); |
89 } |
92 } |
90 |
93 |
91 @Override |
94 @Override |
92 public InputStream openInputStream() throws IOException { |
95 public InputStream openInputStream() throws IOException { |
93 return new FileInputStream(f); |
96 return new FileInputStream(file); |
94 } |
97 } |
95 |
98 |
96 @Override |
99 @Override |
97 public OutputStream openOutputStream() throws IOException { |
100 public OutputStream openOutputStream() throws IOException { |
98 ensureParentDirectoriesExist(); |
101 ensureParentDirectoriesExist(); |
99 return new FileOutputStream(f); |
102 return new FileOutputStream(file); |
100 } |
103 } |
101 |
104 |
102 @Override |
105 @Override |
103 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
106 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
104 CharBuffer cb = fileManager.getCachedContent(this); |
107 CharBuffer cb = fileManager.getCachedContent(this); |
105 if (cb == null) { |
108 if (cb == null) { |
106 InputStream in = new FileInputStream(f); |
109 InputStream in = new FileInputStream(file); |
107 try { |
110 try { |
108 ByteBuffer bb = fileManager.makeByteBuffer(in); |
111 ByteBuffer bb = fileManager.makeByteBuffer(in); |
109 JavaFileObject prev = fileManager.log.useSource(this); |
112 JavaFileObject prev = fileManager.log.useSource(this); |
110 try { |
113 try { |
111 cb = fileManager.decode(bb, ignoreEncodingErrors); |
114 cb = fileManager.decode(bb, ignoreEncodingErrors); |
124 } |
127 } |
125 |
128 |
126 @Override |
129 @Override |
127 public Writer openWriter() throws IOException { |
130 public Writer openWriter() throws IOException { |
128 ensureParentDirectoriesExist(); |
131 ensureParentDirectoriesExist(); |
129 return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName()); |
132 return new OutputStreamWriter(new FileOutputStream(file), fileManager.getEncodingName()); |
130 } |
133 } |
131 |
134 |
132 @Override |
135 @Override |
133 public long getLastModified() { |
136 public long getLastModified() { |
134 return f.lastModified(); |
137 return file.lastModified(); |
135 } |
138 } |
136 |
139 |
137 @Override |
140 @Override |
138 public boolean delete() { |
141 public boolean delete() { |
139 return f.delete(); |
142 return file.delete(); |
140 } |
143 } |
141 |
144 |
142 @Override |
145 @Override |
143 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
146 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
144 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
147 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
145 } |
148 } |
146 |
149 |
147 @Override |
150 @Override |
148 protected String inferBinaryName(Iterable<? extends File> path) { |
151 protected String inferBinaryName(Iterable<? extends File> path) { |
149 String fPath = f.getPath(); |
152 String fPath = file.getPath(); |
150 //System.err.println("RegularFileObject " + file + " " +r.getPath()); |
153 //System.err.println("RegularFileObject " + file + " " +r.getPath()); |
151 for (File dir: path) { |
154 for (File dir: path) { |
152 //System.err.println("dir: " + dir); |
155 //System.err.println("dir: " + dir); |
153 String dPath = dir.getPath(); |
156 String dPath = dir.getPath(); |
154 if (dPath.length() == 0) |
157 if (dPath.length() == 0) |
176 return true; |
179 return true; |
177 } |
180 } |
178 if (name.equalsIgnoreCase(n)) { |
181 if (name.equalsIgnoreCase(n)) { |
179 try { |
182 try { |
180 // allow for Windows |
183 // allow for Windows |
181 return f.getCanonicalFile().getName().equals(n); |
184 return file.getCanonicalFile().getName().equals(n); |
182 } catch (IOException e) { |
185 } catch (IOException e) { |
183 } |
186 } |
184 } |
187 } |
185 return false; |
188 return false; |
186 } |
189 } |
187 |
190 |
188 private void ensureParentDirectoriesExist() throws IOException { |
191 private void ensureParentDirectoriesExist() throws IOException { |
189 if (!hasParents) { |
192 if (!hasParents) { |
190 File parent = f.getParentFile(); |
193 File parent = file.getParentFile(); |
191 if (parent != null && !parent.exists()) { |
194 if (parent != null && !parent.exists()) { |
192 if (!parent.mkdirs()) { |
195 if (!parent.mkdirs()) { |
193 if (!parent.exists() || !parent.isDirectory()) { |
196 if (!parent.exists() || !parent.isDirectory()) { |
194 throw new IOException("could not create parent directories"); |
197 throw new IOException("could not create parent directories"); |
195 } |
198 } |
197 } |
200 } |
198 hasParents = true; |
201 hasParents = true; |
199 } |
202 } |
200 } |
203 } |
201 |
204 |
|
205 /** |
|
206 * Check if two file objects are equal. |
|
207 * Two RegularFileObjects are equal if the absolute paths of the underlying |
|
208 * files are equal. |
|
209 */ |
202 @Override |
210 @Override |
203 public boolean equals(Object other) { |
211 public boolean equals(Object other) { |
204 if (!(other instanceof RegularFileObject)) { |
212 if (this == other) |
|
213 return true; |
|
214 |
|
215 if (!(other instanceof RegularFileObject)) |
205 return false; |
216 return false; |
206 } |
217 |
207 RegularFileObject o = (RegularFileObject) other; |
218 RegularFileObject o = (RegularFileObject) other; |
208 try { |
219 return getAbsoluteFile().equals(o.getAbsoluteFile()); |
209 return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile()); |
|
210 } catch (IOException e) { |
|
211 return false; |
|
212 } |
|
213 } |
220 } |
214 |
221 |
215 @Override |
222 @Override |
216 public int hashCode() { |
223 public int hashCode() { |
217 return f.hashCode(); |
224 return getAbsoluteFile().hashCode(); |
|
225 } |
|
226 |
|
227 private File getAbsoluteFile() { |
|
228 File absFile = (absFileRef == null ? null : absFileRef.get()); |
|
229 if (absFile == null) { |
|
230 absFile = file.getAbsoluteFile(); |
|
231 absFileRef = new SoftReference<File>(absFile); |
|
232 } |
|
233 return absFile; |
218 } |
234 } |
219 } |
235 } |