1 2 17 18 19 package org.apache.poi.hwpf.sprm; 20 21 import java.util.List ; 22 23 import org.apache.poi.util.LittleEndian; 24 25 26 public class SprmUtils 27 { 28 public SprmUtils() 29 { 30 } 31 32 public static byte[] shortArrayToByteArray(short[] convert) 33 { 34 byte[] buf = new byte[convert.length * LittleEndian.SHORT_SIZE]; 35 36 for (int x = 0; x < convert.length; x++) 37 { 38 LittleEndian.putShort(buf, x * LittleEndian.SHORT_SIZE, convert[x]); 39 } 40 41 return buf; 42 } 43 44 public static int addSpecialSprm(short instruction, byte[] varParam, List list) 45 { 46 byte[] sprm = new byte[varParam.length + 4]; 47 System.arraycopy(varParam, 0, sprm, 4, varParam.length); 48 LittleEndian.putShort(sprm, instruction); 49 LittleEndian.putShort(sprm, 2, (short)(varParam.length + 1)); 50 list.add(sprm); 51 return sprm.length; 52 } 53 54 public static int addSprm(short instruction, int param, byte[] varParam, List list) 55 { 56 int type = (instruction & 0xe000) >> 13; 57 58 byte[] sprm = null; 59 switch(type) 60 { 61 case 0: 62 case 1: 63 sprm = new byte[3]; 64 sprm[2] = (byte)param; 65 break; 66 case 2: 67 sprm = new byte[4]; 68 LittleEndian.putShort(sprm, 2, (short)param); 69 break; 70 case 3: 71 sprm = new byte[6]; 72 LittleEndian.putInt(sprm, 2, param); 73 break; 74 case 4: 75 case 5: 76 sprm = new byte[4]; 77 LittleEndian.putShort(sprm, 2, (short)param); 78 break; 79 case 6: 80 sprm = new byte[3 + varParam.length]; 81 sprm[2] = (byte)varParam.length; 82 System.arraycopy(varParam, 0, sprm, 3, varParam.length); 83 break; 84 case 7: 85 sprm = new byte[5]; 86 byte[] temp = new byte[4]; 88 LittleEndian.putInt(temp, 0, param); 89 System.arraycopy(temp, 0, sprm, 2, 3); 90 break; 91 default: 92 break; 94 } 95 LittleEndian.putShort(sprm, 0, instruction); 96 list.add(sprm); 97 return sprm.length; 98 } 99 100 public static byte[] getGrpprl(List sprmList, int size) 101 { 102 byte[] grpprl = new byte[size]; 104 int listSize = sprmList.size() - 1; 105 int index = 0; 106 for (; listSize >= 0; listSize--) 107 { 108 byte[] sprm = (byte[])sprmList.remove(0); 109 System.arraycopy(sprm, 0, grpprl, index, sprm.length); 110 index += sprm.length; 111 } 112 113 return grpprl; 114 115 } 116 117 public static int convertBrcToInt(short[] brc) 118 { 119 byte[] buf = new byte[4]; 120 LittleEndian.putShort(buf, brc[0]); 121 LittleEndian.putShort(buf, LittleEndian.SHORT_SIZE, brc[1]); 122 return LittleEndian.getInt(buf); 123 } 124 } 125 | Popular Tags |