|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.core.projects.cache; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import org.openide.filesystems.FileSystem; /** * Partial implementation of the cache manager using BinaryFS as the layer * implementation. Still not fully working because current LayerCacheManager * don't support replacing of the layer. * Not optimalized yet! * * @author Petr Nejedly */ public class BinaryCacheManager extends ParsingLayerCacheManager { private static final String ALL_LAYERS = "all-layers.dat"; // NOI18N private File cacheFile; /** Creates a new instance of BinaryCacheManager */ public BinaryCacheManager(File cacheDir) throws IOException { super(cacheDir); cacheFile = new File(cacheDir, ALL_LAYERS); } public boolean cacheExists() { return cacheFile.exists(); } public void cleanupCache() throws IOException { if (cacheFile.exists() && !cacheFile.delete()) { throw new IOException("Could not delete: " + cacheFile); // NOI18N } } public boolean supportsLoad() { return false; } public FileSystem createLoadedFileSystem() throws IOException { if (cacheFile.exists()) { LayerCacheManager.err.log("Loading from " + cacheFile); FileSystem fs = new BinaryFS(cacheFile.getAbsolutePath()); /* ??? // pre-enumerate interesting resources Enumeration en = fs.findResource("Services").getChildren(true); // Enumeration en = fs.getRoot().getChildren(true); while (en.hasMoreElements()) { ((FileObject)en.nextElement()).getAttribute(""); } */ return fs; } else { throw new IllegalStateException(); } } protected boolean openURLs() { return false; } protected FileSystem store(MemFolder root) throws IOException { if (cacheFile.exists()) cacheFile.delete(); OutputStream os = new FileOutputStream(cacheFile); try { sizes = new HashMap(1000); LayerCacheManager.err.log("Writing binary layer cache of length " + (computeSize(root) + BinaryFS.MAGIC.length) + " to " + cacheFile); os.write(BinaryFS.MAGIC); BinaryWriter bw = new BinaryWriter (os, root); writeFolder(bw, root); } finally { sizes = null; // free the cache os.close(); } return createLoadedFileSystem(); } private void writeFolder(BinaryWriter bw, MemFolder folder) throws IOException { if (folder.attrs != null) { bw.writeInt(folder.attrs.size()); // attr count for (Iterator it = folder.attrs.iterator(); it.hasNext(); ) { writeAttribute(bw, (MemAttr)it.next()); // write attrs } } else { bw.writeInt(0); // no attrs } if (folder.children != null) { bw.writeInt(folder.children.size()); // file count // compute len of all FileRefs int baseOffset = bw.getPosition(); for (Iterator it = folder.children.iterator(); it.hasNext(); ) { MemFileOrFolder item = (MemFileOrFolder)it.next(); baseOffset += computeHeaderSize(item); } // baseOffset now contains the offset of the first file content // write file headers for (Iterator it = folder.children.iterator(); it.hasNext(); ) { MemFileOrFolder item = (MemFileOrFolder)it.next(); bw.writeString(item.name); // String name bw.writeByte((item instanceof MemFile) ? (byte)0 : (byte)1); //boolean isFolder bw.writeInt(baseOffset); // int contentRef baseOffset += computeSize(item); // baseOffset now contains the offset of the next file content } // write file/folder contents for (Iterator it = folder.children.iterator(); it.hasNext(); ) { MemFileOrFolder item = (MemFileOrFolder)it.next(); // TODO: can check the correctenss of the offsets now if (item instanceof MemFile) { writeFile(bw, (MemFile)item); } else { writeFolder(bw, (MemFolder)item); } } } else { bw.writeInt(0); // no files } } private void writeFile(BinaryWriter bw, MemFile file) throws IOException { if (file.attrs != null) { bw.writeInt(file.attrs.size()); // attr count for (Iterator it = file.attrs.iterator(); it.hasNext(); ) { writeAttribute(bw, (MemAttr)it.next()); // write attrs } } else { bw.writeInt(0); // no attrs } // int contentLength | -1, byte[contentLength] content | String URL if (file.ref != null) { bw.writeInt(-1); // uri bw.writeString(file.ref.toString()); } else if (file.contents != null) { bw.writeInt(file.contents.length); bw.writeBytes(file.contents); } else { bw.writeInt(0); // empty file } bw.writeBaseURL (file.base); } private final static String[] ATTR_TYPES = { "bytevalue", // NOI18N "shortvalue", // NOI18N "intvalue", // NOI18N "longvalue", // NOI18N "floatvalue", // NOI18N "doublevalue", // NOI18N "boolvalue", // NOI18N "charvalue", // NOI18N "stringvalue", // NOI18N "urlvalue", // NOI18N "methodvalue", // NOI18N "newvalue", // NOI18N "serialvalue", // NOI18N }; private void writeAttribute(BinaryWriter bw, MemAttr attr) throws IOException { bw.writeString(attr.name); int i = 0; for(; i |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.