itemstatebinding.java

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

JAVA
644 行
字号
                    break;                case PropertyType.DOUBLE:                    val = InternalValue.create(in.readDouble());                    break;                case PropertyType.LONG:                    val = InternalValue.create(in.readLong());                    break;                case PropertyType.BOOLEAN:                    val = InternalValue.create(in.readBoolean());                    break;                case PropertyType.NAME:                    val = InternalValue.create(readQName(in));                    break;                case PropertyType.REFERENCE:                    val = InternalValue.create(readUUID(in));                    break;                default:                    // because writeUTF(String) has a size limit of 64k,                    // Strings are serialized as <length><byte[]>                    int len = in.readInt();                    byte[] bytes = new byte[len];                    int pos=0;                    while (pos < len) {                        pos+= in.read(bytes, pos, len-pos);                    }                    val = InternalValue.valueOf(new String(bytes, "UTF-8"), type);            }            values[i] = val;        }        state.setValues(values);        return state;    }    /**     * Serializes a <code>PropertyState</code> to the data output stream     * @param out the output stream     * @param state the property state to write     * @throws IOException in an I/O error occurs.     */    public void writeState(DataOutputStream out, PropertyState state)            throws IOException {        // type & mod count        out.writeInt(state.getType() | (state.getModCount() << 16));        // multiValued        out.writeBoolean(state.isMultiValued());        // definitionId        out.writeUTF(state.getDefinitionId().toString());        // values        InternalValue[] values = state.getValues();        out.writeInt(values.length); // count        for (int i = 0; i < values.length; i++) {            InternalValue val = values[i];            switch (state.getType()) {                case PropertyType.BINARY:                    try {                        // special handling required for binary value:                        // spool binary value to file in blob store                        BLOBFileValue blobVal = (BLOBFileValue) val.internalValue();                        long size = blobVal.getLength();                        if (size > minBlobSize) {                            out.writeInt(-1);                            InputStream in = blobVal.getStream();                            String blobId = blobStore.createId((PropertyId) state.getId(), i);                            try {                                blobStore.put(blobId, in, size);                            } finally {                                try {                                    in.close();                                } catch (IOException e) {                                    // ignore                                }                            }                            // store id of blob as property value                            out.writeUTF(blobId);   // value                            // replace value instance with value                            // backed by resource in blob store and delete temp file                            if (blobStore instanceof ResourceBasedBLOBStore) {                                values[i] = InternalValue.create(((ResourceBasedBLOBStore) blobStore).getResource(blobId));                            } else {                                values[i] = InternalValue.create(blobStore.get(blobId), false);                            }                            blobVal.discard();                        } else {                            // delete evt. blob                            out.writeInt((int) size);                            byte[] data = new byte[(int) size];                            InputStream in = blobVal.getStream();                            try {                                int pos = 0;                                while (pos < size) {                                    int n = in.read(data, pos, (int) size-pos);                                    if (n < 0) {                                        throw new EOFException();                                    }                                    pos += n;                                }                            } finally {                                try {                                    in.close();                                } catch (IOException e) {                                    // ignore                                }                            }                            out.write(data, 0, data.length);                            // replace value instance with value                            // backed by resource in blob store and delete temp file                            values[i] = InternalValue.create(data);                            blobVal.discard();                        }                    } catch (IOException e) {                        throw e;                    } catch (Exception e) {                        throw new IOException("Error converting: " + e.toString());                    }                    break;                case PropertyType.DOUBLE:                    out.writeDouble(((Double) val.internalValue()).doubleValue());                    break;                case PropertyType.LONG:                    out.writeLong(((Long) val.internalValue()).longValue());                    break;                case PropertyType.BOOLEAN:                    out.writeBoolean(((Boolean) val.internalValue()).booleanValue());                    break;                case PropertyType.NAME:                    writeQName(out, (QName) val.internalValue());                    break;                case PropertyType.REFERENCE:                    writeUUID(out, (UUID) val.internalValue());                    break;                default:                    // because writeUTF(String) has a size limit of 64k,                    // we're using write(byte[]) instead                    byte[] bytes = val.toString().getBytes("UTF-8");                    out.writeInt(bytes.length); // lenght of byte[]                    out.write(bytes);   // byte[]            }        }    }    /**     * Deserializes a UUID     * @param in the input stream     * @return the uuid     * @throws IOException in an I/O error occurs.     */    public UUID readUUID(DataInputStream in) throws IOException {        if (in.readBoolean()) {            byte[] bytes = new byte[16];            int pos=0;            while (pos < 16) {                pos += in.read(bytes, pos, 16-pos);            }            return new UUID(bytes);        } else {            return null;        }    }    /**     * Serializes a UUID     * @param out the output stream     * @param uuid the uuid     * @throws IOException in an I/O error occurs.     */    public void writeUUID(DataOutputStream out, String uuid) throws IOException {        if (uuid == null) {            out.writeBoolean(false);        } else {            out.writeBoolean(true);            out.write(UUID.fromString(uuid).getRawBytes());        }    }    /**     * Deserializes a NodeID     * @param in the input stream     * @return the uuid     * @throws IOException in an I/O error occurs.     */    public NodeId readID(DataInputStream in) throws IOException {        if (in.readBoolean()) {            byte[] bytes = new byte[16];            int pos=0;            while (pos < 16) {                pos += in.read(bytes, pos, 16-pos);            }            return new NodeId(new UUID(bytes));        } else {            return null;        }    }    /**     * Serializes a node id     * @param out the output stream     * @param id the id     * @throws IOException in an I/O error occurs.     */    public void writeID(DataOutputStream out, NodeId id) throws IOException {        if (id == null) {            out.writeBoolean(false);        } else {            out.writeBoolean(true);            out.write(id.getUUID().getRawBytes());        }    }    /**     * Serializes a UUID     * @param out the output stream     * @param uuid the uuid     * @throws IOException in an I/O error occurs.     */    public void writeUUID(DataOutputStream out, UUID uuid) throws IOException {        if (uuid == null) {            out.writeBoolean(false);        } else {            out.writeBoolean(true);            out.write(uuid.getRawBytes());        }    }    /**     * Deserializes a QName     * @param in the input stream     * @return the qname     * @throws IOException in an I/O error occurs.     */    public QName readQName(DataInputStream in) throws IOException {        String uri = nsIndex.indexToString(in.readInt());        String local = in.readUTF();        return new QName(uri, local);    }    /**     * Deserializes a mod-count     * @param in the input stream     * @return the mod count     * @throws IOException in an I/O error occurs.     */    public short readModCount(DataInputStream in) throws IOException {        return in.readShort();    }    /**     * Sersializes a mod-count     * @param out the output stream     * @param modCount the mod count     * @throws IOException in an I/O error occurs.     */    public void writeModCount(DataOutputStream out, short modCount) throws IOException {        out.writeShort(modCount);    }    /**     * Serializes a QName     * @param out the output stream     * @param name the name     * @throws IOException in an I/O error occurs.     */    public void writeQName(DataOutputStream out, QName name) throws IOException {        out.writeInt(nsIndex.stringToIndex(name.getNamespaceURI()));        out.writeUTF(name.getLocalName());    }    /**     * Deserializes an indexed QName     * @param in the input stream     * @return the qname     * @throws IOException in an I/O error occurs.     */    public QName readIndexedQName(DataInputStream in) throws IOException {        int index = in.readInt();        if (index<0) {            return null;        } else {            String uri = nsIndex.indexToString(index);            String local = nameIndex.indexToString(in.readInt());            return new QName(uri, local);        }    }    /**     * Serializes a indexed QName     * @param out the output stream     * @param name the name     * @throws IOException in an I/O error occurs.     */    public void writeIndexedQName(DataOutputStream out, QName name) throws IOException {        if (name == null) {            out.writeInt(-1);        } else {            out.writeInt(nsIndex.stringToIndex(name.getNamespaceURI()));            out.writeInt(nameIndex.stringToIndex(name.getLocalName()));        }    }    /**     * Serializes a PropertyId     * @param out the output stream     * @param id the id     * @throws IOException in an I/O error occurs.     */    public void writePropertyId(DataOutputStream out, PropertyId id) throws IOException {        writeID(out, id.getParentId());        writeQName(out, id.getName());    }    /**     * Deserializes a PropertyId     * @param in the input stream     * @return the property id     * @throws IOException in an I/O error occurs.     */    public PropertyId readPropertyId(DataInputStream in) throws IOException {        UUID uuid = readUUID(in);        QName name = readQName(in);        return new PropertyId(new NodeId(uuid), name);    }}

⌨️ 快捷键说明

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