Revision: 5843
          http://jnode.svn.sourceforge.net/jnode/?rev=5843&view=rev
Author:   galatnm
Date:     2011-08-04 12:57:47 +0000 (Thu, 04 Aug 2011)

Log Message:
-----------
Fix HFS+ cast exception.

Modified Paths:
--------------
    trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusDirectory.java
    trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/Catalog.java
    trunk/fs/src/fs/org/jnode/fs/hfsplus/extent/ExtentNode.java
    trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/AbstractNode.java
    trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/Node.java
    trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/NodeDescriptor.java

Added Paths:
-----------
    trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogIndexNode.java
    trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogLeafNode.java

Removed Paths:
-------------
    trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogNode.java

Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusDirectory.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusDirectory.java  2011-08-02 
20:04:52 UTC (rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusDirectory.java  2011-08-04 
12:57:47 UTC (rev 5843)
@@ -17,7 +17,7 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.fs.hfsplus;
 
 import java.io.FileNotFoundException;
@@ -127,6 +127,7 @@
 
     @Override
     public Iterator<? extends FSEntry> iterator() throws IOException {
+        checkEntriesLoaded();
         return entries.iterator();
     }
 
@@ -161,6 +162,7 @@
             try {
                 if (rights.canRead()) {
                     entries = readEntries();
+                    log.debug("Load " + entries.size() + " entrie(s).");
                 } else {
                     // the next time, we will call checkEntriesLoaded()
                     // we will retry to load entries
@@ -226,8 +228,9 @@
         Catalog catalog = fs.getCatalog();
         Superblock volumeHeader = ((HfsPlusFileSystem) 
getFileSystem()).getVolumeHeader();
         LeafRecord folderRecord =
-                catalog.createNode(name, this.folder.getFolderId(), new 
CatalogNodeId(volumeHeader
-                        .getNextCatalogId()), 
CatalogFolder.RECORD_TYPE_FOLDER_THREAD);
+                catalog.createNode(name, this.folder.getFolderId(),
+                        new CatalogNodeId(volumeHeader.getNextCatalogId()),
+                        CatalogFolder.RECORD_TYPE_FOLDER_THREAD);
         folder.setValence(folder.getValence() + 1);
 
         HfsPlusEntry newEntry = new HfsPlusDirectory(fs, this, name, 
folderRecord);

Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/Catalog.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/Catalog.java   2011-08-02 
20:04:52 UTC (rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/Catalog.java   2011-08-04 
12:57:47 UTC (rev 5843)
@@ -17,7 +17,7 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.fs.hfsplus.catalog;
 
 import java.io.IOException;
@@ -27,8 +27,8 @@
 
 import org.apache.log4j.Logger;
 import org.jnode.fs.hfsplus.HFSPlusParams;
+import org.jnode.fs.hfsplus.HfsPlusFileSystem;
 import org.jnode.fs.hfsplus.HfsUnicodeString;
-import org.jnode.fs.hfsplus.HfsPlusFileSystem;
 import org.jnode.fs.hfsplus.Superblock;
 import org.jnode.fs.hfsplus.extent.ExtentDescriptor;
 import org.jnode.fs.hfsplus.tree.BTHeaderRecord;
@@ -41,19 +41,19 @@
 
     private final Logger log = Logger.getLogger(getClass());
     private HfsPlusFileSystem fs;
-    
+
     /**
      * B-Tree node descriptor
      */
     private NodeDescriptor btnd;
-    
+
     /**
      * B-Tree Header record
      */
     private BTHeaderRecord bthr;
-    
+
     private int catalogHeaderNodeOffset;
-    
+
     private ByteBuffer buffer;
 
     /**
@@ -84,7 +84,7 @@
 
         }
     }
-    
+
     /**
      * Create new Catalog
      * 
@@ -105,8 +105,8 @@
         log.info("Create catalog header record.");
         bthr =
                 new BTHeaderRecord(1, 1, params.getInitializeNumRecords(), 1, 
1, nodeSize,
-                        CatalogKey.MAXIMUM_KEY_LENGTH, totalNodes, freeNodes, 
params
-                                .getCatalogClumpSize(), 
BTHeaderRecord.BT_TYPE_HFS,
+                        CatalogKey.MAXIMUM_KEY_LENGTH, totalNodes, freeNodes,
+                        params.getCatalogClumpSize(), 
BTHeaderRecord.BT_TYPE_HFS,
                         BTHeaderRecord.KEY_COMPARE_TYPE_CASE_FOLDING,
                         BTHeaderRecord.BT_VARIABLE_INDEX_KEYS_MASK +
                                 BTHeaderRecord.BT_BIG_KEYS_MASK);
@@ -117,9 +117,9 @@
         bufferLength += (rootNodePosition - bufferLength);
         // Create node descriptor
         NodeDescriptor nd =
-                new NodeDescriptor(0, 0, NodeDescriptor.BT_LEAF_NODE, 1, params
-                        .getInitializeNumRecords());
-        CatalogNode rootNode = new CatalogNode(nd, nodeSize);
+                new NodeDescriptor(0, 0, NodeDescriptor.BT_LEAF_NODE, 1,
+                        params.getInitializeNumRecords());
+        CatalogLeafNode rootNode = new CatalogLeafNode(nd, nodeSize);
         // First record (folder)
         HfsUnicodeString name = new HfsUnicodeString(params.getVolumeName());
         CatalogKey ck = new CatalogKey(CatalogNodeId.HFSPLUS_POR_CNID, name);
@@ -142,6 +142,7 @@
         buffer.put(rootNode.getBytes());
         buffer.rewind();
     }
+
     /**
      * Save catalog file to disk.
      * 
@@ -168,7 +169,7 @@
         LeafRecord record = this.getRecord(parentId, name);
         if (record == null) {
             NodeDescriptor nd = new NodeDescriptor(0, 0, 
NodeDescriptor.BT_LEAF_NODE, 1, 2);
-            CatalogNode node = new CatalogNode(nd, 8192);
+            CatalogLeafNode node = new CatalogLeafNode(nd, 8192);
             CatalogKey key = new CatalogKey(parentId, name);
             CatalogThread thread = new CatalogThread(nodeType, parentId, name);
             record = new LeafRecord(key, thread.getBytes());
@@ -185,7 +186,7 @@
         }
         return record;
     }
-    
+
     /**
      * @param parentID
      * @return the leaf record, or possibly {code null}.
@@ -200,18 +201,21 @@
                 nodeData);
         nodeData.rewind();
         byte[] data = ByteBufferUtils.toArray(nodeData);
-        CatalogNode node = new CatalogNode(data, nodeSize);
-        while (node.isIndexNode()) {
+        NodeDescriptor nd = new NodeDescriptor(nodeData.array(), 0);
+
+        while (nd.isIndexNode()) {
+            CatalogIndexNode node = new CatalogIndexNode(data, nodeSize);
             IndexRecord record = (IndexRecord) node.find(parentID);
             currentOffset = catalogHeaderNodeOffset + (record.getIndex() * 
nodeSize);
             nodeData = ByteBuffer.allocate(nodeSize);
             fs.getApi().read(currentOffset, nodeData);
             nodeData.rewind();
             data = ByteBufferUtils.toArray(nodeData);
-            node = new CatalogNode(data, nodeSize);
+            nd = new NodeDescriptor(nodeData.array(), 0);
         }
 
-        if (node.isLeafNode()) {
+        if (nd.isLeafNode()) {
+            CatalogLeafNode node = new CatalogLeafNode(data, nodeSize);
             lr = (LeafRecord) node.find(parentID);
         }
         return lr;
@@ -245,9 +249,10 @@
             int nodeSize = getBTHeaderRecord().getNodeSize();
             ByteBuffer nodeData = ByteBuffer.allocate(nodeSize);
             fs.getApi().read(catalogHeaderNodeOffset + (currentNodeNumber * 
nodeSize), nodeData);
-            CatalogNode node = new CatalogNode(nodeData.array(), nodeSize);
-            if (node.isIndexNode()) {
-                IndexRecord[] records = (IndexRecord[]) 
node.findChildren(parentID);
+            NodeDescriptor nd = new NodeDescriptor(nodeData.array(), 0);
+            if (nd.isIndexNode()) {
+                CatalogIndexNode node = new CatalogIndexNode(nodeData.array(), 
nodeSize);
+                IndexRecord[] records = (IndexRecord[]) node.findAll(parentID);
                 List<LeafRecord> lfList = new LinkedList<LeafRecord>();
                 for (IndexRecord rec : records) {
                     LeafRecord[] lfr = getRecords(parentID, rec.getIndex());
@@ -256,7 +261,8 @@
                     }
                 }
                 return lfList.toArray(new LeafRecord[lfList.size()]);
-            } else if (node.isLeafNode()) {
+            } else if (nd.isLeafNode()) {
+                CatalogLeafNode node = new CatalogLeafNode(nodeData.array(), 
nodeSize);
                 return (LeafRecord[]) node.findAll(parentID);
             } else {
                 return null;
@@ -280,19 +286,21 @@
         int nodeSize = getBTHeaderRecord().getNodeSize();
         ByteBuffer nodeData = ByteBuffer.allocate(nodeSize);
         fs.getApi().read(catalogHeaderNodeOffset + (currentNodeNumber * 
nodeSize), nodeData);
-        CatalogNode node = new CatalogNode(nodeData.array(), nodeSize);
+        NodeDescriptor nd = new NodeDescriptor(nodeData.array(), 0);
         int currentOffset = 0;
         CatalogKey cKey = new CatalogKey(parentID, nodeName);
-        while (node.isIndexNode()) {
+        while (nd.isIndexNode()) {
+            CatalogIndexNode node = new CatalogIndexNode(nodeData.array(), 
nodeSize);
             IndexRecord record = (IndexRecord) node.find(cKey);
             currentNodeNumber = record.getIndex();
             currentOffset = catalogHeaderNodeOffset + record.getIndex() * 
nodeSize;
             nodeData = ByteBuffer.allocate(nodeSize);
             fs.getApi().read(currentOffset, buffer);
-            node = new CatalogNode(nodeData.array(), nodeSize);
+            node = new CatalogIndexNode(nodeData.array(), nodeSize);
         }
         LeafRecord lr = null;
-        if (node.isLeafNode()) {
+        if (nd.isLeafNode()) {
+            CatalogLeafNode node = new CatalogLeafNode(nodeData.array(), 
nodeSize);
             lr = (LeafRecord) node.find(parentID);
         }
         return lr;

Copied: trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogIndexNode.java 
(from rev 5842, trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogNode.java)
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogIndexNode.java          
                (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogIndexNode.java  
2011-08-04 12:57:47 UTC (rev 5843)
@@ -0,0 +1,125 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2010 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, 
Inc., 
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.hfsplus.catalog;
+
+import java.util.LinkedList;
+
+import org.jnode.fs.hfsplus.tree.AbstractNode;
+import org.jnode.fs.hfsplus.tree.IndexRecord;
+import org.jnode.fs.hfsplus.tree.Key;
+import org.jnode.fs.hfsplus.tree.NodeDescriptor;
+
+public class CatalogIndexNode extends AbstractNode<IndexRecord> {
+
+    /**
+     * Create a new node.
+     * 
+     * @param descriptor
+     * @param nodeSize
+     */
+    public CatalogIndexNode(NodeDescriptor descriptor, final int nodeSize) {
+        super(descriptor, nodeSize);
+    }
+
+    /**
+     * Create node from existing data.
+     * 
+     * @param nodeData
+     * @param nodeSize
+     */
+    public CatalogIndexNode(final byte[] nodeData, final int nodeSize) {
+        super(nodeData, nodeSize);
+
+    }
+
+    @Override
+    protected void loadRecords(final byte[] nodeData) {
+        CatalogKey key;
+        int offset;
+        for (int i = 0; i < this.descriptor.getNumRecords(); i++) {
+            offset = offsets.get(i);
+            key = new CatalogKey(nodeData, offset);
+            records.add(new IndexRecord(key, nodeData, offset));
+        }
+    }
+
+    /**
+     * @param parentId
+     * @return a NodeRecord or {@code null}
+     */
+    public final IndexRecord find(final CatalogNodeId parentId) {
+        for (IndexRecord record : records) {
+            Key key = record.getKey();
+            if (key instanceof CatalogKey) {
+                if (((CatalogKey) key).getParentId().getId() == 
parentId.getId()) {
+                    return record;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Find node record based on it's key.
+     * 
+     * @param key The key to search.
+     * @return a NodeRecord or {@code null}
+     */
+    public IndexRecord find(final CatalogKey key) {
+        IndexRecord largestMatchingRecord = null;
+        for (int index = 0; index < this.getNodeDescriptor().getNumRecords(); 
index++) {
+            IndexRecord record = this.getNodeRecord(index);
+            if ((record.getKey().compareTo(key) <= 0)) {
+                if (largestMatchingRecord != null &&
+                        
record.getKey().compareTo(largestMatchingRecord.getKey()) > 0) {
+                    largestMatchingRecord = record;
+                }
+            }
+        }
+        return largestMatchingRecord;
+    }
+
+    /**
+     * @param parentId
+     * @return an array of NodeRecords
+     */
+    public final IndexRecord[] findAll(final CatalogNodeId parentId) {
+        LinkedList<IndexRecord> result = new LinkedList<IndexRecord>();
+        IndexRecord largestMatchingRecord = null;
+        CatalogKey largestMatchingKey = null;
+        for (IndexRecord record : records) {
+            CatalogKey key = (CatalogKey) record.getKey();
+            if (key.getParentId().getId() < parentId.getId() &&
+                    (largestMatchingKey == null || 
key.compareTo(largestMatchingKey) > 0)) {
+                largestMatchingKey = key;
+                largestMatchingRecord = record;
+            } else if (key.getParentId().getId() == parentId.getId()) {
+                result.addLast(record);
+            }
+        }
+
+        if (largestMatchingKey != null) {
+            result.addFirst(largestMatchingRecord);
+        }
+        return result.toArray(new IndexRecord[result.size()]);
+    }
+
+}

Added: trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogLeafNode.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogLeafNode.java           
                (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogLeafNode.java   
2011-08-04 12:57:47 UTC (rev 5843)
@@ -0,0 +1,79 @@
+package org.jnode.fs.hfsplus.catalog;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jnode.fs.hfsplus.tree.AbstractNode;
+import org.jnode.fs.hfsplus.tree.Key;
+import org.jnode.fs.hfsplus.tree.LeafRecord;
+import org.jnode.fs.hfsplus.tree.NodeDescriptor;
+import org.jnode.fs.hfsplus.tree.NodeRecord;
+
+public class CatalogLeafNode extends AbstractNode<LeafRecord> {
+    /**
+     * Create a new node.
+     * 
+     * @param descriptor
+     * @param nodeSize
+     */
+    public CatalogLeafNode(NodeDescriptor descriptor, final int nodeSize) {
+        super(descriptor, nodeSize);
+    }
+
+    /**
+     * Create node from existing data.
+     * 
+     * @param nodeData
+     * @param nodeSize
+     */
+    public CatalogLeafNode(final byte[] nodeData, final int nodeSize) {
+        super(nodeData, nodeSize);
+
+    }
+
+    @Override
+    protected void loadRecords(byte[] nodeData) {
+        CatalogKey key;
+        int offset;
+        for (int i = 0; i < this.descriptor.getNumRecords(); i++) {
+            offset = offsets.get(i);
+            key = new CatalogKey(nodeData, offset);
+            int recordSize = offsets.get(i + 1) - offset;
+            records.add(new LeafRecord(key, nodeData, offset, recordSize));
+        }
+    }
+
+    /**
+     * @param parentId
+     * @return a NodeRecord or {@code null}
+     */
+    public final LeafRecord find(final CatalogNodeId parentId) {
+        for (LeafRecord record : records) {
+            Key key = record.getKey();
+            if (key instanceof CatalogKey) {
+                if (((CatalogKey) key).getParentId().getId() == 
parentId.getId()) {
+                    return record;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * @param parentId
+     * @return an array of NodeRecords
+     */
+    public final LeafRecord[] findAll(final CatalogNodeId parentId) {
+        List<NodeRecord> list = new LinkedList<NodeRecord>();
+        for (int index = 0; index < this.getNodeDescriptor().getNumRecords(); 
index++) {
+            NodeRecord record = this.getNodeRecord(index);
+            Key key = record.getKey();
+            if (key instanceof CatalogKey &&
+                    ((CatalogKey) key).getParentId().getId() == 
parentId.getId()) {
+                list.add(record);
+            }
+        }
+        return list.toArray(new LeafRecord[list.size()]);
+    }
+
+}


Property changes on: 
trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogLeafNode.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Deleted: trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogNode.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogNode.java       
2011-08-02 20:04:52 UTC (rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogNode.java       
2011-08-04 12:57:47 UTC (rev 5843)
@@ -1,160 +0,0 @@
-/*
- * $Id$
- *
- * Copyright (C) 2003-2010 JNode.org
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation; either version 2.1 of the License, or
- * (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful, but 
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
- * License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; If not, write to the Free Software Foundation, 
Inc., 
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- 
-package org.jnode.fs.hfsplus.catalog;
-
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.jnode.fs.hfsplus.tree.AbstractNode;
-import org.jnode.fs.hfsplus.tree.IndexRecord;
-import org.jnode.fs.hfsplus.tree.Key;
-import org.jnode.fs.hfsplus.tree.LeafRecord;
-import org.jnode.fs.hfsplus.tree.NodeDescriptor;
-import org.jnode.fs.hfsplus.tree.NodeRecord;
-import org.jnode.util.BigEndian;
-
-public class CatalogNode extends AbstractNode {
-    
-    /**
-     * Create a new node.
-     * @param descriptor
-     * @param nodeSize
-     */
-    public CatalogNode(NodeDescriptor descriptor, final int nodeSize) {
-        this.descriptor = descriptor;
-        this.size = nodeSize;
-        this.records = new ArrayList<NodeRecord>(descriptor.getNumRecords());
-        this.offsets = new ArrayList<Integer>(descriptor.getNumRecords() + 1);
-        
this.offsets.add(Integer.valueOf(NodeDescriptor.BT_NODE_DESCRIPTOR_LENGTH));
-    }
-
-    /**
-     * Create node from existing data.
-     * @param nodeData
-     * @param nodeSize
-     */
-    public CatalogNode(final byte[] nodeData, final int nodeSize) {
-        this.descriptor = new NodeDescriptor(nodeData, 0);
-        this.size = nodeSize;
-        this.records = new 
ArrayList<NodeRecord>(this.descriptor.getNumRecords());
-        this.offsets = new ArrayList<Integer>(this.descriptor.getNumRecords() 
+ 1);
-        int offset;
-        for (int i = 0; i < this.descriptor.getNumRecords() + 1; i++) {
-            offset = BigEndian.getInt16(nodeData, size - ((i + 1) * 2));
-            offsets.add(Integer.valueOf(offset));
-        }
-        CatalogKey key;
-        for (int i = 0; i < this.descriptor.getNumRecords(); i++) {
-            offset = offsets.get(i);
-            key = new CatalogKey(nodeData, offset);
-            if (isIndexNode()) {
-                records.add(new IndexRecord(key, nodeData, offset));
-            } else {
-                int recordSize = offsets.get(i + 1) - offset;
-                records.add(new LeafRecord(key, nodeData, offset, recordSize));
-            }
-        }
-    }
-
-    @Override
-    public NodeRecord getNodeRecord(int index) {
-        return records.get(index);
-    }
-
-    /**
-     * @param parentId
-     * @return a NodeRecord or {@code null}
-     */
-    public final NodeRecord find(final CatalogNodeId parentId) {
-        for (NodeRecord record : records) {
-            Key key = record.getKey();
-            if (key instanceof CatalogKey) {
-                if (((CatalogKey) key).getParentId().getId() == 
parentId.getId()) {
-                    return record;
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Find node record based on it's key.
-     * 
-     * @param key The key to search.
-     * @return a NodeRecord or {@code null}
-     */
-    public NodeRecord find(final CatalogKey key) {
-        NodeRecord largestMatchingRecord = null;
-        for (int index = 0; index < this.getNodeDescriptor().getNumRecords(); 
index++) {
-            NodeRecord record = this.getNodeRecord(index);
-            if ((record.getKey().compareTo(key) <= 0)) {
-                if (largestMatchingRecord != null &&
-                        
record.getKey().compareTo(largestMatchingRecord.getKey()) > 0) {
-                    largestMatchingRecord = record;
-                }
-            }
-        }
-        return largestMatchingRecord;
-    }
-
-    /**
-     * @param parentId
-     * @return an array of NodeRecords
-     */
-    public final NodeRecord[] findChildren(final CatalogNodeId parentId) {
-        LinkedList<NodeRecord> result = new LinkedList<NodeRecord>();
-        NodeRecord largestMatchingRecord = null;
-        CatalogKey largestMatchingKey = null;
-        for (NodeRecord record : records) {
-            CatalogKey key = (CatalogKey) record.getKey();
-            if (key.getParentId().getId() < parentId.getId()
-                && (largestMatchingKey == null || 
key.compareTo(largestMatchingKey) > 0)) {
-                largestMatchingKey = key;
-                largestMatchingRecord = record;
-            } else if (key.getParentId().getId() == parentId.getId()) {
-                result.addLast(record);
-            }
-        }
-
-        if (largestMatchingKey != null) {
-            result.addFirst(largestMatchingRecord);
-        }
-        return result.toArray(new NodeRecord[result.size()]);
-    }
-
-    /**
-     * @param parentId
-     * @return an array of NodeRecords
-     */
-    public final NodeRecord[] findAll(final CatalogNodeId parentId) {
-        List<NodeRecord> list = new LinkedList<NodeRecord>();
-        for (int index = 0; index < this.getNodeDescriptor().getNumRecords(); 
index++) {
-            NodeRecord record = this.getNodeRecord(index);
-            Key key = record.getKey();
-            if (key instanceof CatalogKey && ((CatalogKey) 
key).getParentId().getId() == parentId.getId()) {
-                list.add(record);
-            }
-        }
-        return list.toArray(new NodeRecord[list.size()]);
-    }
-
-}

Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/extent/ExtentNode.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/extent/ExtentNode.java 2011-08-02 
20:04:52 UTC (rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/extent/ExtentNode.java 2011-08-04 
12:57:47 UTC (rev 5843)
@@ -17,29 +17,33 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.fs.hfsplus.extent;
 
 import org.jnode.fs.hfsplus.tree.AbstractNode;
+import org.jnode.fs.hfsplus.tree.IndexRecord;
 import org.jnode.fs.hfsplus.tree.NodeDescriptor;
-import org.jnode.fs.hfsplus.tree.NodeRecord;
 
-public class ExtentNode extends AbstractNode {
-    
+public class ExtentNode extends AbstractNode<IndexRecord> {
+
     public ExtentNode(NodeDescriptor descriptor, final int nodeSize) {
-        this.descriptor = descriptor;
-        this.size = nodeSize;
+        super(descriptor, nodeSize);
     }
-    
+
     public ExtentNode(final byte[] nodeData, final int nodeSize) {
-        this.descriptor = new NodeDescriptor(nodeData, 0);
-        this.size = nodeSize;
+        super(nodeData, nodeSize);
     }
 
     @Override
-    public NodeRecord getNodeRecord(int index) {
+    public IndexRecord getNodeRecord(int index) {
         // TODO Auto-generated method stub
         return null;
     }
 
+    @Override
+    protected void loadRecords(byte[] nodeData) {
+        // TODO Auto-generated method stub
+
+    }
+
 }

Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/AbstractNode.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/AbstractNode.java 2011-08-02 
20:04:52 UTC (rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/AbstractNode.java 2011-08-04 
12:57:47 UTC (rev 5843)
@@ -17,30 +17,46 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.fs.hfsplus.tree;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.jnode.util.BigEndian;
 
-public abstract class AbstractNode implements Node {
+public abstract class AbstractNode<T extends NodeRecord> implements Node<T> {
     protected NodeDescriptor descriptor;
-    protected List<NodeRecord> records;
+    protected List<T> records;
     protected List<Integer> offsets;
     protected int size;
 
-    @Override
-    public NodeDescriptor getNodeDescriptor() {
-        return descriptor;
+    public AbstractNode(NodeDescriptor descriptor, final int nodeSize) {
+        this.descriptor = descriptor;
+        this.size = nodeSize;
+        this.records = new ArrayList<T>(descriptor.getNumRecords());
+        this.offsets = new ArrayList<Integer>(descriptor.getNumRecords() + 1);
+        
this.offsets.add(Integer.valueOf(NodeDescriptor.BT_NODE_DESCRIPTOR_LENGTH));
     }
 
-    public boolean isIndexNode() {
-        return this.getNodeDescriptor().getKind() == 
NodeDescriptor.BT_INDEX_NODE;
+    public AbstractNode(final byte[] nodeData, final int nodeSize) {
+        this.descriptor = new NodeDescriptor(nodeData, 0);
+        this.size = nodeSize;
+        this.records = new ArrayList<T>(this.descriptor.getNumRecords());
+        this.offsets = new ArrayList<Integer>(this.descriptor.getNumRecords() 
+ 1);
+        int offset;
+        for (int i = 0; i < this.descriptor.getNumRecords() + 1; i++) {
+            offset = BigEndian.getInt16(nodeData, size - ((i + 1) * 2));
+            offsets.add(Integer.valueOf(offset));
+        }
+        loadRecords(nodeData);
     }
 
-    public boolean isLeafNode() {
-        return this.getNodeDescriptor().getKind() == 
NodeDescriptor.BT_LEAF_NODE;
+    protected abstract void loadRecords(final byte[] nodeData);
+
+    @Override
+    public NodeDescriptor getNodeDescriptor() {
+        return descriptor;
     }
 
     @Override
@@ -49,10 +65,12 @@
     }
 
     @Override
-    public abstract NodeRecord getNodeRecord(int index);
+    public T getNodeRecord(int index) {
+        return records.get(index);
+    }
 
     @Override
-    public boolean addNodeRecord(NodeRecord record) {
+    public boolean addNodeRecord(T record) {
         int freeSpace = getFreeSize();
         if (freeSpace < record.getSize() + 2) {
             return false;
@@ -107,7 +125,7 @@
 
     public String toString() {
         StringBuffer b = new StringBuffer();
-        b.append((this.isLeafNode()) ? "Leaf node" : "Index 
node").append("\n");
+        b.append((this.getNodeDescriptor().isLeafNode()) ? "Leaf node" : 
"Index node").append("\n");
         b.append(this.getNodeDescriptor().toString()).append("\n");
         b.append("Offsets : ").append(offsets.toString());
         return b.toString();

Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/Node.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/Node.java 2011-08-02 20:04:52 UTC 
(rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/Node.java 2011-08-04 12:57:47 UTC 
(rev 5843)
@@ -17,22 +17,18 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.fs.hfsplus.tree;
 
-public interface Node {
+public interface Node<T extends NodeRecord> {
 
     public static final int OFFSET_SIZE = 2;
 
     public NodeDescriptor getNodeDescriptor();
 
-    public boolean isIndexNode();
-
-    public boolean isLeafNode();
-
     public int getRecordOffset(int index);
 
-    public NodeRecord getNodeRecord(int index);
+    public T getNodeRecord(int index);
 
     /**
      * Insert a record in the node.
@@ -41,5 +37,5 @@
      * @return True if record is correctly inserted, false if there is not
      *         enough place to insert the record.
      */
-    public boolean addNodeRecord(NodeRecord record);
+    public boolean addNodeRecord(T record);
 }

Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/NodeDescriptor.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/NodeDescriptor.java       
2011-08-02 20:04:52 UTC (rev 5842)
+++ trunk/fs/src/fs/org/jnode/fs/hfsplus/tree/NodeDescriptor.java       
2011-08-04 12:57:47 UTC (rev 5843)
@@ -17,7 +17,7 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.fs.hfsplus.tree;
 
 import org.jnode.util.BigEndian;
@@ -27,22 +27,22 @@
     public static final int BT_INDEX_NODE = 0;
     public static final int BT_HEADER_NODE = 1;
     public static final int BT_MAP_NODE = 2;
-    
+
     /** The size of the node descriptor. */
     public static final int BT_NODE_DESCRIPTOR_LENGTH = 14;
-    
+
     /** The number of the next node. */
     private int fLink;
-    
+
     /** The number of the previous node. */
     private int bLink;
-    
+
     /** The type of the node. */
     private int kind;
-    
+
     /** The depth of this node in the B-Tree. */
     private int height;
-    
+
     /** The number of records in this node. */
     private int numRecords;
 
@@ -118,4 +118,16 @@
         return numRecords;
     }
 
+    public boolean isIndexNode() {
+        return kind == NodeDescriptor.BT_INDEX_NODE;
+    }
+
+    public boolean isLeafNode() {
+        return kind == NodeDescriptor.BT_LEAF_NODE;
+    }
+
+    public boolean isMapNode() {
+        return kind == NodeDescriptor.BT_MAP_NODE;
+    }
+
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts. 
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
Jnode-svn-commits mailing list
Jnode-svn-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits

Reply via email to