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);
}
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;
}
Aggregations