Hi,
I get an EOFException here. ConstantPool.dump() does not write the null
entries in Constant[], which is fine, but it writes the size of the
array first so the ConstantPool constructior gets confused.
@Test public void serializeConstantPool()throws IOException {
ConstantPoolGen cpg =new ConstantPoolGen(); cpg.addMethodref("apa.banan.Klass", "aMethod", "()V"); cpg.addMethodref("apa.banan.Klass2", "aMethod2", "()V"); ConstantPool cp = cpg.getConstantPool(); FileOutputStream file =new FileOutputStream("/tmp/rolf"); DataOutputStream dataOutputStream =new DataOutputStream(file); cp.dump(dataOutputStream); dataOutputStream.flush(); dataOutputStream.close(); InputStream input =new FileInputStream("/tmp/rolf"); DataInputStream inst =new DataInputStream(input); //int constant_pool_count = inst.readUnsignedShort();
//System.out.println("Stupid cp thinks it has nbr elements " +
constant_pool_count); // 256 ConstantPool loadedConstantPool =new ConstantPool(inst); inst.close(); }
Would you be ok with this patch?
/**
* Dump constant pool to file stream in binary format.
*
* @paramfileOutput file stream
* @throwsIOException
*/
publicvoiddump( finalDataOutputStreamfile) throwsIOException{
intnbrToWrite= 0;
for(inti= 1; i < constant_pool.length; i++) {
if(constant_pool[i] != null) {
nbrToWrite++;
}
}
file.writeShort(nbrToWrite);
for(inti= 1; i < constantPool.length; i++) {
if(constantPool[i] != null) {
constantPool[i].dump(file);
}
}
}
(magicator) 15:26:03 of 🐴 :commons-bcel ehsmeng> git diff
diff --git a/src/main/java/org/apache/bcel/classfile/ConstantPool.java
b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
index 3350135a..5a058b7e 100644
--- a/src/main/java/org/apache/bcel/classfile/ConstantPool.java
+++ b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
@@ -218,7 +218,14 @@ public class ConstantPool implements Cloneable, Node {
* @throws IOException
*/
public void dump( final DataOutputStream file ) throws IOException {
- file.writeShort(constantPool.length);
+ int nbrToWrite = 0;
+ for (int i = 1; i < constant_pool.length; i++) {
+ if (constant_pool[i] != null) {
+ nbrToWrite++;
+ }
+ }
+
+ file.writeShort(nbrToWrite);
for (int i = 1; i < constantPool.length; i++) {
if (constantPool[i] != null) {
constantPool[i].dump(file);
(magicator) 15:26:05 of 🐴 :commons-bcel ehsmeng>
Kind regards,
Marcus