KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > ddf > EscherClientDataRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.ddf;
19
20 import org.apache.poi.util.HexDump;
21 import org.apache.poi.util.LittleEndian;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24
25 /**
26  * The EscherClientDataRecord is used to store client specific data about the position of a
27  * shape within a container.
28  *
29  * @author Glen Stampoultzis
30  */

31 public class EscherClientDataRecord
32     extends EscherRecord
33 {
34     public static final short RECORD_ID = (short) 0xF011;
35     public static final String JavaDoc RECORD_DESCRIPTION = "MsofbtClientData";
36
37     private byte[] remainingData;
38
39     /**
40      * This method deserializes the record from a byte array.
41      *
42      * @param data The byte array containing the escher record information
43      * @param offset The starting offset into <code>data</code>.
44      * @param recordFactory May be null since this is not a container record.
45      * @return The number of bytes read from the byte array.
46      */

47     public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory )
48     {
49         int bytesRemaining = readHeader( data, offset );
50         int pos = offset + 8;
51         remainingData = new byte[bytesRemaining];
52         System.arraycopy( data, pos, remainingData, 0, bytesRemaining );
53         return 8 + bytesRemaining;
54     }
55
56     /**
57      * This method serializes this escher record into a byte array.
58      *
59      * @param offset The offset into <code>data</code> to start writing the record data to.
60      * @param data The byte array to serialize to.
61      * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
62      * @return The number of bytes written.
63      * @see NullEscherSerializationListener
64      */

65     public int serialize( int offset, byte[] data, EscherSerializationListener listener )
66     {
67         listener.beforeRecordSerialize( offset, getRecordId(), this );
68
69         if (remainingData == null) remainingData = new byte[0];
70         LittleEndian.putShort( data, offset, getOptions() );
71         LittleEndian.putShort( data, offset + 2, getRecordId() );
72         LittleEndian.putInt( data, offset + 4, remainingData.length );
73         System.arraycopy( remainingData, 0, data, offset + 8, remainingData.length );
74         int pos = offset + 8 + remainingData.length;
75
76         listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
77         return pos - offset;
78     }
79
80     /**
81      * Returns the number of bytes that are required to serialize this record.
82      *
83      * @return Number of bytes
84      */

85     public int getRecordSize()
86     {
87         return 8 + (remainingData == null ? 0 : remainingData.length);
88     }
89
90     /**
91      * Returns the identifier of this record.
92      */

93     public short getRecordId()
94     {
95         return RECORD_ID;
96     }
97
98     /**
99      * The short name for this record
100      */

101     public String JavaDoc getRecordName()
102     {
103         return "ClientData";
104     }
105
106     /**
107      * Returns the string representation of this record.
108      */

109     public String JavaDoc toString()
110     {
111         String JavaDoc nl = System.getProperty("line.separator");
112
113         String JavaDoc extraData;
114         ByteArrayOutputStream JavaDoc b = new ByteArrayOutputStream JavaDoc();
115         try
116         {
117             HexDump.dump(this.remainingData, 0, b, 0);
118             extraData = b.toString();
119         }
120         catch ( Exception JavaDoc e )
121         {
122             extraData = "error";
123         }
124         return getClass().getName() + ":" + nl +
125                 " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl +
126                 " Options: 0x" + HexDump.toHex(getOptions()) + nl +
127                 " Extra Data:" + nl +
128                 extraData;
129
130     }
131
132     /**
133      * Any data recording this record.
134      */

135     public byte[] getRemainingData()
136     {
137         return remainingData;
138     }
139
140     /**
141      * Any data recording this record.
142      */

143     public void setRemainingData( byte[] remainingData )
144     {
145         this.remainingData = remainingData;
146     }
147 }
148
Popular Tags