Search in sources :

Example 1 with Chunk

use of org.apache.poi.hdgf.chunks.Chunk in project poi by apache.

the class TestStreamComplex method testChunkWithText.

@Test
public void testChunkWithText() {
    // Parent ChunkStream is at 0x7194
    // This is one of the last children of the trailer
    Pointer trailerPtr = ptrFactory.createPointer(contents, trailerPointerAt);
    TrailerStream ts = (TrailerStream) Stream.createStream(trailerPtr, contents, chunkFactory, ptrFactory);
    ts.findChildren(contents);
    assertNotNull(ts.getChildPointers());
    assertNotNull(ts.getPointedToStreams());
    assertEquals(20, ts.getChildPointers().length);
    assertEquals(20, ts.getPointedToStreams().length);
    assertEquals(0x7194, ts.getChildPointers()[13].getOffset());
    assertEquals(0x7194, ts.getPointedToStreams()[13].getPointer().getOffset());
    PointerContainingStream ps7194 = (PointerContainingStream) ts.getPointedToStreams()[13];
    // First child is at 0x64b3
    assertEquals(0x64b3, ps7194.getChildPointers()[0].getOffset());
    assertEquals(0x64b3, ps7194.getPointedToStreams()[0].getPointer().getOffset());
    ChunkStream cs = (ChunkStream) ps7194.getPointedToStreams()[0];
    // Should be 26bc bytes un-compressed
    assertEquals(0x26bc, cs.getStore().getContents().length);
    // And should have lots of children
    assertEquals(131, cs.getChunks().length);
    // One of which is Text
    boolean hasText = false;
    for (int i = 0; i < cs.getChunks().length; i++) {
        if (cs.getChunks()[i].getName().equals("Text")) {
            hasText = true;
        }
    }
    assertTrue(hasText);
    // Which is the 72nd command
    assertEquals("Text", cs.getChunks()[72].getName());
    Chunk text = cs.getChunks()[72];
    assertEquals("Text", text.getName());
    // Which contains our text
    assertEquals(1, text.getCommands().length);
    assertEquals("Test View\n", text.getCommands()[0].getValue());
    // Almost at the end is some more text
    assertEquals("Text", cs.getChunks()[128].getName());
    text = cs.getChunks()[128];
    assertEquals("Text", text.getName());
    assertEquals(1, text.getCommands().length);
    assertEquals("Some random text, on a page\n", text.getCommands()[0].getValue());
}
Also used : Pointer(org.apache.poi.hdgf.pointers.Pointer) Chunk(org.apache.poi.hdgf.chunks.Chunk) Test(org.junit.Test)

Example 2 with Chunk

use of org.apache.poi.hdgf.chunks.Chunk in project poi by apache.

the class VSDDumper method dumpStream.

private void dumpStream(Stream stream, int indent) {
    Pointer ptr = stream.getPointer();
    dumpVal("Stream at", ptr.getOffset(), indent);
    dumpVal("Type is", ptr.getType(), indent + 1);
    dumpVal("Format is", ptr.getFormat(), indent + 1);
    dumpVal("Length is", ptr.getLength(), indent + 1);
    if (ptr.destinationCompressed()) {
        dumpVal("DC.Length is", stream._getContentsLength(), indent + 1);
    }
    dumpVal("Compressed is", ptr.destinationCompressed(), indent + 1);
    dumpVal("Stream is", stream.getClass().getName(), indent + 1);
    byte[] db = stream._getStore()._getContents();
    String ds = (db.length >= 8) ? Arrays.toString(db) : "[]";
    dumpVal("First few bytes are", ds, indent + 1);
    if (stream instanceof PointerContainingStream) {
        Stream[] streams = ((PointerContainingStream) stream).getPointedToStreams();
        dumpVal("Nbr of children", streams.length, indent + 1);
        for (Stream s : streams) {
            dumpStream(s, indent + 1);
        }
    }
    if (stream instanceof ChunkStream) {
        Chunk[] chunks = ((ChunkStream) stream).getChunks();
        dumpVal("Nbr of chunks", chunks.length, indent + 1);
        for (Chunk chunk : chunks) {
            dumpChunk(chunk, indent + 1);
        }
    }
}
Also used : ChunkStream(org.apache.poi.hdgf.streams.ChunkStream) Pointer(org.apache.poi.hdgf.pointers.Pointer) PrintStream(java.io.PrintStream) PointerContainingStream(org.apache.poi.hdgf.streams.PointerContainingStream) ChunkStream(org.apache.poi.hdgf.streams.ChunkStream) Stream(org.apache.poi.hdgf.streams.Stream) PointerContainingStream(org.apache.poi.hdgf.streams.PointerContainingStream) Chunk(org.apache.poi.hdgf.chunks.Chunk)

Example 3 with Chunk

use of org.apache.poi.hdgf.chunks.Chunk in project poi by apache.

the class ChunkStream method findChunks.

/**
	 * Process the contents of the stream out into chunks
	 */
public void findChunks() {
    ArrayList<Chunk> chunksA = new ArrayList<Chunk>();
    if (getPointer().getOffset() == 0x64b3) {
        int i = 0;
        i++;
    }
    int pos = 0;
    byte[] contents = getStore().getContents();
    try {
        while (pos < contents.length) {
            // Ensure we have enough data to create a chunk from
            int headerSize = ChunkHeader.getHeaderSize(chunkFactory.getVersion());
            if (pos + headerSize <= contents.length) {
                Chunk chunk = chunkFactory.createChunk(contents, pos);
                chunksA.add(chunk);
                pos += chunk.getOnDiskSize();
            } else {
                logger.log(POILogger.WARN, "Needed " + headerSize + " bytes to create the next chunk header, but only found " + (contents.length - pos) + " bytes, ignoring rest of data");
                pos = contents.length;
            }
        }
    } catch (Exception e) {
        logger.log(POILogger.ERROR, "Failed to create chunk at " + pos + ", ignoring rest of data." + e);
    }
    chunks = chunksA.toArray(new Chunk[chunksA.size()]);
}
Also used : ArrayList(java.util.ArrayList) Chunk(org.apache.poi.hdgf.chunks.Chunk)

Aggregations

Chunk (org.apache.poi.hdgf.chunks.Chunk)3 Pointer (org.apache.poi.hdgf.pointers.Pointer)2 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 ChunkStream (org.apache.poi.hdgf.streams.ChunkStream)1 PointerContainingStream (org.apache.poi.hdgf.streams.PointerContainingStream)1 Stream (org.apache.poi.hdgf.streams.Stream)1 Test (org.junit.Test)1