Search in sources :

Example 1 with LittleEndianInputStream

use of org.apache.poi.util.LittleEndianInputStream in project poi by apache.

the class PropertySetFactory method create.

/**
     * Creates the most specific {@link PropertySet} from an {@link
     * InputStream}. This is preferrably a {@link
     * DocumentSummaryInformation} or a {@link SummaryInformation}. If
     * the specified {@link InputStream} does not contain a property
     * set stream, an exception is thrown and the {@link InputStream}
     * is repositioned at its beginning.
     *
     * @param stream Contains the property set stream's data.
     * @return The created {@link PropertySet}.
     * @throws NoPropertySetStreamException if the stream does not
     * contain a property set.
     * @throws MarkUnsupportedException if the stream does not support
     * the {@code mark} operation.
     * @throws IOException if some I/O problem occurs.
     * @exception UnsupportedEncodingException if the specified codepage is not
     * supported.
     */
public static PropertySet create(final InputStream stream) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException {
    stream.mark(PropertySet.OFFSET_HEADER + ClassID.LENGTH + 1);
    LittleEndianInputStream leis = new LittleEndianInputStream(stream);
    int byteOrder = leis.readUShort();
    int format = leis.readUShort();
    int osVersion = (int) leis.readUInt();
    byte[] clsIdBuf = new byte[ClassID.LENGTH];
    leis.readFully(clsIdBuf);
    int sectionCount = (int) leis.readUInt();
    if (byteOrder != PropertySet.BYTE_ORDER_ASSERTION || format != PropertySet.FORMAT_ASSERTION || sectionCount < 0) {
        throw new NoPropertySetStreamException();
    }
    if (sectionCount > 0) {
        leis.readFully(clsIdBuf);
    }
    stream.reset();
    ClassID clsId = new ClassID(clsIdBuf, 0);
    if (sectionCount > 0 && PropertySet.matchesSummary(clsId, SectionIDMap.SUMMARY_INFORMATION_ID)) {
        return new SummaryInformation(stream);
    } else if (sectionCount > 0 && PropertySet.matchesSummary(clsId, SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID)) {
        return new DocumentSummaryInformation(stream);
    } else {
        return new PropertySet(stream);
    }
}
Also used : LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream)

Example 2 with LittleEndianInputStream

use of org.apache.poi.util.LittleEndianInputStream in project poi by apache.

the class EmbeddedObjectRefSubRecord method readRefPtg.

private static Ptg readRefPtg(byte[] formulaRawBytes) {
    LittleEndianInput in = new LittleEndianInputStream(new ByteArrayInputStream(formulaRawBytes));
    byte ptgSid = in.readByte();
    switch(ptgSid) {
        case AreaPtg.sid:
            return new AreaPtg(in);
        case Area3DPtg.sid:
            return new Area3DPtg(in);
        case RefPtg.sid:
            return new RefPtg(in);
        case Ref3DPtg.sid:
            return new Ref3DPtg(in);
    }
    return null;
}
Also used : LittleEndianInput(org.apache.poi.util.LittleEndianInput) ByteArrayInputStream(java.io.ByteArrayInputStream) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 3 with LittleEndianInputStream

use of org.apache.poi.util.LittleEndianInputStream in project poi by apache.

the class TestUnicodeString method formatRun.

@Test
public void formatRun() throws Exception {
    FormatRun fr = new FormatRun((short) 4, (short) 0x15c);
    assertEquals(4, fr.getCharacterPos());
    assertEquals(0x15c, fr.getFontIndex());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    LittleEndianOutputStream out = new LittleEndianOutputStream(baos);
    fr.serialize(out);
    byte[] b = baos.toByteArray();
    assertEquals(4, b.length);
    assertEquals(4, b[0]);
    assertEquals(0, b[1]);
    assertEquals(0x5c, b[2]);
    assertEquals(0x01, b[3]);
    LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(b));
    fr = new FormatRun(inp);
    assertEquals(4, fr.getCharacterPos());
    assertEquals(0x15c, fr.getFontIndex());
}
Also used : LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FormatRun(org.apache.poi.hssf.record.common.UnicodeString.FormatRun) LittleEndianOutputStream(org.apache.poi.util.LittleEndianOutputStream) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) Test(org.junit.Test)

Example 4 with LittleEndianInputStream

use of org.apache.poi.util.LittleEndianInputStream in project poi by apache.

