Search in sources :

Example 1 with HeaderBlock

use of org.apache.poi.poifs.storage.HeaderBlock in project poi by apache.

the class POIFSHeaderDumper method viewFile.

public static void viewFile(final String filename) throws Exception {
    System.out.println("Dumping headers from: " + filename);
    InputStream inp = new FileInputStream(filename);
    // Header
    HeaderBlock header_block = new HeaderBlock(inp);
    displayHeader(header_block);
    // Raw blocks
    POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
    RawDataBlockList data_blocks = new RawDataBlockList(inp, bigBlockSize);
    displayRawBlocksSummary(data_blocks);
    // Main FAT Table
    BlockAllocationTableReader batReader = new BlockAllocationTableReader(header_block.getBigBlockSize(), header_block.getBATCount(), header_block.getBATArray(), header_block.getXBATCount(), header_block.getXBATIndex(), data_blocks);
    displayBATReader("Big Blocks", batReader);
    // Properties Table
    PropertyTable properties = new PropertyTable(header_block, data_blocks);
    // Mini Fat
    BlockAllocationTableReader sbatReader = SmallBlockTableReader._getSmallDocumentBlockReader(bigBlockSize, data_blocks, properties.getRoot(), header_block.getSBATStart());
    displayBATReader("Small Blocks", sbatReader);
    // Summary of the properties
    displayPropertiesSummary(properties);
}
Also used : BlockAllocationTableReader(org.apache.poi.poifs.storage.BlockAllocationTableReader) PropertyTable(org.apache.poi.poifs.property.PropertyTable) HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize) FileInputStream(java.io.FileInputStream) RawDataBlockList(org.apache.poi.poifs.storage.RawDataBlockList)

Example 2 with HeaderBlock

use of org.apache.poi.poifs.storage.HeaderBlock in project poi by apache.

