Search in sources :

Example 1 with POIFSWriterListener

use of org.apache.poi.poifs.filesystem.POIFSWriterListener in project poi by apache.

the class DataSpaceMapUtils method createEncryptionEntry.

public static DocumentEntry createEncryptionEntry(DirectoryEntry dir, String path, EncryptionRecord out) throws IOException {
    String[] parts = path.split("/");
    for (int i = 0; i < parts.length - 1; i++) {
        dir = dir.hasEntry(parts[i]) ? (DirectoryEntry) dir.getEntry(parts[i]) : dir.createDirectory(parts[i]);
    }
    final byte[] buf = new byte[5000];
    LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0);
    out.write(bos);
    String fileName = parts[parts.length - 1];
    if (dir.hasEntry(fileName)) {
        dir.getEntry(fileName).delete();
    }
    return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener() {

        public void processPOIFSWriterEvent(POIFSWriterEvent event) {
            try {
                event.getStream().write(buf, 0, event.getLimit());
            } catch (IOException e) {
                throw new EncryptedDocumentException(e);
            }
        }
    });
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) POIFSWriterListener(org.apache.poi.poifs.filesystem.POIFSWriterListener) IOException(java.io.IOException) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) POIFSWriterEvent(org.apache.poi.poifs.filesystem.POIFSWriterEvent)

Aggregations

IOException (java.io.IOException)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)1 POIFSWriterEvent (org.apache.poi.poifs.filesystem.POIFSWriterEvent)1 POIFSWriterListener (org.apache.poi.poifs.filesystem.POIFSWriterListener)1 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)1