/** * Write the data content of this BIFF record including the sid and record length. * <p> * The subclass must write the exact number of bytes as reported by * {@link org.apache.poi.hssf.record.Record#getRecordSize()}} */ @Override public final int serialize(int offset, byte[] data) { int dataSize = getDataSize(); int recSize = 4 + dataSize; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); out.writeShort(getSid()); out.writeShort(dataSize); serialize(out); if (out.getWriteIndex() - offset != recSize) { throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): " + "Incorrect number of bytes written - expected " + recSize + " but got " + (out.getWriteIndex() - offset)); } return recSize; }
public void write(LittleEndianByteArrayOutputStream os) { int start = os.getWriteIndex(); LittleEndianOutput sizeOut = os.createDelayedOutput(LittleEndianConsts.INT_SIZE); os.writeInt(referenceComponent.length); for (int i=0; i<referenceComponent.length; i++) { os.writeInt(referenceComponentType[i]); writeUnicodeLPP4(os, referenceComponent[i]); } writeUnicodeLPP4(os, dataSpaceName); sizeOut.writeInt(os.getWriteIndex()-start); } }
/** * 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) { LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset); // NOSONAR List<Ptg> arrayPtgs = null; for (Ptg ptg : ptgs) { ptg.write(out); if (ptg instanceof ArrayPtg) { if (arrayPtgs == null) { arrayPtgs = new ArrayList<>(5); } arrayPtgs.add(ptg); } } if (arrayPtgs != null) { for (Ptg arrayPtg : arrayPtgs) { ArrayPtg p = (ArrayPtg) arrayPtg; p.writeTokenValueBytes(out); } } return out.getWriteIndex() - offset; }
@Override public int serialize(int offset, byte[] data) { int recSize = getRecordSize(); int dataSize = recSize - 4; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); // NOSONAR out.writeShort(sid); out.writeShort(dataSize); if (_uninterpretedData == null) { for (int i = 0; i < subrecords.size(); i++) { SubRecord record = subrecords.get(i); record.serialize(out); } int expectedEndIx = offset+dataSize; // padding while (out.getWriteIndex() < expectedEndIx) { out.writeByte(0); } } else { out.write(_uninterpretedData); } return recSize; }
public void write(LittleEndianByteArrayOutputStream bos) { int start = bos.getWriteIndex(); LittleEndianOutput sizeOut = bos.createDelayedOutput(LittleEndianConsts.INT_SIZE); bos.writeInt(transformType); writeUnicodeLPP4(bos, transformerId); sizeOut.writeInt(bos.getWriteIndex()-start); writeUnicodeLPP4(bos, transformerName); bos.writeShort(readerVersionMajor); bos.writeShort(readerVersionMinor); bos.writeShort(updaterVersionMajor); bos.writeShort(updaterVersionMinor); bos.writeShort(writerVersionMajor); bos.writeShort(writerVersionMinor); } }
public static DocumentEntry createEncryptionEntry(DirectoryEntry dir, String path, EncryptionRecord out) throws IOException { String parts[] = path.split("/"); for (int i=0; i<parts.length-1; i++) { dir = dir.hasEntry(parts[i]) ? (DirectoryEntry)dir.getEntry(parts[i]) : dir.createDirectory(parts[i]); } final byte buf[] = new byte[5000]; LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0); out.write(bos); String fileName = parts[parts.length-1]; if (dir.hasEntry(fileName)) { dir.getEntry(fileName).delete(); } return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener(){ public void processPOIFSWriterEvent(POIFSWriterEvent event) { try { event.getStream().write(buf, 0, event.getLimit()); } catch (IOException e) { throw new EncryptedDocumentException(e); } } }); }
/** * serializes the header */ @Override public void write(LittleEndianByteArrayOutputStream bos) { int startIdx = bos.getWriteIndex(); LittleEndianOutput sizeOutput = bos.createDelayedOutput(LittleEndianConsts.INT_SIZE); bos.writeInt(getFlags()); bos.writeInt(0); // size extra bos.writeInt(getCipherAlgorithm().ecmaId); bos.writeInt(getHashAlgorithm().ecmaId); bos.writeInt(getKeySize()); bos.writeInt(getCipherProvider().ecmaId); bos.writeInt(0); // reserved1 bos.writeInt(0); // reserved2 String cspName = getCspName(); if (cspName == null) { cspName = getCipherProvider().cipherProviderName; } bos.write(StringUtil.getToUnicodeLE(cspName)); bos.writeShort(0); int headerSize = bos.getWriteIndex()-startIdx-LittleEndianConsts.INT_SIZE; sizeOutput.writeInt(headerSize); }
@SuppressWarnings("resource") @Override public void serialize(LittleEndianOutput out) { out.writeShort(encryptionType); byte data[] = new byte[1024]; LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0); // NOSONAR 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()); }
public void write(LittleEndianByteArrayOutputStream os) { int start = os.getWriteIndex(); LittleEndianOutput sizeOut = os.createDelayedOutput(LittleEndianConsts.INT_SIZE); os.writeInt(referenceComponent.length); for (int i=0; i<referenceComponent.length; i++) { os.writeInt(referenceComponentType[i]); writeUnicodeLPP4(os, referenceComponent[i]); } writeUnicodeLPP4(os, dataSpaceName); sizeOut.writeInt(os.getWriteIndex()-start); } }
/** * 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) { LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset); // NOSONAR List<Ptg> arrayPtgs = null; for (Ptg ptg : ptgs) { ptg.write(out); if (ptg instanceof ArrayPtg) { if (arrayPtgs == null) { arrayPtgs = new ArrayList<>(5); } arrayPtgs.add(ptg); } } if (arrayPtgs != null) { for (Ptg arrayPtg : arrayPtgs) { ArrayPtg p = (ArrayPtg) arrayPtg; p.writeTokenValueBytes(out); } } return out.getWriteIndex() - offset; }
/** * Write the data content of this BIFF record including the sid and record length. * <p> * The subclass must write the exact number of bytes as reported by * {@link org.apache.poi.hssf.record.Record#getRecordSize()}} */ @Override public final int serialize(int offset, byte[] data) { int dataSize = getDataSize(); int recSize = 4 + dataSize; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); out.writeShort(getSid()); out.writeShort(dataSize); serialize(out); if (out.getWriteIndex() - offset != recSize) { throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): " + "Incorrect number of bytes written - expected " + recSize + " but got " + (out.getWriteIndex() - offset)); } return recSize; }
/** * Write the data content of this BIFF record including the sid and record length.<br/> * * The subclass must write the exact number of bytes as reported by * {@link org.apache.poi.hssf.record.Record#getRecordSize()}} */ @Override public final int serialize(int offset, byte[] data) { int dataSize = getDataSize(); int recSize = 4 + dataSize; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); out.writeShort(getSid()); out.writeShort(dataSize); serialize(out); if (out.getWriteIndex() - offset != recSize) { throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): " + "Incorrect number of bytes written - expected " + recSize + " but got " + (out.getWriteIndex() - offset)); } return recSize; }
/** * Write the data content of this BIFF record including the sid and record length.<br/> * * The subclass must write the exact number of bytes as reported by * {@link org.apache.poi.hssf.record.Record#getRecordSize()}} */ @Override public final int serialize(int offset, byte[] data) { int dataSize = getDataSize(); int recSize = 4 + dataSize; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); out.writeShort(getSid()); out.writeShort(dataSize); serialize(out); if (out.getWriteIndex() - offset != recSize) { throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): " + "Incorrect number of bytes written - expected " + recSize + " but got " + (out.getWriteIndex() - offset)); } return recSize; }
public int serialize(int offset, byte[] data) { int recSize = getRecordSize(); int dataSize = recSize - 4; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); out.writeShort(sid); out.writeShort(dataSize); if (_uninterpretedData == null) { for (int i = 0; i < subrecords.size(); i++) { SubRecord record = subrecords.get(i); record.serialize(out); } int expectedEndIx = offset+dataSize; // padding while (out.getWriteIndex() < expectedEndIx) { out.writeByte(0); } } else { out.write(_uninterpretedData); } return recSize; }
public int serialize(int offset, byte[] data) { int recSize = getRecordSize(); int dataSize = recSize - 4; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); out.writeShort(sid); out.writeShort(dataSize); if (_uninterpretedData == null) { for (int i = 0; i < subrecords.size(); i++) { SubRecord record = subrecords.get(i); record.serialize(out); } int expectedEndIx = offset+dataSize; // padding while (out.getWriteIndex() < expectedEndIx) { out.writeByte(0); } } else { out.write(_uninterpretedData); } return recSize; }
@Override public int serialize(int offset, byte[] data) { int recSize = getRecordSize(); int dataSize = recSize - 4; LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize); // NOSONAR out.writeShort(sid); out.writeShort(dataSize); if (_uninterpretedData == null) { for (int i = 0; i < subrecords.size(); i++) { SubRecord record = subrecords.get(i); record.serialize(out); } int expectedEndIx = offset+dataSize; // padding while (out.getWriteIndex() < expectedEndIx) { out.writeByte(0); } } else { out.write(_uninterpretedData); } return recSize; }
/** * Write the contents of the record back, so it can be written * to disk */ public void writeOut(OutputStream out) throws IOException { // Data byte data[] = new byte[1024]; LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0); bos.writeShort(ei.getVersionMajor()); bos.writeShort(ei.getVersionMinor()); bos.writeInt(ei.getEncryptionFlags()); ((CryptoAPIEncryptionHeader)ei.getHeader()).write(bos); ((CryptoAPIEncryptionVerifier)ei.getVerifier()).write(bos); // Header LittleEndian.putInt(_header, 4, bos.getWriteIndex()); out.write(_header); out.write(data, 0, bos.getWriteIndex()); bos.close(); }
public void write(LittleEndianByteArrayOutputStream bos) { int start = bos.getWriteIndex(); LittleEndianOutput sizeOut = bos.createDelayedOutput(LittleEndianConsts.INT_SIZE); bos.writeInt(transformType); writeUnicodeLPP4(bos, transformerId); sizeOut.writeInt(bos.getWriteIndex()-start); writeUnicodeLPP4(bos, transformerName); bos.writeShort(readerVersionMajor); bos.writeShort(readerVersionMinor); bos.writeShort(updaterVersionMajor); bos.writeShort(updaterVersionMinor); bos.writeShort(writerVersionMajor); bos.writeShort(writerVersionMinor); } }
public static DocumentEntry createEncryptionEntry(DirectoryEntry dir, String path, EncryptionRecord out) throws IOException { String parts[] = path.split("/"); for (int i=0; i<parts.length-1; i++) { dir = dir.hasEntry(parts[i]) ? (DirectoryEntry)dir.getEntry(parts[i]) : dir.createDirectory(parts[i]); } final byte buf[] = new byte[5000]; LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0); out.write(bos); String fileName = parts[parts.length-1]; if (dir.hasEntry(fileName)) { dir.getEntry(fileName).delete(); } return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener(){ public void processPOIFSWriterEvent(POIFSWriterEvent event) { try { event.getStream().write(buf, 0, event.getLimit()); } catch (IOException e) { throw new EncryptedDocumentException(e); } } }); }
/** * serializes the header */ @Override public void write(LittleEndianByteArrayOutputStream bos) { int startIdx = bos.getWriteIndex(); LittleEndianOutput sizeOutput = bos.createDelayedOutput(LittleEndianConsts.INT_SIZE); bos.writeInt(getFlags()); bos.writeInt(0); // size extra bos.writeInt(getCipherAlgorithm().ecmaId); bos.writeInt(getHashAlgorithm().ecmaId); bos.writeInt(getKeySize()); bos.writeInt(getCipherProvider().ecmaId); bos.writeInt(0); // reserved1 bos.writeInt(0); // reserved2 String cspName = getCspName(); if (cspName == null) { cspName = getCipherProvider().cipherProviderName; } bos.write(StringUtil.getToUnicodeLE(cspName)); bos.writeShort(0); int headerSize = bos.getWriteIndex()-startIdx-LittleEndianConsts.INT_SIZE; sizeOutput.writeInt(headerSize); }