1 2 17 18 19 package org.apache.poi.hwpf.sprm; 20 21 import org.apache.poi.util.LittleEndian; 22 23 import java.util.Arrays ; 24 25 public class SprmBuffer 26 implements Cloneable 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 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 clone() 150 throws CloneNotSupportedException 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 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 |