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.HexDump;
021: import org.apache.poi.util.LittleEndian;
022:
023: import java.io.ByteArrayOutputStream;
024:
025: /**
026: * The EscherClientDataRecord is used to store client specific data about the position of a
027: * shape within a container.
028: *
029: * @author Glen Stampoultzis
030: */
031: public class EscherClientDataRecord extends EscherRecord {
032: public static final short RECORD_ID = (short) 0xF011;
033: public static final String RECORD_DESCRIPTION = "MsofbtClientData";
034:
035: private byte[] remainingData;
036:
037: /**
038: * This method deserializes the record from a byte array.
039: *
040: * @param data The byte array containing the escher record information
041: * @param offset The starting offset into <code>data</code>.
042: * @param recordFactory May be null since this is not a container record.
043: * @return The number of bytes read from the byte array.
044: */
045: public int fillFields(byte[] data, int offset,
046: EscherRecordFactory recordFactory) {
047: int bytesRemaining = readHeader(data, offset);
048: int pos = offset + 8;
049: remainingData = new byte[bytesRemaining];
050: System.arraycopy(data, pos, remainingData, 0, bytesRemaining);
051: return 8 + bytesRemaining;
052: }
053:
054: /**
055: * This method serializes this escher record into a byte array.
056: *
057: * @param offset The offset into <code>data</code> to start writing the record data to.
058: * @param data The byte array to serialize to.
059: * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
060: * @return The number of bytes written.
061: * @see NullEscherSerializationListener
062: */
063: public int serialize(int offset, byte[] data,
064: EscherSerializationListener listener) {
065: listener.beforeRecordSerialize(offset, getRecordId(), this );
066:
067: if (remainingData == null)
068: remainingData = new byte[0];
069: LittleEndian.putShort(data, offset, getOptions());
070: LittleEndian.putShort(data, offset + 2, getRecordId());
071: LittleEndian.putInt(data, offset + 4, remainingData.length);
072: System.arraycopy(remainingData, 0, data, offset + 8,
073: remainingData.length);
074: int pos = offset + 8 + remainingData.length;
075:
076: listener.afterRecordSerialize(pos, getRecordId(), pos - offset,
077: this );
078: return pos - offset;
079: }
080:
081: /**
082: * Returns the number of bytes that are required to serialize this record.
083: *
084: * @return Number of bytes
085: */
086: public int getRecordSize() {
087: return 8 + (remainingData == null ? 0 : remainingData.length);
088: }
089:
090: /**
091: * Returns the identifier of this record.
092: */
093: public short getRecordId() {
094: return RECORD_ID;
095: }
096:
097: /**
098: * The short name for this record
099: */
100: public String getRecordName() {
101: return "ClientData";
102: }
103:
104: /**
105: * Returns the string representation of this record.
106: */
107: public String toString() {
108: String nl = System.getProperty("line.separator");
109:
110: String extraData;
111: ByteArrayOutputStream b = new ByteArrayOutputStream();
112: try {
113: HexDump.dump(this .remainingData, 0, b, 0);
114: extraData = b.toString();
115: } catch (Exception e) {
116: extraData = "error\n";
117: }
118: return getClass().getName() + ":" + nl + " RecordId: 0x"
119: + HexDump.toHex(RECORD_ID) + nl + " Options: 0x"
120: + HexDump.toHex(getOptions()) + nl + " Extra Data:"
121: + nl + extraData;
122:
123: }
124:
125: /**
126: * Any data recording this record.
127: */
128: public byte[] getRemainingData() {
129: return remainingData;
130: }
131:
132: /**
133: * Any data recording this record.
134: */
135: public void setRemainingData(byte[] remainingData) {
136: this.remainingData = remainingData;
137: }
138: }
|