the class POIFSDump method main.

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.err.println("Must specify at least one file to dump");
        System.exit(1);
    }
    boolean dumpProps = false, dumpMini = false;
    for (String filename : args) {
        if (filename.equalsIgnoreCase("-dumprops") || filename.equalsIgnoreCase("-dump-props") || filename.equalsIgnoreCase("-dump-properties")) {
            dumpProps = true;
            continue;
        }
        if (filename.equalsIgnoreCase("-dumpmini") || filename.equalsIgnoreCase("-dump-mini") || filename.equalsIgnoreCase("-dump-ministream") || filename.equalsIgnoreCase("-dump-mini-stream")) {
            dumpMini = true;
            continue;
        }
        System.out.println("Dumping " + filename);
        FileInputStream is = new FileInputStream(filename);
        NPOIFSFileSystem fs;
        try {
            fs = new NPOIFSFileSystem(is);
        } finally {
            is.close();
        }
        try {
            DirectoryEntry root = fs.getRoot();
            String filenameWithoutPath = new File(filename).getName();
            File dumpDir = new File(filenameWithoutPath + "_dump");
            File file = new File(dumpDir, root.getName());
            if (!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(root, file);
            if (dumpProps) {
                HeaderBlock header = fs.getHeaderBlock();
                dump(fs, header.getPropertyStart(), "properties", file);
            }
            if (dumpMini) {
                NPropertyTable props = fs.getPropertyTable();
                int startBlock = props.getRoot().getStartBlock();
                if (startBlock == POIFSConstants.END_OF_CHAIN) {
                    System.err.println("No Mini Stream in file");
                } else {
                    dump(fs, startBlock, "mini-stream", file);
                }
            }
        } finally {
            fs.close();
        }
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) IOException(java.io.IOException) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with HeaderBlock

use of org.apache.poi.poifs.storage.HeaderBlock in project poi by apache.

the class TestNPOIFSFileSystem method addBeforeWrite.

@Test
public void addBeforeWrite() throws IOException {
    NPOIFSFileSystem fs1 = new NPOIFSFileSystem();
    DocumentEntry miniDoc;
    DocumentEntry normDoc;
    HeaderBlock hdr;
    // Initially has Properties + BAT but nothing else
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs1.getNextBlock(1));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs1.getNextBlock(2));
    hdr = writeOutAndReadHeader(fs1);
    // No mini stream, and no xbats
    // Will have fat then properties stream
    assertEquals(1, hdr.getBATCount());
    assertEquals(1, hdr.getBATArray()[0]);
    assertEquals(0, hdr.getPropertyStart());
    assertEquals(POIFSConstants.END_OF_CHAIN, hdr.getSBATStart());
    assertEquals(POIFSConstants.END_OF_CHAIN, hdr.getXBATIndex());
    assertEquals(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 3, fs1.size());
    fs1.close();
    // Get a clean filesystem to start with
    fs1 = new NPOIFSFileSystem();
    // Put our test files in a non-standard place
    DirectoryEntry parentDir = fs1.createDirectory("Parent Directory");
    DirectoryEntry testDir = parentDir.createDirectory("Test Directory");
    // Add to the mini stream
    byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
    testDir.createDocument("Mini", new ByteArrayInputStream(mini));
    // Add to the main stream
    byte[] main4096 = new byte[4096];
    main4096[0] = -10;
    main4096[4095] = -11;
    testDir.createDocument("Normal4096", new ByteArrayInputStream(main4096));
    // Check the mini stream was added, then the main stream
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs1.getNextBlock(1));
    // Mini Fat
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getNextBlock(2));
    // Mini Stream
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getNextBlock(3));
    // Main Stream
    assertEquals(5, fs1.getNextBlock(4));
    assertEquals(6, fs1.getNextBlock(5));
    assertEquals(7, fs1.getNextBlock(6));
    assertEquals(8, fs1.getNextBlock(7));
    assertEquals(9, fs1.getNextBlock(8));
    assertEquals(10, fs1.getNextBlock(9));
    assertEquals(11, fs1.getNextBlock(10));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getNextBlock(11));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs1.getNextBlock(12));
    assertEquals(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 13, fs1.size());
    // Check that we can read the right data pre-write
    miniDoc = (DocumentEntry) testDir.getEntry("Mini");
    assertContentsMatches(mini, miniDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal4096");
    assertContentsMatches(main4096, normDoc);
    // Write, read, check
    hdr = writeOutAndReadHeader(fs1);
    NPOIFSFileSystem fs2 = writeOutAndReadBack(fs1);
    fs1.close();
    // Check the header details - will have the sbat near the start,
    //  then the properties at the end
    assertEquals(1, hdr.getBATCount());
    assertEquals(1, hdr.getBATArray()[0]);
    assertEquals(2, hdr.getSBATStart());
    assertEquals(0, hdr.getPropertyStart());
    assertEquals(POIFSConstants.END_OF_CHAIN, hdr.getXBATIndex());
    // Check the block allocation is unchanged, other than
    //  the properties stream going in at the end
    // Properties
    assertEquals(12, fs2.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs2.getNextBlock(1));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs2.getNextBlock(2));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs2.getNextBlock(3));
    assertEquals(5, fs2.getNextBlock(4));
    assertEquals(6, fs2.getNextBlock(5));
    assertEquals(7, fs2.getNextBlock(6));
    assertEquals(8, fs2.getNextBlock(7));
    assertEquals(9, fs2.getNextBlock(8));
    assertEquals(10, fs2.getNextBlock(9));
    assertEquals(11, fs2.getNextBlock(10));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs2.getNextBlock(11));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs2.getNextBlock(12));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs2.getNextBlock(13));
    assertEquals(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 14, fs2.size());
    // Check the data
    DirectoryEntry fsRoot = fs2.getRoot();
    assertEquals(1, fsRoot.getEntryCount());
    parentDir = (DirectoryEntry) fsRoot.getEntry("Parent Directory");
    assertEquals(1, parentDir.getEntryCount());
    testDir = (DirectoryEntry) parentDir.getEntry("Test Directory");
    assertEquals(2, testDir.getEntryCount());
    miniDoc = (DocumentEntry) testDir.getEntry("Mini");
    assertContentsMatches(mini, miniDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal4096");
    assertContentsMatches(main4096, normDoc);
    // Add one more stream to each, then save and re-load
    byte[] mini2 = new byte[] { -42, 0, -1, -2, -3, -4, -42 };
    testDir.createDocument("Mini2", new ByteArrayInputStream(mini2));
    // Add to the main stream
    byte[] main4106 = new byte[4106];
    main4106[0] = 41;
    main4106[4105] = 42;
    testDir.createDocument("Normal4106", new ByteArrayInputStream(main4106));
    // Recheck the data in all 4 streams
    NPOIFSFileSystem fs3 = writeOutAndReadBack(fs2);
    fs2.close();
    fsRoot = fs3.getRoot();
    assertEquals(1, fsRoot.getEntryCount());
    parentDir = (DirectoryEntry) fsRoot.getEntry("Parent Directory");
    assertEquals(1, parentDir.getEntryCount());
    testDir = (DirectoryEntry) parentDir.getEntry("Test Directory");
    assertEquals(4, testDir.getEntryCount());
    miniDoc = (DocumentEntry) testDir.getEntry("Mini");
    assertContentsMatches(mini, miniDoc);
    miniDoc = (DocumentEntry) testDir.getEntry("Mini2");
    assertContentsMatches(mini2, miniDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal4106");
    assertContentsMatches(main4106, normDoc);
    // All done
    fs3.close();
}
Also used : HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) Test(org.junit.Test)

