我有我用来构建索引RangeTreeIndex
的RangeTreeNode
类,在插入 2500 条记录之后,我试图将RangeTreeIndex
对象保存在我正在接收的文件中StackOverflowError
,下面是我的整个代码:
public class RangeTreeNode<T> implements Serializable {
private static final long serialVersionUID = -1;
public T key;
public Object data;
public RangeTreeNode<T> left;
public RangeTreeNode<T> right;
public RangeTreeNode() {
}
}
public class RangeTreeIndex implements Serializable {
private RangeTreeNode<Number> root;
private RangeTreeNode<Number> firstLeafNode;
private RangeTreeNode<Number> lastLeafNode;
private Number min;
private Number max;
public RangeTreeIndex() {
}
@Override
public int insert(Number key, Object values) {
Number newKey = new Double(key.doubleValue());
if (newKey.doubleValue() < min.doubleValue()) {
min = new Double(key.doubleValue());
}
if (newKey.doubleValue() > max.doubleValue()) {
max = new Double(key.doubleValue());
}
RangeTreeNode<Number> newNode = new RangeTreeNode<>(newKey, values);
this.root = this.insert(root, newNode);
if (firstLeafNode == null && lastLeafNode == null) {
firstLeafNode = newNode;
lastLeafNode = newNode;
} else if (newNode.key.doubleValue() < firstLeafNode.key.doubleValue()) {
firstLeafNode = newNode;
} else if (newNode.key.doubleValue() > lastLeafNode.key.doubleValue()) {
lastLeafNode = newNode;
}
return 1;
}
private RangeTreeNode<Number> insert(RangeTreeNode<Number> parentNode, RangeTreeNode<Number> newNode) {
if (parentNode == null) {
parentNode = newNode;
} else {
if (newNode.key.doubleValue() <= parentNode.key.doubleValue()) {
parentNode.left = insert(parentNode.left, newNode);
} else {
parentNode.right = insert(parentNode.right, newNode);
}
}
return parentNode;
}
}
public class Main {
public static void main(String args[]) throws IOException {
String filePath = "XYZ.jobject";
try (RandomAccessFile raf = new RandomAccessFile(filePath, "rw");
FileOutputStream fOut = new FileOutputStream(raf.getFD());
BufferedOutputStream bos = new BufferedOutputStream(fOut);
ObjectOutputStream oOut = new ObjectOutputStream(bos)) {
oOut.writeObject(preferenceIndex);
}
}
}
以下是我收到的例外情况:
Exception in thread "Thread-5" java.lang.StackOverflowError
at java.lang.Double.doubleToLongBits(Double.java:836)
at java.io.Bits.putDouble(Bits.java:121)
at java.io.ObjectStreamClass$FieldReflector.getPrimFieldValues(ObjectStreamClass.java:2168)
at java.io.ObjectStreamClass.getPrimFieldValues(ObjectStreamClass.java:1389)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1533)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
...
...
...
...
即使我使用仅传递可序列化对象的Apache commons-lang
库类,它也会引发相同的错误。SerializationUtils.serialize
任何人都可以用原始值来面对这个问题吗?