001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hwpf.sprm;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: import java.util.Arrays;
023:
024: public class SprmBuffer implements Cloneable {
025: byte[] _buf;
026: int _offset;
027: boolean _istd;
028:
029: public SprmBuffer(byte[] buf, boolean istd) {
030: _offset = buf.length;
031: _buf = buf;
032: _istd = istd;
033: }
034:
035: public SprmBuffer(byte[] buf) {
036: this (buf, false);
037: }
038:
039: public SprmBuffer() {
040: _buf = new byte[4];
041: _offset = 0;
042: }
043:
044: private int findSprm(short opcode) {
045: int operation = SprmOperation.getOperationFromOpcode(opcode);
046: int type = SprmOperation.getTypeFromOpcode(opcode);
047:
048: SprmIterator si = new SprmIterator(_buf, 2);
049: while (si.hasNext()) {
050: SprmOperation i = si.next();
051: if (i.getOperation() == operation && i.getType() == type)
052: return i.getGrpprlOffset();
053: }
054: return -1;
055: }
056:
057: public void updateSprm(short opcode, byte operand) {
058: int grpprlOffset = findSprm(opcode);
059: if (grpprlOffset != -1) {
060: _buf[grpprlOffset] = operand;
061: return;
062: } else
063: addSprm(opcode, operand);
064: }
065:
066: public void updateSprm(short opcode, short operand) {
067: int grpprlOffset = findSprm(opcode);
068: if (grpprlOffset != -1) {
069: LittleEndian.putShort(_buf, grpprlOffset, operand);
070: return;
071: } else
072: addSprm(opcode, operand);
073: }
074:
075: public void updateSprm(short opcode, int operand) {
076: int grpprlOffset = findSprm(opcode);
077: if (grpprlOffset != -1) {
078: LittleEndian.putInt(_buf, grpprlOffset, operand);
079: return;
080: } else
081: addSprm(opcode, operand);
082: }
083:
084: public void addSprm(short opcode, byte operand) {
085: int addition = LittleEndian.SHORT_SIZE + LittleEndian.BYTE_SIZE;
086: ensureCapacity(addition);
087: LittleEndian.putShort(_buf, _offset, opcode);
088: _offset += LittleEndian.SHORT_SIZE;
089: _buf[_offset++] = operand;
090: }
091:
092: public void addSprm(short opcode, short operand) {
093: int addition = LittleEndian.SHORT_SIZE
094: + LittleEndian.SHORT_SIZE;
095: ensureCapacity(addition);
096: LittleEndian.putShort(_buf, _offset, opcode);
097: _offset += LittleEndian.SHORT_SIZE;
098: LittleEndian.putShort(_buf, _offset, operand);
099: _offset += LittleEndian.SHORT_SIZE;
100: }
101:
102: public void addSprm(short opcode, int operand) {
103: int addition = LittleEndian.SHORT_SIZE + LittleEndian.INT_SIZE;
104: ensureCapacity(addition);
105: LittleEndian.putShort(_buf, _offset, opcode);
106: _offset += LittleEndian.SHORT_SIZE;
107: LittleEndian.putInt(_buf, _offset, operand);
108: _offset += LittleEndian.INT_SIZE;
109: }
110:
111: public void addSprm(short opcode, byte[] operand) {
112: int addition = LittleEndian.SHORT_SIZE + LittleEndian.BYTE_SIZE
113: + operand.length;
114: ensureCapacity(addition);
115: LittleEndian.putShort(_buf, _offset, opcode);
116: _offset += LittleEndian.SHORT_SIZE;
117: _buf[_offset++] = (byte) operand.length;
118: System.arraycopy(operand, 0, _buf, _offset, operand.length);
119: }
120:
121: public byte[] toByteArray() {
122: return _buf;
123: }
124:
125: public boolean equals(Object obj) {
126: SprmBuffer sprmBuf = (SprmBuffer) obj;
127: return (Arrays.equals(_buf, sprmBuf._buf));
128: }
129:
130: public void append(byte[] grpprl) {
131: ensureCapacity(grpprl.length);
132: System.arraycopy(grpprl, 0, _buf, _offset, grpprl.length);
133: }
134:
135: public Object clone() throws CloneNotSupportedException {
136: SprmBuffer retVal = (SprmBuffer) super .clone();
137: retVal._buf = new byte[_buf.length];
138: System.arraycopy(_buf, 0, retVal._buf, 0, _buf.length);
139: return retVal;
140: }
141:
142: private void ensureCapacity(int addition) {
143: if (_offset + addition >= _buf.length) {
144: // add 6 more than they need for use the next iteration
145: byte[] newBuf = new byte[_offset + addition + 6];
146: System.arraycopy(_buf, 0, newBuf, 0, _buf.length);
147: _buf = newBuf;
148: }
149: }
150: }
|