Example 4 with HeaderBlock

use of org.apache.poi.poifs.storage.HeaderBlock in project poi by apache.

the class TestNPOIFSFileSystem method createWriteRead.

/**
    * Create a new file, write it and read it again
    * Then, add some streams, write and read
    */
@Test
public void createWriteRead() throws IOException {
    NPOIFSFileSystem fs1 = new NPOIFSFileSystem();
    DocumentEntry miniDoc;
    DocumentEntry normDoc;
    // Initially has Properties + BAT but not SBAT
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs1.getNextBlock(1));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs1.getNextBlock(2));
    // Check that the SBAT is empty
    assertEquals(POIFSConstants.END_OF_CHAIN, fs1.getRoot().getProperty().getStartBlock());
    // Check that properties table was given block 0
    assertEquals(0, fs1._get_property_table().getStartBlock());
    // Write and read it
    NPOIFSFileSystem fs2 = writeOutAndReadBack(fs1);
    fs1.close();
    // No change, SBAT remains empty 
    assertEquals(POIFSConstants.END_OF_CHAIN, fs2.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs2.getNextBlock(1));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs2.getNextBlock(2));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs2.getNextBlock(3));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs2.getRoot().getProperty().getStartBlock());
    assertEquals(0, fs2._get_property_table().getStartBlock());
    fs2.close();
    // Check the same but with saving to a file
    NPOIFSFileSystem fs3 = new NPOIFSFileSystem();
    NPOIFSFileSystem fs4 = writeOutFileAndReadBack(fs3);
    fs3.close();
    // Same, no change, SBAT remains empty 
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs4.getNextBlock(1));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(2));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(3));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getRoot().getProperty().getStartBlock());
    assertEquals(0, fs4._get_property_table().getStartBlock());
    // Put everything within a new directory
    DirectoryEntry testDir = fs4.createDirectory("Test Directory");
    // Add a new Normal Stream (Normal Streams minimum 4096 bytes)
    byte[] main4096 = new byte[4096];
    main4096[0] = -10;
    main4096[4095] = -11;
    testDir.createDocument("Normal4096", new ByteArrayInputStream(main4096));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs4.getNextBlock(1));
    assertEquals(3, fs4.getNextBlock(2));
    assertEquals(4, fs4.getNextBlock(3));
    assertEquals(5, fs4.getNextBlock(4));
    assertEquals(6, fs4.getNextBlock(5));
    assertEquals(7, fs4.getNextBlock(6));
    assertEquals(8, fs4.getNextBlock(7));
    assertEquals(9, fs4.getNextBlock(8));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(9));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(10));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(11));
    // SBAT still unused
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getRoot().getProperty().getStartBlock());
    // Add a bigger Normal Stream
    byte[] main5124 = new byte[5124];
    main5124[0] = -22;
    main5124[5123] = -33;
    testDir.createDocument("Normal5124", new ByteArrayInputStream(main5124));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs4.getNextBlock(1));
    assertEquals(3, fs4.getNextBlock(2));
    assertEquals(4, fs4.getNextBlock(3));
    assertEquals(5, fs4.getNextBlock(4));
    assertEquals(6, fs4.getNextBlock(5));
    assertEquals(7, fs4.getNextBlock(6));
    assertEquals(8, fs4.getNextBlock(7));
    assertEquals(9, fs4.getNextBlock(8));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(9));
    assertEquals(11, fs4.getNextBlock(10));
    assertEquals(12, fs4.getNextBlock(11));
    assertEquals(13, fs4.getNextBlock(12));
    assertEquals(14, fs4.getNextBlock(13));
    assertEquals(15, fs4.getNextBlock(14));
    assertEquals(16, fs4.getNextBlock(15));
    assertEquals(17, fs4.getNextBlock(16));
    assertEquals(18, fs4.getNextBlock(17));
    assertEquals(19, fs4.getNextBlock(18));
    assertEquals(20, fs4.getNextBlock(19));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(20));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(21));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(22));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getRoot().getProperty().getStartBlock());
    // Now Add a mini stream
    byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
    testDir.createDocument("Mini", new ByteArrayInputStream(mini));
    // Mini stream will get one block for fat + one block for data
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs4.getNextBlock(1));
    assertEquals(3, fs4.getNextBlock(2));
    assertEquals(4, fs4.getNextBlock(3));
    assertEquals(5, fs4.getNextBlock(4));
    assertEquals(6, fs4.getNextBlock(5));
    assertEquals(7, fs4.getNextBlock(6));
    assertEquals(8, fs4.getNextBlock(7));
    assertEquals(9, fs4.getNextBlock(8));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(9));
    assertEquals(11, fs4.getNextBlock(10));
    assertEquals(12, fs4.getNextBlock(11));
    assertEquals(13, fs4.getNextBlock(12));
    assertEquals(14, fs4.getNextBlock(13));
    assertEquals(15, fs4.getNextBlock(14));
    assertEquals(16, fs4.getNextBlock(15));
    assertEquals(17, fs4.getNextBlock(16));
    assertEquals(18, fs4.getNextBlock(17));
    assertEquals(19, fs4.getNextBlock(18));
    assertEquals(20, fs4.getNextBlock(19));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(20));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(21));
    assertEquals(POIFSConstants.END_OF_CHAIN, fs4.getNextBlock(22));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs4.getNextBlock(23));
    // Check the mini stream location was set
    // (21 is mini fat, 22 is first mini stream block)
    assertEquals(22, fs4.getRoot().getProperty().getStartBlock());
    // Write and read back
    NPOIFSFileSystem fs5 = writeOutAndReadBack(fs4);
    fs4.close();
    HeaderBlock header = writeOutAndReadHeader(fs5);
    // Check the header has the right points in it
    assertEquals(1, header.getBATCount());
    assertEquals(1, header.getBATArray()[0]);
    assertEquals(0, header.getPropertyStart());
    assertEquals(1, header.getSBATCount());
    assertEquals(21, header.getSBATStart());
    assertEquals(22, fs5._get_property_table().getRoot().getStartBlock());
    // Block use should be almost the same, except the properties
    //  stream will have grown out to cover 2 blocks
    // Check the block use is all unchanged
    // Properties now extends over 2 blocks
    assertEquals(23, fs5.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs5.getNextBlock(1));
    assertEquals(3, fs5.getNextBlock(2));
    assertEquals(4, fs5.getNextBlock(3));
    assertEquals(5, fs5.getNextBlock(4));
    assertEquals(6, fs5.getNextBlock(5));
    assertEquals(7, fs5.getNextBlock(6));
    assertEquals(8, fs5.getNextBlock(7));
    assertEquals(9, fs5.getNextBlock(8));
    // End of normal4096
    assertEquals(POIFSConstants.END_OF_CHAIN, fs5.getNextBlock(9));
    assertEquals(11, fs5.getNextBlock(10));
    assertEquals(12, fs5.getNextBlock(11));
    assertEquals(13, fs5.getNextBlock(12));
    assertEquals(14, fs5.getNextBlock(13));
    assertEquals(15, fs5.getNextBlock(14));
    assertEquals(16, fs5.getNextBlock(15));
    assertEquals(17, fs5.getNextBlock(16));
    assertEquals(18, fs5.getNextBlock(17));
    assertEquals(19, fs5.getNextBlock(18));
    assertEquals(20, fs5.getNextBlock(19));
    // End of normal5124 
    assertEquals(POIFSConstants.END_OF_CHAIN, fs5.getNextBlock(20));
    // Mini Stream FAT
    assertEquals(POIFSConstants.END_OF_CHAIN, fs5.getNextBlock(21));
    // Mini Stream data
    assertEquals(POIFSConstants.END_OF_CHAIN, fs5.getNextBlock(22));
    // Properties #2
    assertEquals(POIFSConstants.END_OF_CHAIN, fs5.getNextBlock(23));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs5.getNextBlock(24));
    // Check some data
    assertEquals(1, fs5.getRoot().getEntryCount());
    testDir = (DirectoryEntry) fs5.getRoot().getEntry("Test Directory");
    assertEquals(3, testDir.getEntryCount());
    miniDoc = (DocumentEntry) testDir.getEntry("Mini");
    assertContentsMatches(mini, miniDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal4096");
    assertContentsMatches(main4096, normDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal5124");
    assertContentsMatches(main5124, normDoc);
    // Delete a couple of streams
    miniDoc.delete();
    normDoc.delete();
    // Check - will have un-used sectors now
    NPOIFSFileSystem fs6 = writeOutAndReadBack(fs5);
    fs5.close();
    // Props back in 1 block
    assertEquals(POIFSConstants.END_OF_CHAIN, fs6.getNextBlock(0));
    assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs6.getNextBlock(1));
    assertEquals(3, fs6.getNextBlock(2));
    assertEquals(4, fs6.getNextBlock(3));
    assertEquals(5, fs6.getNextBlock(4));
    assertEquals(6, fs6.getNextBlock(5));
    assertEquals(7, fs6.getNextBlock(6));
    assertEquals(8, fs6.getNextBlock(7));
    assertEquals(9, fs6.getNextBlock(8));
    // End of normal4096
    assertEquals(POIFSConstants.END_OF_CHAIN, fs6.getNextBlock(9));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(10));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(11));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(12));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(13));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(14));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(15));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(16));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(17));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(18));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(19));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(20));
    // Mini Stream FAT
    assertEquals(POIFSConstants.END_OF_CHAIN, fs6.getNextBlock(21));
    // Mini Stream data
    assertEquals(POIFSConstants.END_OF_CHAIN, fs6.getNextBlock(22));
    // Properties gone
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(23));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(24));
    assertEquals(POIFSConstants.UNUSED_BLOCK, fs6.getNextBlock(25));
    // All done
    fs6.close();
}
Also used : HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) Test(org.junit.Test)

