Search in sources :

Example 1 with WritingNotSupportedException

use of org.apache.poi.hpsf.WritingNotSupportedException in project poi by apache.

the class TestWrite method writeTwoSections.

/**
     * <p>Writes a simple property set with two sections to a POIFS and reads it
     * back in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception WritingNotSupportedException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeTwoSections() throws WritingNotSupportedException, IOException {
    final String STREAM_NAME = "PropertySetStream";
    final String SECTION1 = "Section 1";
    final String SECTION2 = "Section 2";
    final File dataDir = _samples.getFile("");
    final File filename = new File(dataDir, POI_FS);
    filename.deleteOnExit();
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    ps.clearSections();
    final ClassID formatID = new ClassID();
    formatID.setBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
    final MutableSection s1 = new MutableSection();
    s1.setFormatID(formatID);
    s1.setProperty(2, SECTION1);
    ps.addSection(s1);
    final MutableSection s2 = new MutableSection();
    s2.setFormatID(formatID);
    s2.setProperty(2, SECTION2);
    ps.addSection(s2);
    poiFs.createDocument(ps.toInputStream(), STREAM_NAME);
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final PropertySet[] psa = new PropertySet[1];
    final POIFSReader r = new POIFSReader();
    r.registerListener(new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                psa[0] = PropertySetFactory.create(event.getStream());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }, STREAM_NAME);
    FileInputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    assertNotNull(psa[0]);
    Section s = (psa[0].getSections().get(0));
    assertEquals(s.getFormatID(), formatID);
    Object p = s.getProperty(2);
    assertEquals(SECTION1, p);
    s = (psa[0].getSections().get(1));
    p = s.getProperty(2);
    assertEquals(SECTION2, p);
}
Also used : MutableSection(org.apache.poi.hpsf.MutableSection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) ClassID(org.apache.poi.hpsf.ClassID) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) MutableSection(org.apache.poi.hpsf.MutableSection) Section(org.apache.poi.hpsf.Section) NoPropertySetStreamException(org.apache.poi.hpsf.NoPropertySetStreamException) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalPropertySetDataException(org.apache.poi.hpsf.IllegalPropertySetDataException) UnsupportedVariantTypeException(org.apache.poi.hpsf.UnsupportedVariantTypeException) IOException(java.io.IOException) NoFormatIDException(org.apache.poi.hpsf.NoFormatIDException) ReadingNotSupportedException(org.apache.poi.hpsf.ReadingNotSupportedException) HPSFException(org.apache.poi.hpsf.HPSFException) FileInputStream(java.io.FileInputStream) POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileOutputStream(java.io.FileOutputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) PropertySet(org.apache.poi.hpsf.PropertySet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 2 with WritingNotSupportedException

use of org.apache.poi.hpsf.WritingNotSupportedException in project poi by apache.

the class CryptoAPIEncryptor method getSummaryEntries.

/**
     * Encrypt the Document-/SummaryInformation and other optionally streams.
     * Opposed to other crypto modes, cryptoapi is record based and can't be used
     * to stream-encrypt a whole file
     * 
     * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>
     */
public OutputStream getSummaryEntries(DirectoryNode dir) throws IOException, GeneralSecurityException {
    // NOSONAR
    CryptoAPIDocumentOutputStream bos = new CryptoAPIDocumentOutputStream(this);
    byte[] buf = new byte[8];
    // skip header
    bos.write(buf, 0, 8);
    String[] entryNames = { SummaryInformation.DEFAULT_STREAM_NAME, DocumentSummaryInformation.DEFAULT_STREAM_NAME };
    List<StreamDescriptorEntry> descList = new ArrayList<StreamDescriptorEntry>();
    int block = 0;
    for (String entryName : entryNames) {
        if (!dir.hasEntry(entryName)) {
            continue;
        }
        StreamDescriptorEntry descEntry = new StreamDescriptorEntry();
        descEntry.block = block;
        descEntry.streamOffset = bos.size();
        descEntry.streamName = entryName;
        descEntry.flags = StreamDescriptorEntry.flagStream.setValue(0, 1);
        descEntry.reserved2 = 0;
        bos.setBlock(block);
        DocumentInputStream dis = dir.createDocumentInputStream(entryName);
        IOUtils.copy(dis, bos);
        dis.close();
        descEntry.streamSize = bos.size() - descEntry.streamOffset;
        descList.add(descEntry);
        dir.getEntry(entryName).delete();
        block++;
    }
    int streamDescriptorArrayOffset = bos.size();
    bos.setBlock(0);
    LittleEndian.putUInt(buf, 0, descList.size());
    bos.write(buf, 0, 4);
    for (StreamDescriptorEntry sde : descList) {
        LittleEndian.putUInt(buf, 0, sde.streamOffset);
        bos.write(buf, 0, 4);
        LittleEndian.putUInt(buf, 0, sde.streamSize);
        bos.write(buf, 0, 4);
        LittleEndian.putUShort(buf, 0, sde.block);
        bos.write(buf, 0, 2);
        LittleEndian.putUByte(buf, 0, (short) sde.streamName.length());
        bos.write(buf, 0, 1);
        LittleEndian.putUByte(buf, 0, (short) sde.flags);
        bos.write(buf, 0, 1);
        LittleEndian.putUInt(buf, 0, sde.reserved2);
        bos.write(buf, 0, 4);
        byte[] nameBytes = StringUtil.getToUnicodeLE(sde.streamName);
        bos.write(nameBytes, 0, nameBytes.length);
        // null-termination
        LittleEndian.putShort(buf, 0, (short) 0);
        bos.write(buf, 0, 2);
    }
    int savedSize = bos.size();
    int streamDescriptorArraySize = savedSize - streamDescriptorArrayOffset;
    LittleEndian.putUInt(buf, 0, streamDescriptorArrayOffset);
    LittleEndian.putUInt(buf, 4, streamDescriptorArraySize);
    bos.reset();
    bos.setBlock(0);
    bos.write(buf, 0, 8);
    bos.setSize(savedSize);
    dir.createDocument("EncryptedSummary", new ByteArrayInputStream(bos.getBuf(), 0, savedSize));
    DocumentSummaryInformation dsi = PropertySetFactory.newDocumentSummaryInformation();
    try {
        dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    } catch (WritingNotSupportedException e) {
        throw new IOException(e);
    }
    return bos;
}
Also used : ArrayList(java.util.ArrayList) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) StreamDescriptorEntry(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor.StreamDescriptorEntry) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

IOException (java.io.IOException)2 WritingNotSupportedException (org.apache.poi.hpsf.WritingNotSupportedException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 ClassID (org.apache.poi.hpsf.ClassID)1 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)1 HPSFException (org.apache.poi.hpsf.HPSFException)1 IllegalPropertySetDataException (org.apache.poi.hpsf.IllegalPropertySetDataException)1 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)1 MutableSection (org.apache.poi.hpsf.MutableSection)1 NoFormatIDException (org.apache.poi.hpsf.NoFormatIDException)1 NoPropertySetStreamException (org.apache.poi.hpsf.NoPropertySetStreamException)1 PropertySet (org.apache.poi.hpsf.PropertySet)1 ReadingNotSupportedException (org.apache.poi.hpsf.ReadingNotSupportedException)1