Search in sources :

Example 6 with LittleEndianOutputStream

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

the class Ole10Native method writeOut.

/**
     * Have the contents printer out into an OutputStream, used when writing a
     * file back out to disk (Normally, atom classes will keep their bytes
     * around, but non atom classes will just request the bytes from their
     * children, then chuck on their header and return)
     */
public void writeOut(OutputStream out) throws IOException {
    // byte intbuf[] = new byte[LittleEndianConsts.INT_SIZE];
    // byte shortbuf[] = new byte[LittleEndianConsts.SHORT_SIZE];
    @SuppressWarnings("resource") LittleEndianOutputStream leosOut = new LittleEndianOutputStream(out);
    switch(mode) {
        case parsed:
            {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                LittleEndianOutputStream leos = new LittleEndianOutputStream(bos);
                // total size, will be determined later ..
                leos.writeShort(getFlags1());
                leos.write(getLabel().getBytes(ISO1));
                leos.write(0);
                leos.write(getFileName().getBytes(ISO1));
                leos.write(0);
                leos.writeShort(getFlags2());
                leos.writeShort(getUnknown1());
                leos.writeInt(getCommand().length() + 1);
                leos.write(getCommand().getBytes(ISO1));
                leos.write(0);
                leos.writeInt(getDataSize());
                leos.write(getDataBuffer());
                leos.writeShort(getFlags3());
                // satisfy compiler ...
                leos.close();
                // total size
                leosOut.writeInt(bos.size());
                bos.writeTo(out);
                break;
            }
        case compact:
            leosOut.writeInt(getDataSize() + LittleEndianConsts.SHORT_SIZE);
            leosOut.writeShort(getFlags1());
            out.write(getDataBuffer());
            break;
        default:
        case unparsed:
            leosOut.writeInt(getDataSize());
            out.write(getDataBuffer());
            break;
    }
}
Also used : LittleEndianOutputStream(org.apache.poi.util.LittleEndianOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with LittleEndianOutputStream

use of org.apache.poi.util.LittleEndianOutputStream 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)

Example 8 with LittleEndianOutputStream

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

the class TestLbsDataSubRecord method test_LbsDropData.

public void test_LbsDropData() throws IOException {
    byte[] data = HexRead.readFromString(//LbsDropData
    //flags
    "0A, 00, " + //the number of lines to be displayed in the dropdown
    "14, 00, " + //the smallest width in pixels allowed for the dropdown window
    "6C, 00, " + //num chars
    "00, 00, " + //compression flag
    "00, " + //padding byte
    "00");
    LittleEndianInputStream in = new LittleEndianInputStream(new ByteArrayInputStream(data));
    try {
        LbsDataSubRecord.LbsDropData lbs = new LbsDataSubRecord.LbsDropData(in);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        LittleEndianOutputStream out = new LittleEndianOutputStream(baos);
        try {
            lbs.serialize(out);
            assertArrayEquals(data, baos.toByteArray());
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) LittleEndianOutputStream(org.apache.poi.util.LittleEndianOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with LittleEndianOutputStream

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

the class TestDConRefRecord method testReadWrite.

private void testReadWrite(byte[] data, String message) throws IOException {
    RecordInputStream is = TestcaseRecordInputStream.create(81, data);
    DConRefRecord d = new DConRefRecord(is);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    LittleEndianOutputStream o = new LittleEndianOutputStream(bos);
    d.serialize(o);
    o.flush();
    assertTrue(message, Arrays.equals(data, bos.toByteArray()));
}
Also used : LittleEndianOutputStream(org.apache.poi.util.LittleEndianOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LittleEndianOutputStream (org.apache.poi.util.LittleEndianOutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)3 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)3 LittleEndianInputStream (org.apache.poi.util.LittleEndianInputStream)3 Test (org.junit.Test)3 LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)2 IOException (java.io.IOException)1 HSLFException (org.apache.poi.hslf.exceptions.HSLFException)1 TextPropCollection (org.apache.poi.hslf.model.textproperties.TextPropCollection)1 ExtRst (org.apache.poi.hssf.record.common.UnicodeString.ExtRst)1 FormatRun (org.apache.poi.hssf.record.common.UnicodeString.FormatRun)1 ContinuableRecordOutput (org.apache.poi.hssf.record.cont.ContinuableRecordOutput)1