Example usage for org.apache.poi.poifs.filesystem POIFSFileSystem writeFilesystem

List of usage examples for org.apache.poi.poifs.filesystem POIFSFileSystem writeFilesystem

Introduction

In this page you can find the example usage for org.apache.poi.poifs.filesystem POIFSFileSystem writeFilesystem.

Prototype

public void writeFilesystem(final OutputStream stream) throws IOException 

Source Link

Document

Write the filesystem out

Usage

From source file:tv.amwa.maj.io.aaf.AAFBuilder.java

License:Apache License

/**
 * @param args/*from   ww  w  .j  av  a 2s.co m*/
 */
public static void main(String[] args) throws IOException {

    if (args.length != 2)
        System.exit(1);

    final String inputFilename = args[0];
    final String outputFilename = args[1];

    POIFSReader r = new AAFReader();

    LocalAAFEventReader eventReader = new LocalAAFEventReader();
    r.registerListener(eventReader);

    AvidFactory.registerAvidExtensions();

    FileInputStream fis = null;

    long startTime = System.nanoTime();
    try {
        fis = new FileInputStream(inputFilename);
        r.read(new FileInputStream(inputFilename));
        eventReader.resolveEntries();
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (Exception e) {
            }
    }
    long endTime = System.nanoTime();

    System.out.println("AAF file read in " + (endTime - startTime) + "ns.");

    ((PrefaceImpl) eventReader.getPreface()).setByteOrder(tv.amwa.maj.enumeration.ByteOrder.Big);
    System.out.println(eventReader.getPreface().getByteOrder());

    POIFSFileSystem outputFileSystem = new POIFSFileSystem();

    DirectoryEntry rootDir = outputFileSystem.getRoot();

    AAFWriterListener aafWriter = makeAAFWriterListener();
    DirectoryEntry metaDictionaryDir = rootDir.createDirectory(META_DICTIONARY_DIRNAME);
    DirectoryEntry prefaceDir = rootDir.createDirectory(PREFACE_DIRNAME);

    generateAAFStructure(prefaceDir, aafWriter, eventReader.getPreface());
    generateMetaDictionary(metaDictionaryDir, aafWriter, eventReader.getPreface());
    rootDir.createDocument(PROPERTIES_STREAMNAME, 70, aafWriter);
    rootDir.createDocument(REFERENCED_PROPERTIES_STREAMNAME, aafWriter.getReferencedPropertiesSize(),
            aafWriter);

    //       rootDir.setStorageClsid(
    //             (outputFileSystem.getBigBlockSize() == 4096) ?
    //                   new AAFClassID(AAFSignatureSSBin4K) :
    //                      new AAFClassID(AAFSignatureSSBinary));

    rootDir.setStorageClsid(new ClassID(rootEntryClassID, 0));

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFilename);
        outputFileSystem.writeFilesystem(fos);
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (Exception e) {
            }
    }

    // AAF puts a signature in bytes 8-23 of the file
    // POIFS cannot write this

    RandomAccessFile fixFile = null;
    FileChannel fixChannel = null;
    try {
        fixFile = new RandomAccessFile(outputFilename, "rw");
        fixChannel = fixFile.getChannel();

        fixChannel.position(8);
        if (outputFileSystem.getBigBlockSize() == 4096)
            fixChannel.write(ByteBuffer.wrap(AAFSignatureSSBin4KBytes));
        else
            fixChannel.write(ByteBuffer.wrap(AAFSignatureSSBinaryBytes));
    } finally {
        if (fixChannel != null)
            fixChannel.close();
    }
}

From source file:tv.amwa.maj.io.aaf.AAFFactory.java

License:Apache License

/**
 * <p>Write an AAF file with the given filename that is constructed from the given {@linkplain Preface preface}.<p>
 * /*from  w w  w  .  ja  v  a2 s.c  o m*/
 * <p>Note that this version of MAJ only supports writing metadata-only files.</p>
 * 
 * @param preface Preface to use to construct an AAF file from.
 * @param outputFilename File path specification for the file to write the preface to.
 * 
 * @throws IOException An error occurred when writing the AAF file.
 * 
 * @see #readPreface(String)
 */
