public byte[] writeTo(ByteArrayOutputStream docStream) throws IOException { PlexOfCps textPlex = new PlexOfCps(PieceDescriptor.getSizeInBytes()); // int fcMin = docStream.getOffset(); for (TextPiece next : _textPieces) { PieceDescriptor pd = next.getPieceDescriptor(); int offset = docStream.size(); int mod = (offset % POIFSConstants.SMALLER_BIG_BLOCK_SIZE); if (mod != 0) { mod = POIFSConstants.SMALLER_BIG_BLOCK_SIZE - mod; byte[] buf = IOUtils.safelyAllocate(mod, MAX_RECORD_LENGTH); docStream.write(buf); } // set the text piece position to the current docStream offset. pd.setFilePosition(docStream.size()); // write the text to the docstream and save the piece descriptor to // the // plex which will be written later to the tableStream. docStream.write(next.getRawBytes()); // The TextPiece is already in characters, which // makes our life much easier int nodeStart = next.getStart(); int nodeEnd = next.getEnd(); textPlex.addProperty(new GenericPropertyNode(nodeStart, nodeEnd, pd.toByteArray())); } return textPlex.toByteArray(); }
/** * * @param guessedCharset charset that we think this is * @return a new text piece * @throws IllegalStateException if the length isn't correct */ private TextPiece buildTextPiece(Charset guessedCharset) throws IllegalStateException { PieceDescriptor pd = new PieceDescriptor(new byte[] {0,0, 0,0,0,127, 0,0}, 0, guessedCharset); pd.setFilePosition(_fib.getFibBase().getFcMin()); // Generate a single Text Piece Table, with a single Text Piece // which covers all the (8 bit only) text in the file tpt = new OldTextPieceTable(); byte[] textData = IOUtils.safelyAllocate( _fib.getFibBase().getFcMac()-_fib.getFibBase().getFcMin(), MAX_RECORD_LENGTH); System.arraycopy(_mainStream, _fib.getFibBase().getFcMin(), textData, 0, textData.length); int numChars = textData.length; if (CodePageUtil.DOUBLE_BYTE_CHARSETS.contains(guessedCharset)) { numChars /= 2; } return new TextPiece( 0, numChars, textData, pd ); }
pd.setFilePosition( docStream.getOffset() );
pd.setFilePosition(_fib.getFibBase().getFcMin());