the class TestUnicodeString method extRstFromData.

@Test
public void extRstFromData() throws Exception {
    byte[] data = new byte[] { 01, 00, 0x0C, 00, 00, 00, 0x37, 00, 00, 00, 00, 00, 00, 00, // Cruft at the end, as found from real files
    00, // Cruft at the end, as found from real files
    00 };
    assertEquals(16, data.length);
    LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(data));
    ExtRst ext = new ExtRst(inp, data.length);
    // Excludes 4 byte header
    assertEquals(0x0c, ext.getDataSize());
    assertEquals(0, ext.getNumberOfRuns());
    assertEquals(0x37, ext.getFormattingOptions());
    assertEquals(0, ext.getFormattingFontIndex());
    assertEquals("", ext.getPhoneticText());
    assertEquals(0, ext.getPhRuns().length);
}
Also used : LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) ExtRst(org.apache.poi.hssf.record.common.UnicodeString.ExtRst) Test(org.junit.Test)

Example 5 with LittleEndianInputStream

use of org.apache.poi.util.LittleEndianInputStream in project poi by apache.

the class TestUnicodeString method extRstFromEmpty.

@Test
public void extRstFromEmpty() throws Exception {
    ExtRst ext = new ExtRst();
    assertEquals(0, ext.getNumberOfRuns());
    assertEquals(0, ext.getFormattingFontIndex());
    assertEquals(0, ext.getFormattingOptions());
    assertEquals("", ext.getPhoneticText());
    assertEquals(0, ext.getPhRuns().length);
    // Excludes 4 byte header
    assertEquals(10, ext.getDataSize());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    LittleEndianOutputStream out = new LittleEndianOutputStream(baos);
    ContinuableRecordOutput cout = new ContinuableRecordOutput(out, 0xffff);
    ext.serialize(cout);
    cout.writeContinue();
    byte[] b = baos.toByteArray();
    assertEquals(20, b.length);
    // First 4 bytes from the outputstream
    assertEquals(-1, b[0]);
    assertEquals(-1, b[1]);
    assertEquals(14, b[2]);
    assertEquals(00, b[3]);
    // Reserved
    assertEquals(1, b[4]);
    assertEquals(0, b[5]);
    // Data size
    assertEquals(10, b[6]);
    assertEquals(00, b[7]);
    // Font*2
    assertEquals(0, b[8]);
    assertEquals(0, b[9]);
    assertEquals(0, b[10]);
    assertEquals(0, b[11]);
    // 0 Runs
    assertEquals(0, b[12]);
    assertEquals(0, b[13]);
    // Size=0, *2
    assertEquals(0, b[14]);
    assertEquals(0, b[15]);
    assertEquals(0, b[16]);
    assertEquals(0, b[17]);
    // Last 2 bytes from the outputstream
    assertEquals(ContinueRecord.sid, b[18]);
    assertEquals(0, b[19]);
    // Load in again and re-test
    byte[] data = new byte[14];
    System.arraycopy(b, 4, data, 0, data.length);
    LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(data));
    ext = new ExtRst(inp, data.length);
    assertEquals(0, ext.getNumberOfRuns());
    assertEquals(0, ext.getFormattingFontIndex());
    assertEquals(0, ext.getFormattingOptions());
    assertEquals("", ext.getPhoneticText());
    assertEquals(0, ext.getPhRuns().length);
}
Also used : ContinuableRecordOutput(org.apache.poi.hssf.record.cont.ContinuableRecordOutput) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianOutputStream(org.apache.poi.util.LittleEndianOutputStream) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) ExtRst(org.apache.poi.hssf.record.common.UnicodeString.ExtRst) Test(org.junit.Test)

Aggregations

LittleEndianInputStream (org.apache.poi.util.LittleEndianInputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)4 Test (org.junit.Test)4 ExtRst (org.apache.poi.hssf.record.common.UnicodeString.ExtRst)3 LittleEndianOutputStream (org.apache.poi.util.LittleEndianOutputStream)3 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 FormatRun (org.apache.poi.hssf.record.common.UnicodeString.FormatRun)1 ContinuableRecordOutput (org.apache.poi.hssf.record.cont.ContinuableRecordOutput)1 ChunkedCipherInputStream (org.apache.poi.poifs.crypt.ChunkedCipherInputStream)1 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)1 DocumentNode (org.apache.poi.poifs.filesystem.DocumentNode)1 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)1 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)1