KickJava   Java API By Example, From Geeks To Geeks.

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


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.LittleEndian;
21 import org.apache.poi.util.HexDump;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 /**
27  * The BSE record is related closely to the <code>EscherBlipRecord</code> and stores
28  * extra information about the blip.
29  *
30  * @author Glen Stampoultzis
31  * @see EscherBlipRecord
32  */

33 public class EscherBSERecord
34         extends EscherRecord
35 {
36     public static final short RECORD_ID = (short) 0xF007;
37     public static final String JavaDoc RECORD_DESCRIPTION = "MsofbtBSE";
38
39     public static final byte BT_ERROR = 0;
40     public static final byte BT_UNKNOWN = 1;
41     public static final byte BT_EMF = 2;
42     public static final byte BT_WMF = 3;
43     public static final byte BT_PICT = 4;
44     public static final byte BT_JPEG = 5;
45     public static final byte BT_PNG = 6;
46     public static final byte BT_DIB = 7;
47
48     private byte field_1_blipTypeWin32;
49     private byte field_2_blipTypeMacOS;
50     private byte[] field_3_uid; // 16 bytes
51
private short field_4_tag;
52     private int field_5_size;
53     private int field_6_ref;
54     private int field_7_offset;
55     private byte field_8_usage;
56     private byte field_9_name;
57     private byte field_10_unused2;
58     private byte field_11_unused3;
59
60     private byte[] remainingData;
61
62     /**
63      * This method deserializes the record from a byte array.
64      *
65      * @param data The byte array containing the escher record information
66      * @param offset The starting offset into <code>data</code>.
67      * @param recordFactory May be null since this is not a container record.
68      * @return The number of bytes read from the byte array.
69      */

70     public int fillFields( byte[] data, int offset,
71                               EscherRecordFactory recordFactory
72                               )
73     {
74         int bytesRemaining = readHeader( data, offset );
75         int pos = offset + 8;
76         field_1_blipTypeWin32 = data[pos];
77         field_2_blipTypeMacOS = data[pos + 1];
78         System.arraycopy( data, pos + 2, field_3_uid = new byte[16], 0, 16 );
79         field_4_tag = LittleEndian.getShort( data, pos + 18 );
80         field_5_size = LittleEndian.getInt( data, pos + 20 );
81         field_6_ref = LittleEndian.getInt( data, pos + 24 );
82         field_7_offset = LittleEndian.getInt( data, pos + 28 );
83         field_8_usage = data[pos + 32];
84         field_9_name = data[pos + 33];
85         field_10_unused2 = data[pos + 34];
86         field_11_unused3 = data[pos + 35];
87         bytesRemaining -= 36;
88         remainingData = new byte[bytesRemaining];
89         System.arraycopy( data, pos + 36, remainingData, 0, bytesRemaining );
90         return bytesRemaining + 8 + 36;
91
92     }
93
94     /**
95      * This method serializes this escher record into a byte array.
96      *
97      * @param offset The offset into <code>data</code> to start writing the record data to.
98      * @param data The byte array to serialize to.
99      * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
100      * @return The number of bytes written.
101      * @see NullEscherSerializationListener
102      */

103     public int serialize( int offset, byte[] data, EscherSerializationListener listener )
104     {
105         listener.beforeRecordSerialize( offset, getRecordId(), this );
106
107         LittleEndian.putShort( data, offset, getOptions() );
108         LittleEndian.putShort( data, offset + 2, getRecordId() );
109         int remainingBytes = remainingData.length + 36;
110         LittleEndian.putInt( data, offset + 4, remainingBytes );
111
112         data[offset + 8] = field_1_blipTypeWin32;
113         data[offset + 9] = field_2_blipTypeMacOS;
114         for ( int i = 0; i < 16; i++ )
115             data[offset + 10 + i] = field_3_uid[i];
116         LittleEndian.putShort( data, offset + 26, field_4_tag );
117         LittleEndian.putInt( data, offset + 28, field_5_size );
118         LittleEndian.putInt( data, offset + 32, field_6_ref );
119         LittleEndian.putInt( data, offset + 36, field_7_offset );
120         data[offset + 40] = field_8_usage;
121         data[offset + 41] = field_9_name;
122         data[offset + 42] = field_10_unused2;
123         data[offset + 43] = field_11_unused3;
124         System.arraycopy( remainingData, 0, data, offset + 44, remainingData.length );
125         int pos = offset + 8 + 36 + remainingData.length;
126
127         listener.afterRecordSerialize(pos, getRecordId(), pos - offset, this);
128         return pos - offset;
129     }
130
131     /**
132      * Returns the number of bytes that are required to serialize this record.
133      *
134      * @return Number of bytes
135      */

136     public int getRecordSize()
137     {
138         return 8 + 1 + 1 + 16 + 2 + 4 + 4 + 4 + 1 + 1 + 1 + 1 + remainingData.length;
139     }
140
141     /**
142      * The short name for this record
143      */

144     public String JavaDoc getRecordName()
145     {
146         return "BSE";
147     }
148
149     /**
150      * The expected blip type under windows (failure to match this blip type will result in
151      * Excel converting to this format).
152      */

153     public byte getBlipTypeWin32()
154     {
155         return field_1_blipTypeWin32;
156     }
157
158     /**
159      * Set the expected win32 blip type
160      */

161     public void setBlipTypeWin32( byte blipTypeWin32 )
162     {
163         this.field_1_blipTypeWin32 = blipTypeWin32;
164     }
165
166     /**
167      * The expected blip type under MacOS (failure to match this blip type will result in
168      * Excel converting to this format).
169      */

170     public byte getBlipTypeMacOS()
171     {
172         return field_2_blipTypeMacOS;
173     }
174
175     /**
176      * Set the expected MacOS blip type
177      */

178     public void setBlipTypeMacOS( byte blipTypeMacOS )
179     {
180         this.field_2_blipTypeMacOS = blipTypeMacOS;
181     }
182
183     /**
184      * 16 byte MD4 checksum.
185      */

186     public byte[] getUid()
187     {
188         return field_3_uid;
189     }
190
191     /**
192      * 16 byte MD4 checksum.
193      */

194     public void setUid( byte[] uid )
195     {
196         this.field_3_uid = uid;
197     }
198
199     /**
200      * unused
201      */

202     public short getTag()
203     {
204         return field_4_tag;
205     }
206
207     /**
208      * unused
209      */

210     public void setTag( short tag )
211     {
212         this.field_4_tag = tag;
213     }
214
215     /**
216      * Blip size in stream.
217      */

218     public int getSize()
219     {
220         return field_5_size;
221     }
222
223     /**
224      * Blip size in stream.
225      */

226     public void setSize( int size )
227     {
228         this.field_5_size = size;
229     }
230
231     /**
232      * The reference count of this blip.
233      */

234     public int getRef()
235     {
236         return field_6_ref;
237     }
238
239     /**
240      * The reference count of this blip.
241      */

242     public void setRef( int ref )
243     {
244         this.field_6_ref = ref;
245     }
246
247     /**
248      * File offset in the delay stream.
249      */

250     public int getOffset()
251     {
252         return field_7_offset;
253     }
254
255     /**
256      * File offset in the delay stream.
257      */

258     public void setOffset( int offset )
259     {
260         this.field_7_offset = offset;
261     }
262
263     /**
264      * Defines the way this blip is used.
265      */

266     public byte getUsage()
267     {
268         return field_8_usage;
269     }
270
271     /**
272      * Defines the way this blip is used.
273      */

274     public void setUsage( byte usage )
275     {
276         this.field_8_usage = usage;
277     }
278
279     /**
280      * The length in characters of the blip name.
281      */

282     public byte getName()
283     {
284         return field_9_name;
285     }
286
287     /**
288      * The length in characters of the blip name.
289      */

290     public void setName( byte name )
291     {
292         this.field_9_name = name;
293     }
294
295     public byte getUnused2()
296     {
297         return field_10_unused2;
298     }
299
300     public void setUnused2( byte unused2 )
301     {
302         this.field_10_unused2 = unused2;
303     }
304
305     public byte getUnused3()
306     {
307         return field_11_unused3;
308     }
309
310     public void setUnused3( byte unused3 )
311     {
312         this.field_11_unused3 = unused3;
313     }
314
315     /**
316      * Any remaining data in this record.
317      */

318     public byte[] getRemainingData()
319     {
320         return remainingData;
321     }
322
323     /**
324      * Any remaining data in this record.
325      */

326     public void setRemainingData( byte[] remainingData )
327     {
328         this.remainingData = remainingData;
329     }
330
331     /**
332      * Calculate the string representation of this object
333      */

334     public String JavaDoc toString()
335     {
336         String JavaDoc nl = System.getProperty( "line.separator" );
337
338         String JavaDoc extraData;
339         ByteArrayOutputStream JavaDoc b = new ByteArrayOutputStream JavaDoc();
340         try
341         {
342             HexDump.dump( this.remainingData, 0, b, 0 );
343             extraData = b.toString();
344         }
345         catch ( Exception JavaDoc e )
346         {
347             extraData = e.toString();
348         }
349         return getClass().getName() + ":" + nl +
350                 " RecordId: 0x" + HexDump.toHex( RECORD_ID ) + nl +
351                 " Options: 0x" + HexDump.toHex( getOptions() ) + nl +
352                 " BlipTypeWin32: " + field_1_blipTypeWin32 + nl +
353                 " BlipTypeMacOS: " + field_2_blipTypeMacOS + nl +
354                 " SUID: " + HexDump.toHex(field_3_uid) + nl +
355                 " Tag: " + field_4_tag + nl +
356                 " Size: " + field_5_size + nl +
357                 " Ref: " + field_6_ref + nl +
358                 " Offset: " + field_7_offset + nl +
359                 " Usage: " + field_8_usage + nl +
360                 " Name: " + field_9_name + nl +
361                 " Unused2: " + field_10_unused2 + nl +
362                 " Unused3: " + field_11_unused3 + nl +
363                 " Extra Data:" + nl + extraData;
364
365
366     }
367
368     /**
369      * Retrieve the string representation given a blip id.
370      */

371     public String JavaDoc getBlipType( byte b )
372     {
373         switch ( b )
374         {
375             case BT_ERROR:
376                 return " ERROR";
377             case BT_UNKNOWN:
378                 return " UNKNOWN";
379             case BT_EMF:
380                 return " EMF";
381             case BT_WMF:
382                 return " WMF";
383             case BT_PICT:
384                 return " PICT";
385             case BT_JPEG:
386                 return " JPEG";
387             case BT_PNG:
388                 return " PNG";
389             case BT_DIB:
390                 return " DIB";
391             default:
392                 if ( b < 32 )
393                     return " NotKnown";
394                 else
395                     return " Client";
396         }
397     }
398
399
400 }
401
Popular Tags