public final static void writePreface(Preface preface, String outputFilename) throws IOException {

    POIFSFileSystem outputFileSystem = new POIFSFileSystem();

    DirectoryEntry rootDir = outputFileSystem.getRoot();

    preface.updateDictionaries();
    //       System.out.println(preface);

    AAFWriterListener aafWriter = AAFBuilder.makeAAFWriterListener();
    DirectoryEntry metaDictionaryDir = rootDir.createDirectory(META_DICTIONARY_DIRNAME);
    DirectoryEntry prefaceDir = rootDir.createDirectory(PREFACE_DIRNAME);

    AAFBuilder.generateAAFStructure(prefaceDir, aafWriter, preface);
    AAFBuilder.generateMetaDictionary(metaDictionaryDir, aafWriter, preface);
    rootDir.createDocument(PROPERTIES_STREAMNAME, 70, aafWriter);
    rootDir.createDocument(REFERENCED_PROPERTIES_STREAMNAME, aafWriter.getReferencedPropertiesSize(),
            aafWriter);

    //       rootDir.setStorageClsid(
    //             (outputFileSystem.getBigBlockSize() == 4096) ?
    //                   new AAFClassID(AAFSignatureSSBin4K) :
    //                      new AAFClassID(AAFSignatureSSBinary));

    rootDir.setStorageClsid(new ClassID(rootEntryClassID, 0));

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFilename);
        outputFileSystem.writeFilesystem(fos);
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (Exception e) {
            }
    }

    // AAF puts a signature in bytes 8-23 of the file
    // POIFS cannot write this

    RandomAccessFile fixFile = null;
    FileChannel fixChannel = null;
    try {
        fixFile = new RandomAccessFile(outputFilename, "rw");
        fixChannel = fixFile.getChannel();

        fixChannel.position(8);
        if (outputFileSystem.getBigBlockSize() == 4096)
            fixChannel.write(ByteBuffer.wrap(AAFSignatureSSBin4KBytes));
        else
            fixChannel.write(ByteBuffer.wrap(AAFSignatureSSBinaryBytes));
    } finally {
        if (fixChannel != null)
            fixChannel.close();
    }
}

From source file:uk.ac.ucl.cs.cmic.giftcloud.uploader.ExcelWriter.java

License:Open Source License

@Override
protected void saveFile(final File file) throws IOException {
    if (spreadsheetPassword.isPresent() && StringUtils.isNotBlank(new String(spreadsheetPassword.get()))) {
        try {/*from  w  w w  . j av a  2 s . c om*/
            // Write the unencrypted spreadsheet to an in-memory stream
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            workbook.write(baos);
            baos.close();

            // Create objects for encryption
            POIFSFileSystem fs = new POIFSFileSystem();
            EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
            Encryptor enc = info.getEncryptor();

            // Set the password
            enc.confirmPassword(new String(spreadsheetPassword.get()));

            // Open the in-memory spreadsheet
            InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
            OPCPackage opc = OPCPackage.open(inputStream);
            OutputStream os = enc.getDataStream(fs);
            opc.save(os);
            opc.close();
            inputStream.close();

            // Write the encrypted Excel spreadsheet
            FileOutputStream fos = new FileOutputStream(file);
            fs.writeFilesystem(fos);
            fos.close();
        } catch (InvalidFormatException e) {
            throw new IOException(
                    "Unable to save the patient list file due to the following InvalidFormatException when reading the excel file:"
                            + e.getLocalizedMessage(),
                    e);
        } catch (GeneralSecurityException e) {
            throw new IOException(
                    "Unable to save the patient list file due to the following GeneralSecurityException when reading the excel file:"
                            + e.getLocalizedMessage(),
                    e);
        }
    } else {
        // Write the unencrypted spreadsheet to an in-memory stream
        final FileOutputStream fos = new FileOutputStream(file);
        workbook.write(fos);
        fos.close();
    }
}