1.1 --- a/src/share/classes/com/sun/tools/classfile/ConstantPool.java Thu Jul 30 10:30:34 2009 +0100 1.2 +++ b/src/share/classes/com/sun/tools/classfile/ConstantPool.java Thu Jul 30 07:48:24 2009 -0700 1.3 @@ -25,7 +25,9 @@ 1.4 1.5 package com.sun.tools.classfile; 1.6 1.7 +import java.io.DataOutputStream; 1.8 import java.io.IOException; 1.9 +import java.io.OutputStream; 1.10 import java.util.Iterator; 1.11 1.12 /** 1.13 @@ -179,6 +181,16 @@ 1.14 return pool.length; 1.15 } 1.16 1.17 + public int byteLength() { 1.18 + int length = 2; 1.19 + for (int i = 1; i < size(); ) { 1.20 + CPInfo cpInfo = pool[i]; 1.21 + length += cpInfo.byteLength(); 1.22 + i += cpInfo.size(); 1.23 + } 1.24 + return length; 1.25 + } 1.26 + 1.27 public CPInfo get(int index) throws InvalidIndex { 1.28 if (index <= 0 || index >= pool.length) 1.29 throw new InvalidIndex(index); 1.30 @@ -291,6 +303,8 @@ 1.31 return 1; 1.32 } 1.33 1.34 + public abstract int byteLength(); 1.35 + 1.36 public abstract <R,D> R accept(Visitor<R,D> visitor, D data); 1.37 1.38 protected final ConstantPool cp; 1.39 @@ -315,6 +329,10 @@ 1.40 return tag; 1.41 } 1.42 1.43 + public int byteLength() { 1.44 + return 5; 1.45 + } 1.46 + 1.47 public CONSTANT_Class_info getClassInfo() throws ConstantPoolException { 1.48 return cp.getClassInfo(class_index); 1.49 } 1.50 @@ -347,6 +365,10 @@ 1.51 return CONSTANT_Class; 1.52 } 1.53 1.54 + public int byteLength() { 1.55 + return 3; 1.56 + } 1.57 + 1.58 public String getName() throws ConstantPoolException { 1.59 return cp.getUTF8Value(name_index); 1.60 } 1.61 @@ -390,6 +412,10 @@ 1.62 return CONSTANT_Double; 1.63 } 1.64 1.65 + public int byteLength() { 1.66 + return 9; 1.67 + } 1.68 + 1.69 @Override 1.70 public int size() { 1.71 return 2; 1.72 @@ -439,6 +465,10 @@ 1.73 return CONSTANT_Float; 1.74 } 1.75 1.76 + public int byteLength() { 1.77 + return 5; 1.78 + } 1.79 + 1.80 @Override 1.81 public String toString() { 1.82 return "CONSTANT_Float_info[value: " + value + "]"; 1.83 @@ -464,6 +494,10 @@ 1.84 return CONSTANT_Integer; 1.85 } 1.86 1.87 + public int byteLength() { 1.88 + return 5; 1.89 + } 1.90 + 1.91 @Override 1.92 public String toString() { 1.93 return "CONSTANT_Integer_info[value: " + value + "]"; 1.94 @@ -513,6 +547,10 @@ 1.95 return 2; 1.96 } 1.97 1.98 + public int byteLength() { 1.99 + return 9; 1.100 + } 1.101 + 1.102 @Override 1.103 public String toString() { 1.104 return "CONSTANT_Long_info[value: " + value + "]"; 1.105 @@ -561,6 +599,10 @@ 1.106 return CONSTANT_NameAndType; 1.107 } 1.108 1.109 + public int byteLength() { 1.110 + return 5; 1.111 + } 1.112 + 1.113 public String getName() throws ConstantPoolException { 1.114 return cp.getUTF8Value(name_index); 1.115 } 1.116 @@ -597,6 +639,10 @@ 1.117 return CONSTANT_String; 1.118 } 1.119 1.120 + public int byteLength() { 1.121 + return 3; 1.122 + } 1.123 + 1.124 public String getString() throws ConstantPoolException { 1.125 return cp.getUTF8Value(string_index); 1.126 } 1.127 @@ -626,6 +672,20 @@ 1.128 return CONSTANT_Utf8; 1.129 } 1.130 1.131 + public int byteLength() { 1.132 + class SizeOutputStream extends OutputStream { 1.133 + @Override 1.134 + public void write(int b) throws IOException { 1.135 + size++; 1.136 + } 1.137 + int size; 1.138 + } 1.139 + SizeOutputStream sizeOut = new SizeOutputStream(); 1.140 + DataOutputStream out = new DataOutputStream(sizeOut); 1.141 + try { out.writeUTF(value); } catch (IOException ignore) { } 1.142 + return 1 + sizeOut.size; 1.143 + } 1.144 + 1.145 @Override 1.146 public String toString() { 1.147 if (value.length() < 32 && isPrintableAscii(value))