itemstatebinding.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 644 行 · 第 1/2 页

JAVA
644 行
字号
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core.persistence.bundle.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.apache.jackrabbit.core.persistence.util.BLOBStore;import org.apache.jackrabbit.core.persistence.util.ResourceBasedBLOBStore;import org.apache.jackrabbit.core.persistence.PersistenceManager;import org.apache.jackrabbit.core.state.NodeReferencesId;import org.apache.jackrabbit.core.state.NodeReferences;import org.apache.jackrabbit.core.state.NodeState;import org.apache.jackrabbit.core.state.PropertyState;import org.apache.jackrabbit.core.PropertyId;import org.apache.jackrabbit.core.NodeId;import org.apache.jackrabbit.core.value.InternalValue;import org.apache.jackrabbit.core.value.BLOBFileValue;import org.apache.jackrabbit.core.nodetype.NodeDefId;import org.apache.jackrabbit.core.nodetype.PropDefId;import org.apache.jackrabbit.name.QName;import org.apache.jackrabbit.uuid.UUID;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.jcr.PropertyType;/** * This Class implements relatively efficient serialization methods for item * states. */public class ItemStateBinding {    /** the cvs/svn id */    static final String CVS_ID = "$URL: http://svn.apache.org/repos/asf/jackrabbit/tags/1.3.3/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ItemStateBinding.java $ $Rev: 517150 $ $Date: 2007-03-12 11:42:12 +0200 (Mon, 12 Mar 2007) $";    /**     * default logger     */    private static Logger log = LoggerFactory.getLogger(ItemStateBinding.class);    /**     * serialization version 1     */    public final static int VERSION_1 = 1;    /**     * current version     */    public final static int VERSION_CURRENT = VERSION_1;    /**     * the namespace index     */    protected final StringIndex nsIndex;    /**     * the name index     */    protected final StringIndex nameIndex;    /**     * the blob store     */    protected final BLOBStore blobStore;    /**     * minimum size of binaries to store in blob store     */    protected long minBlobSize = 0x4000; // 16k    /**     * the error handling     */    protected final ErrorHandling errorHandling;    /**     * Creates a new item state binding     *     * @param errorHandling the error handing configuration     * @param blobStore the blobstore     * @param nsIndex the namespace index     * @param nameIndex the name index     */    public ItemStateBinding(ErrorHandling errorHandling,                            BLOBStore blobStore,                            StringIndex nsIndex, StringIndex nameIndex) {        this.errorHandling = errorHandling;        this.nsIndex = nsIndex;        this.nameIndex = nameIndex;        this.blobStore = blobStore;    }    /**     * Returns the minimum blob size     * @see #setMinBlobSize(long) for details.     * @return the minimum blob size     */    public long getMinBlobSize() {        return minBlobSize;    }    /**     * Sets the minimum blob size. Binary values that are shorted than this given     * size will be inlined in the serialization stream, binary value that are     * longer, will be stored in the blob store. default is 4k.     *     * @param minBlobSize the minimum blob size.     */    public void setMinBlobSize(long minBlobSize) {        this.minBlobSize = minBlobSize;    }    /**     * Deserializes a <code>NodeReferences</code> from the data input stream.     *     * @param in the input stream     * @param id the id of the nodereference to deserialize     * @param pMgr the persistence manager     * @return the node references     * @throws IOException in an I/O error occurs.     */    public NodeReferences readState(DataInputStream in, NodeReferencesId id,                                    PersistenceManager pMgr)            throws IOException {        NodeReferences state = new NodeReferences(id);        int count = in.readInt();   // count & version        int version = (count >> 24) | 0x0ff;        count &= 0x00ffffff;        for (int i = 0; i < count; i++) {            state.addReference(readPropertyId(in));    // propertyId        }        return state;    }    /**     * Serializes a <code>NodeReferences</code> to the data output stream.     *     * @param out the output stream     * @param state the state to write.     * @throws IOException in an I/O error occurs.     */    public void writeState(DataOutputStream out, NodeReferences state)            throws IOException {        // references        Collection c = state.getReferences();        out.writeInt(c.size() | (VERSION_CURRENT << 24)); // count        for (Iterator iter = c.iterator(); iter.hasNext();) {            PropertyId propId = (PropertyId) iter.next();            writePropertyId(out, propId);        }    }    /**     * Deserializes a <code>NodeState</code> from the data input stream.     *     * @param in the input streaam     * @param id the id of the nodestate to read     * @param pMgr the persistence manager     * @return the node state     * @throws IOException in an I/O error occurs.     */    public NodeState readState(DataInputStream in, NodeId id,                               PersistenceManager pMgr)            throws IOException {        NodeState state = pMgr.createNew(id);        // primaryType & version        int index = in.readInt();        int version = (index >> 24) & 0x0ff;        String uri = nsIndex.indexToString(index & 0x0ffffff);        String local = in.readUTF();        state.setNodeTypeName(new QName(uri, local));        // parentUUID        state.setParentId(readID(in));        // definitionId        state.setDefinitionId(NodeDefId.valueOf(in.readUTF()));        // mixin types        int count = in.readInt();   // count        Set set = new HashSet(count);        for (int i = 0; i < count; i++) {            set.add(readQName(in)); // name        }        if (set.size() > 0) {            state.setMixinTypeNames(set);        }        // properties (names)        count = in.readInt();   // count        for (int i = 0; i < count; i++) {            state.addPropertyName(readIndexedQName(in)); // name        }        // child nodes (list of name/uuid pairs)        count = in.readInt();   // count        for (int i = 0; i < count; i++) {            QName name = readQName(in);            NodeId parentId = readID(in);            state.addChildNodeEntry(name, parentId);        }        if (version >= VERSION_1) {            state.setModCount(readModCount(in));        }        return state;    }    /**     * Serializes a <code>NodeState</code> to the data output stream     *     * @param out the output stream     * @param state the state to write     * @throws IOException in an I/O error occurs.     */    public void writeState(DataOutputStream out, NodeState state)            throws IOException {        // primaryType & version        out.writeInt((VERSION_CURRENT << 24) | nsIndex.stringToIndex(state.getNodeTypeName().getNamespaceURI()));        out.writeUTF(state.getNodeTypeName().getLocalName());        // parentUUID        writeID(out, state.getParentId());        // definitionId        out.writeUTF(state.getDefinitionId().toString());        // mixin types        Collection c = state.getMixinTypeNames();        out.writeInt(c.size()); // count        for (Iterator iter = c.iterator(); iter.hasNext();) {            writeQName(out, (QName) iter.next());        }        // properties (names)        c = state.getPropertyNames();        out.writeInt(c.size()); // count        for (Iterator iter = c.iterator(); iter.hasNext();) {            QName pName = (QName) iter.next();            writeIndexedQName(out, pName);        }        // child nodes (list of name/uuid pairs)        c = state.getChildNodeEntries();        out.writeInt(c.size()); // count        for (Iterator iter = c.iterator(); iter.hasNext();) {            NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) iter.next();            writeQName(out, entry.getName());   // name            writeID(out, entry.getId());  // uuid        }        writeModCount(out, state.getModCount());    }    /**     * Deserializes a <code>PropertyState</code> from the data input stream.     *     * @param in the input stream     * @param id the id for the new property state     * @param pMgr the persistence manager     * @return the property state     * @throws IOException in an I/O error occurs.     */    public PropertyState readState(DataInputStream in, PropertyId id,                                   PersistenceManager pMgr)            throws IOException {        PropertyState state = pMgr.createNew(id);        // type and modcount        int type = in.readInt();        state.setModCount((short) ((type >> 16) & 0x0ffff));        type&=0x0ffff;        state.setType(type);        // multiValued        state.setMultiValued(in.readBoolean());        // definitionId        state.setDefinitionId(PropDefId.valueOf(in.readUTF()));        // values        int count = in.readInt();   // count        InternalValue[] values = new InternalValue[count];        for (int i = 0; i < count; i++) {            InternalValue val;            switch (type) {                case PropertyType.BINARY:                    int size = in.readInt();                    if (size == -1) {                        String s = in.readUTF();                        try {                            if (blobStore instanceof ResourceBasedBLOBStore) {                                val = InternalValue.create(                                        ((ResourceBasedBLOBStore) blobStore).getResource(s));                            } else {                                val = InternalValue.create(blobStore.get(s), false);                            }                        } catch (IOException e) {                            if (errorHandling.ignoreMissingBlobs()) {                                log.warn("Ignoring error while reading blob-resource: " + e);                                val = InternalValue.create(new byte[0]);                            } else {                                throw e;                            }                        } catch (Exception e) {                            throw new IOException("Unable to create property value: " + e.toString());                        }                    } else {                        // short values into memory                        byte[] data = new byte[size];                        in.readFully(data);                        val = InternalValue.create(data);                    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?