⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystemnode.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }

            buf.put(this.isLeaf() ? (byte) 1 : (byte) 2);
            buf.putLong(this.parentOffset);

            long pointOffset = 0;

            if (this.getEntriesCount() > 0) {
                Envelope env = null;

                for (int i = 0; i < this.getEntriesCount(); i++) {
                    env = this.entries[i].getBounds();
                    buf.putDouble(env.getMinX());
                    buf.putDouble(env.getMaxX());
                    buf.putDouble(env.getMinY());
                    buf.putDouble(env.getMaxY());

                    Object objData = this.entries[i].getData();

                    if (this.isLeaf()) {
                        this.storeKeyData(dataBuf, (Data) objData);
                        pointOffset = -1;
                    } else {
                        pointOffset = ((Long) objData).longValue();
                    }

                    buf.putLong(pointOffset);
                }
            }

            synchronized (channel) {
                if (this.offset == -1) {
                    throw new TreeException("Cannot flush a new node!");
                }

                buf.position(0);
                channel.position(this.offset);
                channel.write(buf);

                // If I'm a leaf, then store my Data
                if (this.isLeaf()) {
                    dataBuf.position(0);
                    channel.write(dataBuf);
                }

                if (this.params.getForceChannel()) {
                    channel.force(false);
                }
            }

            this.flushNeeded = false;
        } catch (IOException e) {
            throw new TreeException(e);
        }
    }

    /**
     * @see org.geotools.rtree.Node#save()
     */
    protected void doSave() throws TreeException {
        FileChannel channel = this.params.getChannel();

        try {
            // Allocate needed space for this node
            if (this.offset == -1) {
                /*
                 * synchronized (channel) { this.offset = channel.size();
                 * channel.position(this.offset);
                 * 
                 * ByteBuffer buf = this.getEmptyByteBuffer(); buf.position(0);
                 * channel.write(buf);
                 * 
                 * if (this.isLeaf()) { buf =
                 * this.getEmptyByteBuffer(this.params.getDataDef());
                 * buf.position(0); channel.write(buf); } if
                 * (this.params.getForceChannel()) { channel.force(false); } }
                 */
                int len = pageLen;

                if (this.isLeaf()) {
                    len += (this.params.getDataDef().getEncodedLen() * this.params
                            .getMaxNodeEntries());
                }

                this.offset = this.params.getNewNodeOffset(len);

                this.flushNeeded = true;
            }

            // Change parentOffset of my childrens
            if (this.isChanged && !this.isLeaf()) {
                FileSystemNode child = null;

                for (int i = 0; i < this.entriesCount; i++) {
                    child = this.params.getFromCache(((Long) this.entries[i]
                            .getData()).longValue());

                    child.setParent(this);
                }
            }

            this.params.putToCache(this);
        } catch (IOException e) {
            throw new TreeException(e);
        }

        /*
         * try { // Prepare buffers... ByteBuffer buf =
         * this.getEmptyByteBuffer(); ByteBuffer dataBuf = null; if
         * (this.isLeaf()) { dataBuf =
         * this.getEmptyByteBuffer(this.params.getDataDef()); }
         * 
         * buf.put(this.isLeaf() ? (byte)1 : (byte)2);
         * buf.putLong(this.parentOffset);
         * 
         * long pointOffset = 0; if (this.getEntriesCount() > 0) { Envelope env =
         * null; for (int i = 0; i < this.getEntriesCount(); i++) { env =
         * this.entries[i].getBounds(); buf.putDouble(env.getMinX());
         * buf.putDouble(env.getMaxX()); buf.putDouble(env.getMinY());
         * buf.putDouble(env.getMaxY());
         * 
         * Object objData = this.entries[i].getData(); if (this.isLeaf()) {
         * this.storeKeyData(dataBuf, (Data)objData); pointOffset = -1; } else {
         * pointOffset = ((Long)objData).longValue(); }
         * 
         * buf.putLong(pointOffset); } } synchronized (channel) { if
         * (this.offset == -1) { // I'm a new Node this.offset = channel.size(); }
         * 
         * buf.position(0); channel.position(this.offset); channel.write(buf);
         *  // If I'm a leaf, then store my Data if (this.isLeaf()) {
         * dataBuf.position(0); channel.write(dataBuf); }
         *  // Change parentOffset of my childrens if (this.isChanged &&
         * !this.isLeaf()) { ByteBuffer childBuf = ByteBuffer.allocate(8);
         * childBuf.putLong(this.offset); Long pos = null; for (int i = 0; i <
         * this.entriesCount; i++) { pos = (Long)this.entries[i].getData();
         * childBuf.flip(); channel.position(pos.longValue() + 1);
         * channel.write(childBuf); } }
         * 
         * if (this.params.getForceChannel()) { channel.force(false); } }
         *  } catch (IOException e) { throw new TreeException(e); }
         */
    }

    /**
     * Force this node flush
     * 
     * @throws Throwable
     *                 DOCUMENT ME!
     */
    protected void finalize() throws Throwable {
        this.flush();
    }

    /**
     * DOCUMENT ME!
     * 
     * @param buf
     * @param data
     * 
     * @throws IOException
     */
    private void storeKeyData(ByteBuffer buf, Data data) throws IOException {
        Object val = null;
        Field field = null;

        for (int i = 0; i < data.getValuesCount(); i++) {
            val = data.getValue(i);
            field = data.getDefinition().getField(i);

            if (val instanceof Short) {
                buf.putShort(((Short) val).shortValue());
            } else if (val instanceof Integer) {
                buf.putInt(((Integer) val).intValue());
            } else if (val instanceof Long) {
                buf.putLong(((Long) val).longValue());
            } else if (val instanceof Float) {
                buf.putFloat(((Float) val).floatValue());
            } else if (val instanceof Double) {
                buf.putDouble(((Double) val).doubleValue());
            } else if (val instanceof String) {
                ByteBuffer strBuffer = ByteBuffer.allocate(field
                        .getEncodedLen());

                ByteBuffer enc = data.getDefinition().getCharset().encode(
                        val.toString());

                enc.position(0);
                strBuffer.put(enc);
                strBuffer.position(0);
                buf.put(strBuffer);
            }
        }
    }

    /**
     * DOCUMENT ME!
     * 
     * @throws IOException
     */
    void free() throws IOException {
        if (this.offset < 0) {
            return;
        }

        /*
         * FileChannel channel = this.params.getChannel();
         * 
         * ByteBuffer buf = this.getEmptyByteBuffer();
         * 
         * synchronized (channel) { channel.position(this.offset);
         * channel.write(buf);
         * 
         * if (this.isLeaf()) { buf =
         * this.getEmptyByteBuffer(this.params.getDataDef());
         * channel.write(buf); }
         * 
         * if (this.params.getForceChannel()) { channel.force(false); } }
         */
        this.flushNeeded = false;
        this.params.removeFromCache(this);
        this.params.getFreePages().push(new Long(this.offset));
    }

    /**
     * @see org.geotools.rtree.Node#getEntry(org.geotools.rtree.Node)
     */
    protected Entry getEntry(Node node) {
        FileSystemNode fn = (FileSystemNode) node;

        Entry ret = null;
        Long l = null;

        for (int i = 0; i < this.getEntriesCount(); i++) {
            l = (Long) this.entries[i].getData();

            if (l.longValue() == fn.getOffset()) {
                ret = this.entries[i];

                break;
            }
        }

        return ret;
    }

    /**
     * @see org.geotools.rtree.Node#setParent(org.geotools.rtree.Node)
     */
    public void setParent(Node node) {
        if (node == null) {
            this.parentOffset = -1;
        } else {
            FileSystemNode fn = (FileSystemNode) node;
            this.parentOffset = fn.getOffset();
        }

        this.flushNeeded = true;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {
        FileSystemNode comp = (FileSystemNode) obj;

        return this.getOffset() == comp.getOffset();
    }
}

⌨️ 快捷键说明

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