gpt4 book ai didi

java - 从 POF 流读取数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:53 24 4
gpt4 key购买 nike

有没有办法知道 POF 流中数组的元素数量?

我需要使用 PofReader 来读取数组。但是,我不知道如何获得大小以匹配 POF 流中的元素数量。

public class BasicTypesPofSerializer implements PofSerializer {

private static final int ARRAY_SIZE = 10;

private static final int ID = 0;

private static final int BOOLEAN1 = 1;
private static final int BOOLEAN2 = 2;
private static final int BOOLEAN3 = 3;

private static final int BYTE1 = 4;
private static final int BYTE2 = 5;
private static final int BYTE3 = 6;

private static final int SHORT1 = 7;
private static final int SHORT2 = 8;
private static final int SHORT3 = 9;

private static final int INT1 = 10;
private static final int INT2 = 11;
private static final int INT3 = 12;

private static final int FLOAT1 = 13;
private static final int FLOAT2 = 14;
private static final int FLOAT3 = 15;

private static final int DOUBLE1 = 16;
private static final int DOUBLE2 = 17;
private static final int DOUBLE3 = 18;

private static final int NUMBER1 = 19;
private static final int NUMBER2 = 20;

private static final int CHAR1 = 22;
private static final int CHAR2 = 23;
private static final int CHAR3 = 24;

private static final int STRING1 = 25;
private static final int STRING2 = 26;

private static final int DATE1 = 28;
private static final int DATE2 = 29;

private static final int OBJECT1 = 31;
private static final int OBJECT2 = 32;

private static final int CLASS1 = 34;
private static final int CLASS2 = 35;

@Override
public Object deserialize(PofReader reader) throws IOException {

BasicTypes basic = new BasicTypes();

basic.number2 = (Number[]) reader.readObjectArray(NUMBER2, new Number[ARRAY_SIZE]);
basic.string2 = (String[]) reader.readObjectArray(STRING2, new String[ARRAY_SIZE]);
basic.obj2 = (Serializable[]) reader.readObjectArray(OBJECT2, new Serializable[ARRAY_SIZE]);

reader.readRemainder();
return basic;
}
}

正如您在此代码示例中所看到的,我有几个数组。每个元素中的元素数量可以相同也可以不同。我需要知道一种方法来使大小与元素数量匹配,这样我就不会得到元素被切断或空位置。有什么办法可以做到这一点吗?

最佳答案

根据documentation ,您甚至不必知道尺寸。你可以这样做:

@Override
public Object deserialize(PofReader reader) throws IOException {

BasicTypes basic = new BasicTypes();

basic.number2 = (Number[]) reader.readObjectArray(NUMBER2, new Number[0]);
basic.string2 = (String[]) reader.readObjectArray(STRING2, new String[0]);
basic.obj2 = (Serializable[]) reader.readObjectArray(OBJECT2, new Serializable[0]);

reader.readRemainder();
return basic;
}
}

我认为你甚至可以尝试传递 null。

关于java - 从 POF 流读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24310205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com