KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > sprm > SprmUtils


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hwpf.sprm;
20
21 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc 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         // this is a three byte int so it has to be handled special
87
byte[] temp = new byte[4];
88         LittleEndian.putInt(temp, 0, param);
89         System.arraycopy(temp, 0, sprm, 2, 3);
90         break;
91       default:
92         //should never happen
93
break;
94     }
95     LittleEndian.putShort(sprm, 0, instruction);
96     list.add(sprm);
97     return sprm.length;
98   }
99
100   public static byte[] getGrpprl(List JavaDoc sprmList, int size)
101   {
102     // spit out the final grpprl
103
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