/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.poifs.filesystem; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndianByteArrayInputStream; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutputStream; import org.apache.poi.util.StringUtil; /** * Represents an Ole10Native record which is wrapped around certain binary * files being embedded in OLE2 documents.
* * Ole10Native objects come in different shapes: *
null
if no data was embedded. Note that an embedding may
* provide information about the data, but the actual data is not included.
* (So label, filename etc. are available, but this method returns
* null
.)
*
* @return the dataBuffer
*/
public byte[] getDataBuffer() {
return dataBuffer;
}
/**
* 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();
try (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());
if (command2 == null || label2 == null || fileName2 == null) {
leos.writeShort(0);
} else {
leos.writeUInt(command2.length());
leos.write(StringUtil.getToUnicodeLE(command2));
leos.writeUInt(label2.length());
leos.write(StringUtil.getToUnicodeLE(label2));
leos.writeUInt(fileName2.length());
leos.write(StringUtil.getToUnicodeLE(fileName2));
}
}
// 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;
}
}
public void setFlags1(short flags1) {
this.flags1 = flags1;
}
public void setFlags2(short flags2) {
this.flags2 = flags2;
}
public void setLabel(String label) {
this.label = label;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setCommand(String command) {
this.command = command;
}
public void setUnknown1(short unknown1) {
this.unknown1 = unknown1;
}
public void setDataBuffer(byte[] dataBuffer) {
this.dataBuffer = dataBuffer.clone();
}
/**
* Get Command string of UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
*/
public String getCommand2() {
return command2;
}
/**
* Set Command string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
*/
public void setCommand2(String command2) {
this.command2 = command2;
}
/**
* Get Label string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
*/
public String getLabel2() {
return label2;
}
/**
* Set Label string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
*/
public void setLabel2(String label2) {
this.label2 = label2;
}
/**
* Get filename string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
*/
public String getFileName2() {
return fileName2;
}
/**
* Set filename string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
*/
public void setFileName2(String fileName2) {
this.fileName2 = fileName2;
}
}