📄 objectstreamclasses.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.//// $Id: ObjectStreamClasses.java,v 1.1.2.1 2004/04/10 10:06:51 per_nyfelt Exp $package org.ozoneDB.core.storage.gammaStore;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.ObjectStreamClass;import java.io.ObjectStreamField;import java.security.Key;import java.util.Collection;import java.util.HashMap;import java.util.LinkedList;import java.util.Map;import java.util.Properties;import org.ozoneDB.OzoneObject;import org.ozoneDB.OzoneProxy;import org.ozoneDB.core.ConfigurationException;import org.ozoneDB.core.storage.PropertyConfigurable;import org.ozoneDB.core.storage.PropertyInfo;import org.ozoneDB.core.storage.wizardStore.Cluster;/** * Takes care of storing ObjectStreamClass instances into another medium, while * providing for a replacement in the form of an int. * * @author Leo */public class ObjectStreamClasses implements PropertyConfigurable { public static final PropertyInfo DIRECTORY = new PropertyInfo( ".classDesc", "string (path)", null, "directory to store class descriptions, either relative to the database directory or an absolute path", new String[] { "/var/ozone/classdesc (*nix absolute path)", "c:\\ozoneFiles\\classdesc (windows absolute path)", "classdesc (*nix or windows relative path in database dir)", "./classdesc (*nix relative path in database dir)", ".\\classdesc (windows relative path in database dir)", } ); private Map handleToOsc = new HashMap(); private Map keyToHandle = new HashMap(); private String prefix; private File directory; /** * * @throws ConfigurationException if error in properties * @throws IOException if directory could not be created (initialize == true) * @throws IOException if file in directory could not be read (initialize == false) * @throws ClassNotFoundException invalid file in directory (initialize == false) * @throws NumberFormatException invalid file in directory (initialize == false) */ public ObjectStreamClasses(Properties properties, String prefix, boolean initialize) throws IOException, ClassNotFoundException { this.prefix = prefix; String dirStr = properties.getProperty(getPrefix() + DIRECTORY.getKey(), DIRECTORY.getDefaultValue()); if (dirStr == null) { throw new ConfigurationException(getPrefix() + DIRECTORY.getKey() + " has not been specified"); } directory = new File(dirStr); if (!directory.isAbsolute()) { directory = new File(properties.getProperty(GammaStore.DIRECTORY.getKey()), dirStr); } if (initialize) { if (directory.exists()) { throw new ConfigurationException("directory " + directory + " already exists"); } if (!directory.mkdirs()) { throw new IOException("could not create " + directory); } } else { File[] files = directory.listFiles(); for (int i = 0; i < files.length; i++) { ObjectInputStream in = new ObjectInputStream(new FileInputStream(files[i])); Integer handle = Integer.valueOf(files[i].getName()); ObjectStreamClass objectStreamClass = (ObjectStreamClass) in.readObject(); handleToOsc.put(handle, objectStreamClass); keyToHandle.put(toImage(objectStreamClass), handle); } } } public String getPrefix() { return prefix; } /** * Converts this instance to a byte[] that can be directly saved to disk or * used to find the handle of an ObjectStreamClass. */ public static byte[] toImage(ObjectStreamClass objectStreamClass) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(buf); out.writeObject(objectStreamClass); out.close(); return buf.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } /** * Retrieves the ObjectStreamClass given its handle. */ public ObjectStreamClass getObjectStreamClass(int handle) { return (ObjectStreamClass) handleToOsc.get(new Integer(handle)); } /** * Gives the handle for a particular ObjectStreamClass. This handle can be * used as a unique identifier. If the given <code>ObjectStreamClass</code> * is unknown a new handle is allocated and returned. When a new handle is * returned the same handle will always be returned for that same instance. */ public int getHandle(ObjectStreamClass objectStreamClass) { byte[] image = toImage(objectStreamClass); Integer handle = (Integer) handleToOsc.get(image); if (handle != null) { return handle.intValue(); } int result = handleToOsc.size(); handleToOsc.put(image, new Integer(result)); try { FileOutputStream out = new FileOutputStream(new File(directory, Integer.toString(result))); out.write(image); out.close(); } catch (IOException e) { throw new RuntimeException(e); } return result; } public Collection getPropertyInfos() { Collection result = new LinkedList(); result.add(DIRECTORY); return result; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -