Search in sources :

Example 6 with LittleEndianByteArrayOutputStream

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

the class FilePassRecord method serialize.

@SuppressWarnings("resource")
@Override
public void serialize(LittleEndianOutput out) {
    out.writeShort(encryptionType);
    byte[] data = new byte[1024];
    // NOSONAR
    LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0);
    switch(encryptionInfo.getEncryptionMode()) {
        case xor:
            ((XOREncryptionHeader) encryptionInfo.getHeader()).write(bos);
            ((XOREncryptionVerifier) encryptionInfo.getVerifier()).write(bos);
            break;
        case binaryRC4:
            out.writeShort(encryptionInfo.getVersionMajor());
            out.writeShort(encryptionInfo.getVersionMinor());
            ((BinaryRC4EncryptionHeader) encryptionInfo.getHeader()).write(bos);
            ((BinaryRC4EncryptionVerifier) encryptionInfo.getVerifier()).write(bos);
            break;
        case cryptoAPI:
            out.writeShort(encryptionInfo.getVersionMajor());
            out.writeShort(encryptionInfo.getVersionMinor());
            out.writeInt(encryptionInfo.getEncryptionFlags());
            ((CryptoAPIEncryptionHeader) encryptionInfo.getHeader()).write(bos);
            ((CryptoAPIEncryptionVerifier) encryptionInfo.getVerifier()).write(bos);
            break;
        default:
            throw new EncryptedDocumentException("not supported");
    }
    out.write(data, 0, bos.getWriteIndex());
}
Also used : CryptoAPIEncryptionVerifier(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) CryptoAPIEncryptionHeader(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader) XOREncryptionHeader(org.apache.poi.poifs.crypt.xor.XOREncryptionHeader) BinaryRC4EncryptionVerifier(org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionVerifier) XOREncryptionVerifier(org.apache.poi.poifs.crypt.xor.XOREncryptionVerifier) BinaryRC4EncryptionHeader(org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionHeader)

Example 7 with LittleEndianByteArrayOutputStream

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

the class Ptg method serializePtgs.

/**
	 * Writes the ptgs to the data buffer, starting at the specified offset.
	 *
	 * <br/>
	 * The 2 byte encode length field is <b>not</b> written by this method.
	 * @return number of bytes written
	 */
public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
    // NOSONAR
    LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset);
    List<Ptg> arrayPtgs = null;
    for (Ptg ptg : ptgs) {
        ptg.write(out);
        if (ptg instanceof ArrayPtg) {
            if (arrayPtgs == null) {
                arrayPtgs = new ArrayList<Ptg>(5);
            }
            arrayPtgs.add(ptg);
        }
    }
    if (arrayPtgs != null) {
        for (Ptg arrayPtg : arrayPtgs) {
            ArrayPtg p = (ArrayPtg) arrayPtg;
            p.writeTokenValueBytes(out);
        }
    }
    return out.getWriteIndex() - offset;
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream)

Example 8 with LittleEndianByteArrayOutputStream

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

the class CellRangeAddressList method serialize.

public int serialize(int offset, byte[] data) {
    int totalSize = getSize();
    serialize(new LittleEndianByteArrayOutputStream(data, offset, totalSize));
    return totalSize;
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream)

Example 9 with LittleEndianByteArrayOutputStream

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

the class TestArrayPtg method testReadWriteTokenValueBytes.

/**
	 * Lots of problems with ArrayPtg's decoding and encoding of the element value data
	 */
public void testReadWriteTokenValueBytes() {
    ArrayPtg ptg = create(ENCODED_PTG_DATA, ENCODED_CONSTANT_DATA);
    assertEquals(3, ptg.getColumnCount());
    assertEquals(2, ptg.getRowCount());
    Object[][] values = ptg.getTokenArrayValues();
    assertEquals(2, values.length);
    assertEquals(Boolean.TRUE, values[0][0]);
    assertEquals("ABCD", values[0][1]);
    assertEquals(new Double(0), values[1][0]);
    assertEquals(Boolean.FALSE, values[1][1]);
    assertEquals("FG", values[1][2]);
    byte[] outBuf = new byte[ENCODED_CONSTANT_DATA.length];
    ptg.writeTokenValueBytes(new LittleEndianByteArrayOutputStream(outBuf, 0));
    if (outBuf[0] == 4) {
        throw new AssertionFailedError("Identified bug 42564b");
    }
    assertArrayEquals(ENCODED_CONSTANT_DATA, outBuf);
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) AssertionFailedError(junit.framework.AssertionFailedError)

Example 10 with LittleEndianByteArrayOutputStream

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

the class TestUnicodeString method extRstEqualsAndHashCode.

@Test
public void extRstEqualsAndHashCode() {
    byte[] buf = new byte[200];
    LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0);
    String str = "ᴂᴒᴢ";
    bos.writeShort(1);
    // data size
    bos.writeShort(5 * LittleEndianConsts.SHORT_SIZE + str.length() * 2 + 3 * LittleEndianConsts.SHORT_SIZE + 2);
    bos.writeShort(0x4711);
    bos.writeShort(0x0815);
    bos.writeShort(1);
    bos.writeShort(str.length());
    bos.writeShort(str.length());
    StringUtil.putUnicodeLE(str, bos);
    bos.writeShort(1);
    bos.writeShort(1);
    bos.writeShort(3);
    bos.writeShort(42);
    LittleEndianInput in = new LittleEndianByteArrayInputStream(buf, 0, bos.getWriteIndex());
    UnicodeString.ExtRst extRst1 = new UnicodeString.ExtRst(in, bos.getWriteIndex());
    in = new LittleEndianByteArrayInputStream(buf, 0, bos.getWriteIndex());
    UnicodeString.ExtRst extRst2 = new UnicodeString.ExtRst(in, bos.getWriteIndex());
    assertEquals(extRst1, extRst2);
    assertEquals(extRst1.hashCode(), extRst2.hashCode());
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) LittleEndianInput(org.apache.poi.util.LittleEndianInput) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ExtRst(org.apache.poi.hssf.record.common.UnicodeString.ExtRst) ExtRst(org.apache.poi.hssf.record.common.UnicodeString.ExtRst) Test(org.junit.Test)

Aggregations

LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)20 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)5 IOException (java.io.IOException)4 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)4 EncryptionRecord (org.apache.poi.poifs.crypt.standard.EncryptionRecord)3 Test (org.junit.Test)3 ChunkedCipherOutputStream (org.apache.poi.poifs.crypt.ChunkedCipherOutputStream)2 CryptoAPIEncryptionHeader (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader)2 CryptoAPIEncryptionVerifier (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier)2 LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)2 EncryptionDocument (com.microsoft.schemas.office.x2006.encryption.EncryptionDocument)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1