Search in sources :

Example 11 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class TestWrite method writeEmptyPropertySet.

/**
     * <p>Writes an empty property set to a POIFS and reads it back
     * in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception UnsupportedVariantTypeException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeEmptyPropertySet() throws IOException, UnsupportedVariantTypeException {
    final File dataDir = _samples.getFile("");
    final File filename = new File(dataDir, POI_FS);
    filename.deleteOnExit();
    /* Create a mutable property set and write it to a POIFS: */
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    final MutableSection s = (MutableSection) ps.getSections().get(0);
    s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
    final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
    ps.write(psStream);
    psStream.close();
    final byte[] streamData = psStream.toByteArray();
    poiFs.createDocument(new ByteArrayInputStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME);
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final POIFSReader r = new POIFSReader();
    r.registerListener(new MyPOIFSReaderListener(), SummaryInformation.DEFAULT_STREAM_NAME);
    FileInputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) 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) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Aggregations

FileInputStream (java.io.FileInputStream)11 POIFSReader (org.apache.poi.poifs.eventfilesystem.POIFSReader)11 IOException (java.io.IOException)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 POIFSReaderEvent (org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent)4 POIFSReaderListener (org.apache.poi.poifs.eventfilesystem.POIFSReaderListener)4 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)4 TempFile (org.apache.poi.util.TempFile)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)3 MutableSection (org.apache.poi.hpsf.MutableSection)3 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)3 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2