Example 5 with HeaderBlock

use of org.apache.poi.poifs.storage.HeaderBlock in project poi by apache.

the class TestPOIFSFileSystem method testBATandXBAT.

/**
	 * Tests that we can write and read a file that contains XBATs
	 *  as well as regular BATs.
	 * However, because a file needs to be at least 6.875mb big
	 *  to have an XBAT in it, we don't have a test one. So, generate it.
	 */
public void testBATandXBAT() throws Exception {
    byte[] hugeStream = new byte[8 * 1024 * 1024];
    OPOIFSFileSystem fs = new OPOIFSFileSystem();
    fs.getRoot().createDocument("BIG", new ByteArrayInputStream(hugeStream));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    fs.writeFilesystem(baos);
    byte[] fsData = baos.toByteArray();
    // Check the header was written properly
    InputStream inp = new ByteArrayInputStream(fsData);
    HeaderBlock header = new HeaderBlock(inp);
    assertEquals(109 + 21, header.getBATCount());
    assertEquals(1, header.getXBATCount());
    // We should have 21 BATs in the XBAT
    ByteBuffer xbatData = ByteBuffer.allocate(512);
    xbatData.put(fsData, (1 + header.getXBATIndex()) * 512, 512);
    xbatData.position(0);
    BATBlock xbat = BATBlock.createBATBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, xbatData);
    for (int i = 0; i < 21; i++) {
        assertTrue(xbat.getValueAt(i) != POIFSConstants.UNUSED_BLOCK);
    }
    for (int i = 21; i < 127; i++) {
        assertEquals(POIFSConstants.UNUSED_BLOCK, xbat.getValueAt(i));
    }
    assertEquals(POIFSConstants.END_OF_CHAIN, xbat.getValueAt(127));
    // Load the blocks and check with that
    RawDataBlockList blockList = new RawDataBlockList(inp, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
    // Header not counted
    assertEquals(fsData.length / 512, blockList.blockCount() + 1);
    new BlockAllocationTableReader(header.getBigBlockSize(), header.getBATCount(), header.getBATArray(), header.getXBATCount(), header.getXBATIndex(), blockList);
    // Header not counted
    assertEquals(fsData.length / 512, blockList.blockCount() + 1);
    // Now load it and check
    fs = null;
    fs = new OPOIFSFileSystem(new ByteArrayInputStream(fsData));
    DirectoryNode root = fs.getRoot();
    assertEquals(1, root.getEntryCount());
    DocumentNode big = (DocumentNode) root.getEntry("BIG");
    assertEquals(hugeStream.length, big.getSize());
}
Also used : HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) RawDataBlockList(org.apache.poi.poifs.storage.RawDataBlockList) BlockAllocationTableReader(org.apache.poi.poifs.storage.BlockAllocationTableReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BATBlock(org.apache.poi.poifs.storage.BATBlock)

Aggregations

HeaderBlock (org.apache.poi.poifs.storage.HeaderBlock)10 RawDataBlockList (org.apache.poi.poifs.storage.RawDataBlockList)5 BlockAllocationTableReader (org.apache.poi.poifs.storage.BlockAllocationTableReader)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 FileInputStream (java.io.FileInputStream)2 POIFSBigBlockSize (org.apache.poi.poifs.common.POIFSBigBlockSize)2 PropertyTable (org.apache.poi.poifs.property.PropertyTable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 POIDataSamples (org.apache.poi.POIDataSamples)1 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)1 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)1 POIFSDocumentPath (org.apache.poi.poifs.filesystem.POIFSDocumentPath)1 NPropertyTable (org.apache.poi.poifs.property.NPropertyTable)1 RootProperty (org.apache.poi.poifs.property.RootProperty)1 BATBlock (org.apache.poi.poifs.storage.BATBlock)1