KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.poi.util.LittleEndian;
22
23 import java.util.Arrays JavaDoc;
24
25 public class SprmBuffer
26   implements Cloneable JavaDoc
27 {
28   byte[] _buf;
29   int _offset;
30   boolean _istd;
31
32   public SprmBuffer(byte[] buf, boolean istd)
33   {
34     _offset = buf.length;
35     _buf = buf;
36     _istd = istd;
37   }
38   public SprmBuffer(byte[] buf)
39   {
40     this(buf, false);
41   }
42   public SprmBuffer()
43   {
44     _buf = new byte[4];
45     _offset = 0;
46   }
47
48   private int findSprm(short opcode)
49   {
50     int operation = SprmOperation.getOperationFromOpcode(opcode);
51     int type = SprmOperation.getTypeFromOpcode(opcode);
52
53     SprmIterator si = new SprmIterator(_buf, 2);
54     while(si.hasNext())
55     {
56       SprmOperation i = si.next();
57       if(i.getOperation() == operation && i.getType() == type)
58         return i.getGrpprlOffset();
59     }
60     return -1;
61   }
62
63   public void updateSprm(short opcode, byte operand)
64   {
65     int grpprlOffset = findSprm(opcode);
66     if(grpprlOffset != -1)
67     {
68       _buf[grpprlOffset] = operand;
69       return;
70     }
71     else addSprm(opcode, operand);
72   }
73
74   public void updateSprm(short opcode, short operand)
75   {
76     int grpprlOffset = findSprm(opcode);
77     if(grpprlOffset != -1)
78     {
79       LittleEndian.putShort(_buf, grpprlOffset, operand);
80       return;
81     }
82     else addSprm(opcode, operand);
83   }
84
85   public void updateSprm(short opcode, int operand)
86   {
87     int grpprlOffset = findSprm(opcode);
88     if(grpprlOffset != -1)
89     {
90       LittleEndian.putInt(_buf, grpprlOffset, operand);
91       return;
92     }
93     else addSprm(opcode, operand);
94   }
95
96   public void addSprm(short opcode, byte operand)
97   {
98     int addition = LittleEndian.SHORT_SIZE + LittleEndian.BYTE_SIZE;
99     ensureCapacity(addition);
100     LittleEndian.putShort(_buf, _offset, opcode);
101     _offset += LittleEndian.SHORT_SIZE;
102     _buf[_offset++] = operand;
103   }
104   public void addSprm(short opcode, short operand)
105   {
106     int addition = LittleEndian.SHORT_SIZE + LittleEndian.SHORT_SIZE;
107     ensureCapacity(addition);
108     LittleEndian.putShort(_buf, _offset, opcode);
109     _offset += LittleEndian.SHORT_SIZE;
110     LittleEndian.putShort(_buf, _offset, operand);
111     _offset += LittleEndian.SHORT_SIZE;
112   }
113   public void addSprm(short opcode, int operand)
114   {
115     int addition = LittleEndian.SHORT_SIZE + LittleEndian.INT_SIZE;
116     ensureCapacity(addition);
117     LittleEndian.putShort(_buf, _offset, opcode);
118     _offset += LittleEndian.SHORT_SIZE;
119     LittleEndian.putInt(_buf, _offset, operand);
120     _offset += LittleEndian.INT_SIZE;
121   }
122   public void addSprm(short opcode, byte[] operand)
123   {
124     int addition = LittleEndian.SHORT_SIZE + LittleEndian.BYTE_SIZE + operand.length;
125     ensureCapacity(addition);
126     LittleEndian.putShort(_buf, _offset, opcode);
127     _offset += LittleEndian.SHORT_SIZE;
128     _buf[_offset++] = (byte)operand.length;
129     System.arraycopy(operand, 0, _buf, _offset, operand.length);
130   }
131
132   public byte[] toByteArray()
133   {
134     return _buf;
135   }
136
137   public boolean equals(Object JavaDoc obj)
138   {
139     SprmBuffer sprmBuf = (SprmBuffer)obj;
140     return (Arrays.equals(_buf, sprmBuf._buf));
141   }
142
143   public void append(byte[] grpprl)
144   {
145     ensureCapacity(grpprl.length);
146     System.arraycopy(grpprl, 0, _buf, _offset, grpprl.length);
147   }
148
149   public Object JavaDoc clone()
150     throws CloneNotSupportedException JavaDoc
151   {
152     SprmBuffer retVal = (SprmBuffer)super.clone();
153     retVal._buf = new byte[_buf.length];
154     System.arraycopy(_buf, 0, retVal._buf, 0, _buf.length);
155     return retVal;
156   }
157
158   private void ensureCapacity(int addition)
159   {
160     if (_offset + addition >= _buf.length)
161     {
162       // add 6 more than they need for use the next iteration
163
byte[] newBuf = new byte[_offset + addition + 6];
164       System.arraycopy(_buf, 0, newBuf, 0, _buf.length);
165       _buf = newBuf;
166     }
167   }
168 }
169
Popular Tags