有没有办法知道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;
}
}
正如您在此代码示例中所看到的,我有几个数组。每个元素的数量可以相同或不同。我需要知道一种方法来获得大小匹配元素的数量,所以我不会得到元素切断或空位置。 有没有办法做到这一点?
答案 0 :(得分:1)
根据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。