1 2 17 18 package org.apache.poi.hwpf.model; 19 20 import java.util.List ; 21 import java.util.ArrayList ; 22 23 import org.apache.poi.util.LittleEndian; 24 25 41 public class CHPFormattedDiskPage extends FormattedDiskPage 42 { 43 private static final int FC_SIZE = 4; 44 45 private ArrayList _chpxList = new ArrayList (); 46 private ArrayList _overFlow; 47 48 49 public CHPFormattedDiskPage() 50 { 51 } 52 53 57 public CHPFormattedDiskPage(byte[] documentStream, int offset, int fcMin) 58 { 59 super(documentStream, offset); 60 61 for (int x = 0; x < _crun; x++) 62 { 63 _chpxList.add(new CHPX(getStart(x) - fcMin, getEnd(x) - fcMin, getGrpprl(x))); 64 } 65 } 66 67 public CHPX getCHPX(int index) 68 { 69 return (CHPX)_chpxList.get(index); 70 } 71 72 public void fill(List filler) 73 { 74 _chpxList.addAll(filler); 75 } 76 77 public ArrayList getOverflow() 78 { 79 return _overFlow; 80 } 81 82 88 protected byte[] getGrpprl(int index) 89 { 90 int chpxOffset = 2 * LittleEndian.getUnsignedByte(_fkp, _offset + (((_crun + 1) * 4) + index)); 91 92 if(chpxOffset == 0) 94 { 95 return new byte[0]; 96 } 97 98 int size = LittleEndian.getUnsignedByte(_fkp, _offset + chpxOffset); 99 100 byte[] chpx = new byte[size]; 101 102 System.arraycopy(_fkp, _offset + ++chpxOffset, chpx, 0, size); 103 return chpx; 104 } 105 106 protected byte[] toByteArray(int fcMin) 107 { 108 byte[] buf = new byte[512]; 109 int size = _chpxList.size(); 110 int grpprlOffset = 511; 111 int offsetOffset = 0; 112 int fcOffset = 0; 113 114 int totalSize = FC_SIZE + 2; 116 117 int index = 0; 118 for (; index < size; index++) 119 { 120 int grpprlLength = ((CHPX)_chpxList.get(index)).getGrpprl().length; 121 122 totalSize += (FC_SIZE + 2 + grpprlLength); 125 if (totalSize > 511 + (index % 2)) 128 { 129 totalSize -= (FC_SIZE + 2 + grpprlLength); 130 break; 131 } 132 133 if ((1 + grpprlLength) % 2 > 0) 135 { 136 totalSize += 1; 137 } 138 } 139 140 if (index != size) 142 { 143 _overFlow = new ArrayList (); 144 _overFlow.addAll(_chpxList.subList(index, size)); 145 } 146 147 buf[511] = (byte)index; 149 150 offsetOffset = (FC_SIZE * index) + FC_SIZE; 151 153 CHPX chpx = null; 154 for (int x = 0; x < index; x++) 155 { 156 chpx = (CHPX)_chpxList.get(x); 157 byte[] grpprl = chpx.getGrpprl(); 158 159 LittleEndian.putInt(buf, fcOffset, chpx.getStart() + fcMin); 160 grpprlOffset -= (1 + grpprl.length); 161 grpprlOffset -= (grpprlOffset % 2); 162 buf[offsetOffset] = (byte)(grpprlOffset/2); 163 buf[grpprlOffset] = (byte)grpprl.length; 164 System.arraycopy(grpprl, 0, buf, grpprlOffset + 1, grpprl.length); 165 166 offsetOffset += 1; 167 fcOffset += FC_SIZE; 168 } 169 LittleEndian.putInt(buf, fcOffset, chpx.getEnd() + fcMin); 171 return buf; 172 } 173 174 } 175 | Popular Tags |