use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestDocumentEncryption method cryptoAPIChangeKeySize.
@Test
public void cryptoAPIChangeKeySize() throws Exception {
String pptFile = "cryptoapi-proc2356.ppt";
Biff8EncryptionKey.setCurrentUserPassword("crypto");
NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true);
HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
// need to cache data (i.e. read all data) before changing the key size
List<HSLFPictureData> picsExpected = hss.getPictureData();
hss.getDocumentSummaryInformation();
EncryptionInfo ei = hss.getDocumentEncryptionAtom().getEncryptionInfo();
((CryptoAPIEncryptionHeader) ei.getHeader()).setKeySize(0x78);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
hss.write(bos);
hss.close();
fs.close();
fs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
hss = new HSLFSlideShowImpl(fs);
List<HSLFPictureData> picsActual = hss.getPictureData();
assertEquals(picsExpected.size(), picsActual.size());
for (int i = 0; i < picsExpected.size(); i++) {
assertArrayEquals(picsExpected.get(i).getRawData(), picsActual.get(i).getRawData());
}
hss.close();
fs.close();
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestHWPFWrite method testInPlaceWrite.
/**
* Writing to the file we opened from - note, uses a temp file to avoid
* changing our test files!
*/
@Test
public void testInPlaceWrite() throws Exception {
// Setup as a copy of a known-good file
final File file = TempFile.createTempFile("TestDocument", ".doc");
InputStream inputStream = SAMPLES.openResourceAsStream("SampleDoc.doc");
try {
FileOutputStream outputStream = new FileOutputStream(file);
try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
}
} finally {
inputStream.close();
}
// Open from the temp file in read-write mode
NPOIFSFileSystem poifs = new NPOIFSFileSystem(file, false);
HWPFDocument doc = new HWPFDocument(poifs.getRoot());
Range r = doc.getRange();
assertEquals("I am a test document\r", r.getParagraph(0).text());
// Change
r.replaceText("X XX a test document\r", false);
// Save in-place, close, re-open and check
doc.write();
doc.close();
poifs.close();
poifs = new NPOIFSFileSystem(file);
doc = new HWPFDocument(poifs.getRoot());
r = doc.getRange();
assertEquals("X XX a test document\r", r.getParagraph(0).text());
doc.close();
poifs.close();
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestHWPFWrite method testInvalidInPlaceWriteNPOIFS.
@Test(expected = IllegalStateException.class)
public void testInvalidInPlaceWriteNPOIFS() throws Exception {
// Can't work for Read-Only files
NPOIFSFileSystem fs = new NPOIFSFileSystem(SAMPLES.getFile("SampleDoc.doc"), true);
HWPFDocument doc = new HWPFDocument(fs.getRoot());
try {
doc.write();
} finally {
doc.close();
fs.close();
}
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestWordExtractor method testDifferentPOIFS.
/**
* Tests that we can work with both {@link POIFSFileSystem}
* and {@link NPOIFSFileSystem}
*/
@Test
public void testDifferentPOIFS() throws Exception {
// Open the two filesystems
File file = docTests.getFile("test2.doc");
InputStream is = new FileInputStream(file);
OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
is.close();
NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
// Open directly
for (DirectoryNode dir : files) {
@SuppressWarnings("resource") WordExtractor extractor = new WordExtractor(dir);
assertEqualsTrim(p_text1_block, extractor.getText());
// extractor.close();
}
// Open via a HWPFDocument
for (DirectoryNode dir : files) {
HWPFDocument doc = new HWPFDocument(dir);
WordExtractor extractor = new WordExtractor(doc);
assertEqualsTrim(p_text1_block, extractor.getText());
extractor.close();
}
npoifs.close();
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestBugs method test51671.
/**
* [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on NPOIFSFileSystem
* throws a NullPointerException
*/
@Test
public void test51671() throws Exception {
InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("empty.doc");
NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
try {
HWPFDocument hwpfDocument = new HWPFDocument(npoifsFileSystem.getRoot());
hwpfDocument.write(new ByteArrayOutputStream());
hwpfDocument.close();
} finally {
npoifsFileSystem.close();
}
}
Aggregations