public void testReadWrite() throws Exception { FileInformationBlock fib = _hWPFDocFixture._fib; byte[] mainStream = _hWPFDocFixture._mainStream; byte[] tableStream = _hWPFDocFixture._tableStream; int fcMin = fib.getFibBase().getFcMin(); _cHPBinTable = new CHPBinTable( mainStream, tableStream, fib.getFcPlcfbteChpx(), fib.getLcbPlcfbteChpx(), fakeTPT); HWPFFileSystem fileSys = new HWPFFileSystem(); _cHPBinTable.writeTo(fileSys, 0, fakeTPT); ByteArrayOutputStream tableOut = fileSys.getStream("1Table"); ByteArrayOutputStream mainOut = fileSys.getStream("WordDocument"); byte[] newTableStream = tableOut.toByteArray(); byte[] newMainStream = mainOut.toByteArray(); CHPBinTable newBinTable = new CHPBinTable(newMainStream, newTableStream, 0, newTableStream.length, fakeTPT); List<CHPX> oldTextRuns = _cHPBinTable._textRuns; List<CHPX> newTextRuns = newBinTable._textRuns; assertEquals(oldTextRuns.size(), newTextRuns.size()); int size = oldTextRuns.size(); for (int x = 0; x < size; x++) { CHPX oldNode = oldTextRuns.get(x); CHPX newNode = newTextRuns.get(x); assertTrue(oldNode.equals(newNode)); } }
protected byte[] toByteArray(int fcMin) { byte[] buf = new byte[512]; int size = _chpxList.size(); int grpprlOffset = 511; int offsetOffset = 0; int fcOffset = 0; // total size is currently the size of one FC int totalSize = FC_SIZE + 2; int index = 0; for (; index < size; index++) { int grpprlLength = (_chpxList.get(index)).getGrpprl().length; // check to see if we have enough room for an FC, the grpprl offset, // the grpprl size byte and the grpprl. totalSize += (FC_SIZE + 2 + grpprlLength); // if size is uneven we will have to add one so the first grpprl falls // on a word boundary if (totalSize > 511 + (index % 2)) { totalSize -= (FC_SIZE + 2 + grpprlLength); break; } // grpprls must fall on word boundaries if ((1 + grpprlLength) % 2 > 0) { totalSize += 1; } } // see if we couldn't fit some if (index != size) { _overFlow = new ArrayList<CHPX>(); _overFlow.addAll(_chpxList.subList(index, size)); } // index should equal number of CHPXs that will be in this fkp now. buf[511] = (byte) index; offsetOffset = (FC_SIZE * index) + FC_SIZE; // grpprlOffset = offsetOffset + index + (grpprlOffset % 2); CHPX chpx = null; for (int x = 0; x < index; x++) { chpx = (CHPX) _chpxList.get(x); byte[] grpprl = chpx.getGrpprl(); LittleEndian.putInt(buf, fcOffset, chpx.getStartBytes() + fcMin); grpprlOffset -= (1 + grpprl.length); grpprlOffset -= (grpprlOffset % 2); buf[offsetOffset] = (byte) (grpprlOffset / 2); buf[grpprlOffset] = (byte) grpprl.length; System.arraycopy(grpprl, 0, buf, grpprlOffset + 1, grpprl.length); offsetOffset += 1; fcOffset += FC_SIZE; } // put the last chpx's end in LittleEndian.putInt(buf, fcOffset, chpx.getEndBytes() + fcMin); return buf; }