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.ddf;
019:
020: import org.apache.poi.util.LittleEndian;
021: import org.apache.poi.util.HexDump;
022:
023: import java.io.ByteArrayOutputStream;
024:
025: /**
026: * @author Glen Stampoultzis
027: * @version $Id: EscherBlipRecord.java 569827 2007-08-26 15:26:29Z yegor $
028: */
029: public class EscherBlipRecord extends EscherRecord {
030: public static final short RECORD_ID_START = (short) 0xF018;
031: public static final short RECORD_ID_END = (short) 0xF117;
032: public static final String RECORD_DESCRIPTION = "msofbtBlip";
033:
034: private static final int HEADER_SIZE = 8;
035:
036: protected byte[] field_pictureData;
037:
038: public EscherBlipRecord() {
039: }
040:
041: /**
042: * This method deserializes the record from a byte array.
043: *
044: * @param data The byte array containing the escher record information
045: * @param offset The starting offset into <code>data</code>.
046: * @param recordFactory May be null since this is not a container record.
047: * @return The number of bytes read from the byte array.
048: */
049: public int fillFields(byte[] data, int offset,
050: EscherRecordFactory recordFactory) {
051: int bytesAfterHeader = readHeader(data, offset);
052: int pos = offset + HEADER_SIZE;
053:
054: field_pictureData = new byte[bytesAfterHeader];
055: System.arraycopy(data, pos, field_pictureData, 0,
056: bytesAfterHeader);
057:
058: return bytesAfterHeader + 8;
059: }
060:
061: /**
062: * Serializes the record to an existing byte array.
063: *
064: * @param offset the offset within the byte array
065: * @param data the data array to serialize to
066: * @param listener a listener for begin and end serialization events. This
067: * is useful because the serialization is
068: * hierarchical/recursive and sometimes you need to be able
069: * break into that.
070: * @return the number of bytes written.
071: */
072: public int serialize(int offset, byte[] data,
073: EscherSerializationListener listener) {
074: listener.beforeRecordSerialize(offset, getRecordId(), this );
075:
076: LittleEndian.putShort(data, offset, getOptions());
077: LittleEndian.putShort(data, offset + 2, getRecordId());
078:
079: System.arraycopy(field_pictureData, 0, data, offset + 4,
080: field_pictureData.length);
081:
082: listener.afterRecordSerialize(offset + 4
083: + field_pictureData.length, getRecordId(),
084: field_pictureData.length + 4, this );
085: return field_pictureData.length + 4;
086: }
087:
088: /**
089: * Returns the number of bytes that are required to serialize this record.
090: *
091: * @return Number of bytes
092: */
093: public int getRecordSize() {
094: return field_pictureData.length + HEADER_SIZE;
095: }
096:
097: /**
098: * The short name for this record
099: */
100: public String getRecordName() {
101: return "Blip";
102: }
103:
104: public byte[] getPicturedata() {
105: return field_pictureData;
106: }
107:
108: public void setPictureData(byte[] pictureData) {
109: field_pictureData = pictureData;
110: }
111:
112: public String toString() {
113: String nl = System.getProperty("line.separator");
114:
115: String extraData;
116: ByteArrayOutputStream b = new ByteArrayOutputStream();
117: try {
118: HexDump.dump(this .field_pictureData, 0, b, 0);
119: extraData = b.toString();
120: } catch (Exception e) {
121: extraData = e.toString();
122: }
123: return getClass().getName() + ":" + nl + " RecordId: 0x"
124: + HexDump.toHex(getRecordId()) + nl + " Options: 0x"
125: + HexDump.toHex(getOptions()) + nl + " Extra Data:"
126: + nl + extraData;
127:
128: }
129: }
|