1 16 17 package org.apache.poi.hpsf; 18 19 import org.apache.poi.util.LittleEndian; 20 21 30 public class TypeReader 31 { 32 33 48 public static Object read(final byte[] src, int offset, int length, 49 final int type) 50 { 51 54 Object value; 55 length = length - LittleEndian.INT_SIZE; 56 switch (type) 57 { 58 case Variant.VT_EMPTY: 59 { 60 65 value = null; 66 break; 67 } 68 case Variant.VT_I2: 69 { 70 74 value = new Integer (LittleEndian.getUShort(src, offset)); 75 break; 76 } 77 case Variant.VT_I4: 78 { 79 83 value = new Long (LittleEndian.getUInt(src, offset)); 84 break; 85 } 86 case Variant.VT_FILETIME: 87 { 88 92 final long low = LittleEndian.getUInt(src, offset); 93 offset += LittleEndian.INT_SIZE; 94 final long high = LittleEndian.getUInt(src, offset); 95 value = Util.filetimeToDate((int) high, (int) low); 96 break; 97 } 98 case Variant.VT_LPSTR: 99 { 100 110 final int first = offset + LittleEndian.INT_SIZE; 111 long last = first + LittleEndian.getUInt(src, offset) - 1; 112 offset += LittleEndian.INT_SIZE; 113 while (src[(int) last] == 0 && first <= last) 114 last--; 115 value = new String (src, (int) first, (int) (last - first + 1)); 116 break; 117 } 118 case Variant.VT_LPWSTR: 119 { 120 125 final int first = offset + LittleEndian.INT_SIZE; 126 long last = first + LittleEndian.getUInt(src, offset) - 1; 127 long l = last - first; 128 offset += LittleEndian.INT_SIZE; 129 StringBuffer b = new StringBuffer ((int) (last - first)); 130 for (int i = 0; i <= l; i++) 131 { 132 final int i1 = offset + (i * 2); 133 final int i2 = i1 + 1; 134 b.append((char) ((src[i2] << 8) + src[i1])); 135 } 136 137 while (b.charAt(b.length() - 1) == 0x00) 138 b.setLength(b.length() - 1); 139 value = b.toString(); 140 break; 141 } 142 case Variant.VT_CF: 143 { 144 final byte[] v = new byte[length]; 145 for (int i = 0; i < length; i++) 146 v[i] = src[(int) (offset + i)]; 147 value = v; 148 break; 149 } 150 case Variant.VT_BOOL: 151 { 152 157 long bool = LittleEndian.getUInt(src, offset); 159 if (bool != 0) 160 value = new Boolean (true); 161 else 162 value = new Boolean (false); 163 break; 164 } 165 default: 166 { 167 final byte[] v = new byte[length]; 168 for (int i = 0; i < length; i++) 169 v[i] = src[(int) (offset + i)]; 170 value = v; 171 break; 172 } 173 } 174 return value; 175 } 176 177 } 178 | Popular Tags |