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