@Override @NonNull JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, @NullAllowed final JavaFileFilterImplementation filter, @NullAllowed final Charset encoding) { final String[] pkgNamePair = FileObjects.getFolderAndBaseName(FileObjects.getRelativePath(root, file), File.separatorChar); String pname = FileObjects.convertFolder2Package(pkgNamePair[0], File.separatorChar); CachedFileObject cfo = getFileObject(location, pname, pkgNamePair[1], false); if (cfo != null) { return cfo; } String relPath = FileObjects.getRelativePath(root, file); File shadowRoot = new File(root.getParent(), root.getName() + WORK_SUFFIX); File shadowFile = new File(shadowRoot, relPath); workDirs.add(shadowRoot); cfo = new CachedFileObject(this, file, pname, pkgNamePair[1], filter, encoding); cfo.setShadowFile(shadowFile); addFile(location, pname, cfo); if (!shadowRoot.mkdirs() && !shadowRoot.exists() && !shadowRoot.isDirectory()) { throw new IllegalStateException(); } return cfo; }
private void doFlushFiles( @NonNull final Map<String, Map<File, CachedFileObject>> cacheLine, final boolean inCommit) throws IOException { for (Map<File, CachedFileObject> dirContent : cacheLine.values()) { for (CachedFileObject cfo : dirContent.values()) { cfo.flush(inCommit); if (inCommit) { cfo.commit(); } } } }
if (wasFlushed()) { return; if (inCommit) { shadowFile = this.f; release(); File f = getCurrentFile(); if (!f.getParentFile().mkdirs() && !f.getParentFile().exists()) { throw new IOException( try { out.write(content); release(); } finally { out.close();
@Override public OutputStream openOutputStream() throws IOException { modify(); if (delegate != null) { return delegate.first().openOutputStream(); } else { return new ByteArrayOutputStream() { boolean closed; @Override public void close() throws IOException { // prevent work when close() called multiple times if (closed) { return; } super.close(); closed = true; content = toByteArray(); writer.maybeFlush(); } }; } }
void commit() throws IOException { if (wasCommitted() ) { return; } flush(true); File cur = getCurrentFile(); if (f.equals(cur)) { return; } if (f.exists() && !f.delete()) { throw new IOException("Cannot delete obsolete sigfile"); } cur.renameTo(f); shadowFile = null; }
private void addFile( @NonNull final Location location, @NonNull final String packageName, @NonNull final CachedFileObject fo) { LOG.log(Level.FINE, "File added to cache:{0}:{1}", new Object[] { fo.getFile(), root }); // check whether the softref has been freed: final Map<String, Map<File, CachedFileObject>> cache = getCacheLine(location, false); Map<File, CachedFileObject> dirContent = cache.get(packageName); if (dirContent == null) { dirContent = new HashMap<File, CachedFileObject>(); cache.put(packageName, dirContent); } dirContent.put(toFile(fo), fo); }
private JavaFileObject createDelegate() { if (wasCommitted()) { delegate = Pair.<FileObjects.FileBase,Boolean>of( (FileObjects.FileBase)FileObjects.fileFileObject(f, getRootFile(getFile(), getPackage()), filter, encoding), Boolean.TRUE); } else if (wasFlushed()) { delegate = Pair.<FileObjects.FileBase,Boolean>of( (FileObjects.FileBase)FileObjects.fileFileObject(shadowFile, getRootFile(shadowFile, getPackage()), filter, encoding), Boolean.TRUE); } return delegate.first(); }
public CachedFileObject(WriteBackTransaction wb, File file, String pkgName, String name, JavaFileFilterImplementation filter, Charset encoding) { super(file, pkgName, name, filter, encoding); this.writer = wb; delegate = Pair.<FileObjects.FileBase,Boolean>of( (FileObjects.FileBase)FileObjects.fileFileObject(f, getRootFile(getFile(), getPackage()), filter, encoding), Boolean.FALSE); }
/** * Releases data held by the cache, and redirects all calls to a regular * File-based FileObject */ void release() { content = null; writer = null; createDelegate(); }