1 2 17 18 package org.apache.poi.hwpf.model; 19 20 import java.util.ArrayList ; 21 22 import org.apache.poi.util.LittleEndian; 23 24 25 26 33 public class PlexOfCps 34 { 35 private int _count; 36 private int _offset; 37 private int _sizeOfStruct; 38 private ArrayList _props; 39 40 41 public PlexOfCps(int sizeOfStruct) 42 { 43 _props = new ArrayList (); 44 _sizeOfStruct = sizeOfStruct; 45 } 46 47 54 public PlexOfCps(byte[] buf, int start, int size, int sizeOfStruct) 55 { 56 _count = (size - 4)/(4 + sizeOfStruct); 57 _sizeOfStruct = sizeOfStruct; 58 _props = new ArrayList (_count); 59 60 for (int x = 0; x < _count; x++) 61 { 62 _props.add(getProperty(x, buf, start)); 63 } 64 } 65 66 public GenericPropertyNode getProperty(int index) 67 { 68 return (GenericPropertyNode)_props.get(index); 69 } 70 71 public void addProperty(GenericPropertyNode node) 72 { 73 _props.add(node); 74 } 75 76 public byte[] toByteArray() 77 { 78 int size = _props.size(); 79 int cpBufSize = ((size + 1) * LittleEndian.INT_SIZE); 80 int structBufSize = + (_sizeOfStruct * size); 81 int bufSize = cpBufSize + structBufSize; 82 83 byte[] buf = new byte[bufSize]; 84 85 GenericPropertyNode node = null; 86 for (int x = 0; x < size; x++) 87 { 88 node = (GenericPropertyNode)_props.get(x); 89 90 LittleEndian.putInt(buf, (LittleEndian.INT_SIZE * x), node.getStart()); 92 93 System.arraycopy(node.getBytes(), 0, buf, cpBufSize + (x * _sizeOfStruct), 95 _sizeOfStruct); 96 } 97 LittleEndian.putInt(buf, LittleEndian.INT_SIZE * size, node.getEnd()); 99 100 return buf; 101 102 } 103 104 private GenericPropertyNode getProperty(int index, byte[] buf, int offset) 105 { 106 int start = LittleEndian.getInt(buf, offset + getIntOffset(index)); 107 int end = LittleEndian.getInt(buf, offset + getIntOffset(index+1)); 108 109 byte[] struct = new byte[_sizeOfStruct]; 110 System.arraycopy(buf, offset + getStructOffset(index), struct, 0, _sizeOfStruct); 111 112 return new GenericPropertyNode(start, end, struct); 113 } 114 115 private int getIntOffset(int index) 116 { 117 return index * 4; 118 } 119 120 125 public int length() 126 { 127 return _count; 128 } 129 130 139 private int getStructOffset(int index) 140 { 141 return (4 * (_count + 1)) + (_sizeOfStruct * index); 142 } 143 } 144 | Popular Tags |