001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.record;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.util.zip.InflaterInputStream;
025:
026: import org.apache.poi.util.LittleEndian;
027:
028: /**
029: * Storage for embedded OLE objects.
030: *
031: * @author Daniel Noll
032: */
033: public class ExOleObjStg extends RecordAtom {
034: /**
035: * Record header.
036: */
037: private byte[] _header;
038:
039: /**
040: * Record data.
041: */
042: private byte[] _data;
043:
044: /**
045: * Constructs a new empty storage container.
046: */
047: protected ExOleObjStg() {
048: _header = new byte[8];
049: _data = new byte[0];
050:
051: LittleEndian.putShort(_header, 2, (short) getRecordType());
052: LittleEndian.putInt(_header, 4, _data.length);
053: }
054:
055: /**
056: * Constructs the link related atom record from its
057: * source data.
058: *
059: * @param source the source data as a byte array.
060: * @param start the start offset into the byte array.
061: * @param len the length of the slice in the byte array.
062: */
063: protected ExOleObjStg(byte[] source, int start, int len) {
064: // Get the header.
065: _header = new byte[8];
066: System.arraycopy(source, start, _header, 0, 8);
067:
068: // Get the record data.
069: _data = new byte[len - 8];
070: System.arraycopy(source, start + 8, _data, 0, len - 8);
071: }
072:
073: /**
074: * Gets the uncompressed length of the data.
075: *
076: * @return the uncompressed length of the data.
077: */
078: public int getDataLength() {
079: return LittleEndian.getInt(_data, 0);
080: }
081:
082: /**
083: * Opens an input stream which will decompress the data on the fly.
084: *
085: * @return the data input stream.
086: */
087: public InputStream getData() {
088: InputStream compressedStream = new ByteArrayInputStream(_data,
089: 4, _data.length);
090: return new InflaterInputStream(compressedStream);
091: }
092:
093: /**
094: * Gets the record type.
095: *
096: * @return the record type.
097: */
098: public long getRecordType() {
099: return RecordTypes.ExOleObjStg.typeID;
100: }
101:
102: /**
103: * Write the contents of the record back, so it can be written
104: * to disk.
105: *
106: * @param out the output stream to write to.
107: * @throws IOException if an error occurs.
108: */
109: public void writeOut(OutputStream out) throws IOException {
110: out.write(_header);
111: out.write(_data);
112: }
113: }
|