Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / ddf / EscherBlipWMFRecord.java
blob54217e1bb12ea4f465a56c8a6da02a0d0edc8d6c
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
19 package org.apache.poi.ddf;
21 import org.apache.poi.hssf.record.RecordFormatException;
22 import org.apache.poi.util.HexDump;
23 import org.apache.poi.util.LittleEndian;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.InputStream;
28 import java.io.IOException;
29 import java.util.zip.InflaterInputStream;
30 import java.util.zip.DeflaterOutputStream;
32 /**
33 * The blip record is used to hold details about large binary objects that occur in escher such
34 * as JPEG, GIF, PICT and WMF files. The contents of the stream is usually compressed. Inflate
35 * can be used to decompress the data.
37 * @author Glen Stampoultzis
38 * @see java.util.zip.Inflater
40 public class EscherBlipWMFRecord
41 extends EscherBlipRecord
43 // public static final short RECORD_ID_START = (short) 0xF018;
44 // public static final short RECORD_ID_END = (short) 0xF117;
45 public static final String RECORD_DESCRIPTION = "msofbtBlip";
46 private static final int HEADER_SIZE = 8;
48 private byte[] field_1_secondaryUID;
49 private int field_2_cacheOfSize;
50 private int field_3_boundaryTop;
51 private int field_4_boundaryLeft;
52 private int field_5_boundaryWidth;
53 private int field_6_boundaryHeight;
54 private int field_7_width;
55 private int field_8_height;
56 private int field_9_cacheOfSavedSize;
57 private byte field_10_compressionFlag;
58 private byte field_11_filter;
59 private byte[] field_12_data;
62 /**
63 * This method deserializes the record from a byte array.
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.
70 public int fillFields( byte[] data, int offset,
71 EscherRecordFactory recordFactory
74 int bytesAfterHeader = readHeader( data, offset );
75 int pos = offset + HEADER_SIZE;
77 int size = 0;
78 field_1_secondaryUID = new byte[16];
79 System.arraycopy( data, pos + size, field_1_secondaryUID, 0, 16 ); size += 16;
80 field_2_cacheOfSize = LittleEndian.getInt( data, pos + size );size+=4;
81 field_3_boundaryTop = LittleEndian.getInt( data, pos + size );size+=4;
82 field_4_boundaryLeft = LittleEndian.getInt( data, pos + size );size+=4;
83 field_5_boundaryWidth = LittleEndian.getInt( data, pos + size );size+=4;
84 field_6_boundaryHeight = LittleEndian.getInt( data, pos + size );size+=4;
85 field_7_width = LittleEndian.getInt( data, pos + size );size+=4;
86 field_8_height = LittleEndian.getInt( data, pos + size );size+=4;
87 field_9_cacheOfSavedSize = LittleEndian.getInt( data, pos + size );size+=4;
88 field_10_compressionFlag = data[pos + size]; size++;
89 field_11_filter = data[pos + size]; size++;
91 int bytesRemaining = bytesAfterHeader - size;
92 field_12_data = new byte[bytesRemaining];
93 System.arraycopy(data, pos + size, field_12_data, 0, bytesRemaining);
94 size += bytesRemaining;
96 return HEADER_SIZE + size;
101 * This method serializes this escher record into a byte array.
103 * @param offset The offset into <code>data</code> to start writing the record data to.
104 * @param data The byte array to serialize to.
105 * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
106 * @return The number of bytes written.
108 * @see NullEscherSerializationListener
110 public int serialize( int offset, byte[] data, EscherSerializationListener listener )
112 listener.beforeRecordSerialize(offset, getRecordId(), this);
114 LittleEndian.putShort( data, offset, getOptions() );
115 LittleEndian.putShort( data, offset + 2, getRecordId() );
116 int remainingBytes = field_12_data.length + 36;
117 LittleEndian.putInt( data, offset + 4, remainingBytes );
119 int pos = offset + HEADER_SIZE;
120 System.arraycopy(field_1_secondaryUID, 0, data, pos, 16 ); pos += 16;
121 LittleEndian.putInt( data, pos, field_2_cacheOfSize); pos += 4;
122 LittleEndian.putInt( data, pos, field_3_boundaryTop); pos += 4;
123 LittleEndian.putInt( data, pos, field_4_boundaryLeft); pos += 4;
124 LittleEndian.putInt( data, pos, field_5_boundaryWidth); pos += 4;
125 LittleEndian.putInt( data, pos, field_6_boundaryHeight); pos += 4;
126 LittleEndian.putInt( data, pos, field_7_width); pos += 4;
127 LittleEndian.putInt( data, pos, field_8_height); pos += 4;
128 LittleEndian.putInt( data, pos, field_9_cacheOfSavedSize); pos += 4;
129 data[pos++] = field_10_compressionFlag;
130 data[pos++] = field_11_filter;
131 System.arraycopy(field_12_data, 0, data, pos, field_12_data.length); pos += field_12_data.length;
133 listener.afterRecordSerialize(pos, getRecordId(), pos - offset, this);
134 return pos - offset;
138 * Returns the number of bytes that are required to serialize this record.
140 * @return Number of bytes
142 public int getRecordSize()
144 return 58 + field_12_data.length;
148 * The short name for this record
150 public String getRecordName()
152 return "Blip";
156 * Retrieve the secondary UID
158 public byte[] getSecondaryUID()
160 return field_1_secondaryUID;
164 * Set the secondary UID
166 public void setSecondaryUID( byte[] field_1_secondaryUID )
168 this.field_1_secondaryUID = field_1_secondaryUID;
172 * Retrieve the cache of the metafile size
174 public int getCacheOfSize()
176 return field_2_cacheOfSize;
180 * Set the cache of the metafile size
182 public void setCacheOfSize( int field_2_cacheOfSize )
184 this.field_2_cacheOfSize = field_2_cacheOfSize;
188 * Retrieve the top boundary of the metafile drawing commands
190 public int getBoundaryTop()
192 return field_3_boundaryTop;
196 * Set the top boundary of the metafile drawing commands
198 public void setBoundaryTop( int field_3_boundaryTop )
200 this.field_3_boundaryTop = field_3_boundaryTop;
204 * Retrieve the left boundary of the metafile drawing commands
206 public int getBoundaryLeft()
208 return field_4_boundaryLeft;
212 * Set the left boundary of the metafile drawing commands
214 public void setBoundaryLeft( int field_4_boundaryLeft )
216 this.field_4_boundaryLeft = field_4_boundaryLeft;
220 * Retrieve the boundary width of the metafile drawing commands
222 public int getBoundaryWidth()
224 return field_5_boundaryWidth;
228 * Set the boundary width of the metafile drawing commands
230 public void setBoundaryWidth( int field_5_boundaryWidth )
232 this.field_5_boundaryWidth = field_5_boundaryWidth;
236 * Retrieve the boundary height of the metafile drawing commands
238 public int getBoundaryHeight()
240 return field_6_boundaryHeight;
244 * Set the boundary height of the metafile drawing commands
246 public void setBoundaryHeight( int field_6_boundaryHeight )
248 this.field_6_boundaryHeight = field_6_boundaryHeight;
252 * Retrieve the width of the metafile in EMU's (English Metric Units).
254 public int getWidth()
256 return field_7_width;
260 * Set the width of the metafile in EMU's (English Metric Units).
262 public void setWidth( int width )
264 this.field_7_width = width;
268 * Retrieve the height of the metafile in EMU's (English Metric Units).
270 public int getHeight()
272 return field_8_height;
276 * Set the height of the metafile in EMU's (English Metric Units).
278 public void setHeight( int height )
280 this.field_8_height = height;
284 * Retrieve the cache of the saved size
286 public int getCacheOfSavedSize()
288 return field_9_cacheOfSavedSize;
292 * Set the cache of the saved size
294 public void setCacheOfSavedSize( int field_9_cacheOfSavedSize )
296 this.field_9_cacheOfSavedSize = field_9_cacheOfSavedSize;
300 * Is the contents of the blip compressed?
302 public byte getCompressionFlag()
304 return field_10_compressionFlag;
308 * Set whether the contents of the blip is compressed
310 public void setCompressionFlag( byte field_10_compressionFlag )
312 this.field_10_compressionFlag = field_10_compressionFlag;
316 * Filter should always be 0
318 public byte getFilter()
320 return field_11_filter;
324 * Filter should always be 0
326 public void setFilter( byte field_11_filter )
328 this.field_11_filter = field_11_filter;
332 * The BLIP data
334 public byte[] getData()
336 return field_12_data;
340 * The BLIP data
342 public void setData( byte[] field_12_data )
344 this.field_12_data = field_12_data;
348 * The string representation of this record.
350 * @return A string
352 public String toString()
354 String nl = System.getProperty( "line.separator" );
356 String extraData;
357 ByteArrayOutputStream b = new ByteArrayOutputStream();
360 HexDump.dump( this.field_12_data, 0, b, 0 );
361 extraData = b.toString();
363 catch ( Exception e )
365 extraData = e.toString();
367 return getClass().getName() + ":" + nl +
368 " RecordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
369 " Options: 0x" + HexDump.toHex( getOptions() ) + nl +
370 " Secondary UID: " + HexDump.toHex( field_1_secondaryUID ) + nl +
371 " CacheOfSize: " + field_2_cacheOfSize + nl +
372 " BoundaryTop: " + field_3_boundaryTop + nl +
373 " BoundaryLeft: " + field_4_boundaryLeft + nl +
374 " BoundaryWidth: " + field_5_boundaryWidth + nl +
375 " BoundaryHeight: " + field_6_boundaryHeight + nl +
376 " X: " + field_7_width + nl +
377 " Y: " + field_8_height + nl +
378 " CacheOfSavedSize: " + field_9_cacheOfSavedSize + nl +
379 " CompressionFlag: " + field_10_compressionFlag + nl +
380 " Filter: " + field_11_filter + nl +
381 " Data:" + nl + extraData;
385 * Compress the contents of the provided array
387 * @param data An uncompressed byte array
388 * @see DeflaterOutputStream#write(int b)
390 public static byte[] compress( byte[] data )
392 ByteArrayOutputStream out = new ByteArrayOutputStream();
393 DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream( out );
396 for ( int i = 0; i < data.length; i++ )
397 deflaterOutputStream.write( data[i] );
399 catch ( IOException e )
401 throw new RecordFormatException( e.toString() );
404 return out.toByteArray();
408 * Decompresses a byte array.
410 * @param data The compressed byte array
411 * @param pos The starting position into the byte array
412 * @param length The number of compressed bytes to decompress
413 * @return An uncompressed byte array
414 * @see InflaterInputStream#read
416 public static byte[] decompress( byte[] data, int pos, int length )
418 byte[] compressedData = new byte[length];
419 System.arraycopy( data, pos + 50, compressedData, 0, length );
420 InputStream compressedInputStream = new ByteArrayInputStream( compressedData );
421 InflaterInputStream inflaterInputStream = new InflaterInputStream( compressedInputStream );
422 ByteArrayOutputStream out = new ByteArrayOutputStream();
423 int c;
426 while ( ( c = inflaterInputStream.read() ) != -1 )
427 out.write( c );
429 catch ( IOException e )
431 throw new RecordFormatException( e.toString() );
433 return out.toByteArray();