01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.internal;
22:
23: import com.db4o.foundation.Tree;
24:
25: /**
26: * @exclude
27: */
28: public final class TreeReader {
29: private final Readable i_template;
30: private final Buffer i_bytes;
31: private int i_current = 0;
32: private int i_levels = 0;
33: private int i_size;
34: private boolean i_orderOnRead;
35:
36: public TreeReader(Buffer a_bytes, Readable a_template) {
37: this (a_bytes, a_template, false);
38: }
39:
40: public TreeReader(Buffer a_bytes, Readable a_template,
41: boolean a_orderOnRead) {
42: i_template = a_template;
43: i_bytes = a_bytes;
44: i_orderOnRead = a_orderOnRead;
45: }
46:
47: public Tree read() {
48: return read(i_bytes.readInt());
49: }
50:
51: public Tree read(int a_size) {
52: i_size = a_size;
53: if (i_size > 0) {
54: if (i_orderOnRead) {
55: Tree tree = null;
56: for (int i = 0; i < i_size; i++) {
57: tree = Tree.add(tree, (Tree) i_template
58: .read(i_bytes));
59: }
60: return tree;
61: }
62: while ((1 << i_levels) < (i_size + 1)) {
63: i_levels++;
64: }
65: return linkUp(null, i_levels);
66: }
67: return null;
68: }
69:
70: private final Tree linkUp(Tree a_preceding, int a_level) {
71: Tree node = (Tree) i_template.read(i_bytes);
72: i_current++;
73: node._preceding = a_preceding;
74: node._subsequent = linkDown(a_level + 1);
75: node.calculateSize();
76: if (i_current < i_size) {
77: return linkUp(node, a_level - 1);
78: }
79: return node;
80:
81: }
82:
83: private final Tree linkDown(int a_level) {
84: if (i_current < i_size) {
85: i_current++;
86: if (a_level < i_levels) {
87: Tree preceding = linkDown(a_level + 1);
88: Tree node = (Tree) i_template.read(i_bytes);
89: node._preceding = preceding;
90: node._subsequent = linkDown(a_level + 1);
91: node.calculateSize();
92: return node;
93: }
94: return (Tree) i_template.read(i_bytes);
95: }
96: return null;
97: }
98: }
|