PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/ddf/EscherBSERecord.java

http://github.com/openmicroscopy/bioformats
Java | 464 lines | 266 code | 49 blank | 149 comment | 15 complexity | 8eff335d4d8014a4a1105e64c94acc96 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.ddf;
  38. import loci.poi.util.HexDump;
  39. import loci.poi.util.LittleEndian;
  40. import java.io.ByteArrayOutputStream;
  41. /**
  42. * The BSE record is related closely to the <code>EscherBlipRecord</code> and stores
  43. * extra information about the blip. A blip record is actually stored inside
  44. * the BSE record even though the BSE record isn't actually a container record.
  45. *
  46. * @author Glen Stampoultzis
  47. * @see EscherBlipRecord
  48. */
  49. public class EscherBSERecord
  50. extends EscherRecord
  51. {
  52. public static final short RECORD_ID = (short) 0xF007;
  53. public static final String RECORD_DESCRIPTION = "MsofbtBSE";
  54. public static final byte BT_ERROR = 0;
  55. public static final byte BT_UNKNOWN = 1;
  56. public static final byte BT_EMF = 2;
  57. public static final byte BT_WMF = 3;
  58. public static final byte BT_PICT = 4;
  59. public static final byte BT_JPEG = 5;
  60. public static final byte BT_PNG = 6;
  61. public static final byte BT_DIB = 7;
  62. private byte field_1_blipTypeWin32;
  63. private byte field_2_blipTypeMacOS;
  64. private byte[] field_3_uid; // 16 bytes
  65. private short field_4_tag;
  66. private int field_5_size;
  67. private int field_6_ref;
  68. private int field_7_offset;
  69. private byte field_8_usage;
  70. private byte field_9_name;
  71. private byte field_10_unused2;
  72. private byte field_11_unused3;
  73. private EscherBlipRecord field_12_blipRecord;
  74. private byte[] remainingData;
  75. /**
  76. * This method deserializes the record from a byte array.
  77. *
  78. * @param data The byte array containing the escher record information
  79. * @param offset The starting offset into <code>data</code>.
  80. * @param recordFactory May be null since this is not a container record.
  81. * @return The number of bytes read from the byte array.
  82. */
  83. public int fillFields( byte[] data, int offset,
  84. EscherRecordFactory recordFactory
  85. )
  86. {
  87. int bytesRemaining = readHeader( data, offset );
  88. int pos = offset + 8;
  89. field_1_blipTypeWin32 = data[pos];
  90. field_2_blipTypeMacOS = data[pos + 1];
  91. System.arraycopy( data, pos + 2, field_3_uid = new byte[16], 0, 16 );
  92. field_4_tag = LittleEndian.getShort( data, pos + 18 );
  93. field_5_size = LittleEndian.getInt( data, pos + 20 );
  94. field_6_ref = LittleEndian.getInt( data, pos + 24 );
  95. field_7_offset = LittleEndian.getInt( data, pos + 28 );
  96. field_8_usage = data[pos + 32];
  97. field_9_name = data[pos + 33];
  98. field_10_unused2 = data[pos + 34];
  99. field_11_unused3 = data[pos + 35];
  100. bytesRemaining -= 36;
  101. int bytesRead = 0;
  102. if (bytesRemaining > 0)
  103. {
  104. field_12_blipRecord = (EscherBlipRecord) recordFactory.createRecord( data, pos + 36 );
  105. bytesRead = field_12_blipRecord.fillFields( data, pos + 36, recordFactory );
  106. }
  107. pos += 36 + bytesRead;
  108. bytesRemaining -= bytesRead;
  109. // if (field_1_blipTypeWin32 == BT_PNG)
  110. // {
  111. // byte[] uid = new byte[16];
  112. // System.arraycopy( data, pos + 36, uid, 0, 16 );
  113. // byte[] puid = new byte[16];
  114. // System.arraycopy( data, pos + 52, puid, 0, 16 );
  115. // byte tag = data[pos+68];
  116. // System.out.println( HexDump.dump( data, 0, 0 ) );
  117. // byte[] pngBytes = EscherBlipRecord.decompress( data, pos+69, bytesRemaining);
  118. // }
  119. remainingData = new byte[bytesRemaining];
  120. System.arraycopy( data, pos, remainingData, 0, bytesRemaining );
  121. return bytesRemaining + 8 + 36 + (field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize()) ;
  122. }
  123. /**
  124. * This method serializes this escher record into a byte array.
  125. *
  126. * @param offset The offset into <code>data</code> to start writing the record data to.
  127. * @param data The byte array to serialize to.
  128. * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
  129. * @return The number of bytes written.
  130. * @see NullEscherSerializationListener
  131. */
  132. public int serialize( int offset, byte[] data, EscherSerializationListener listener )
  133. {
  134. listener.beforeRecordSerialize( offset, getRecordId(), this );
  135. if (remainingData == null)
  136. remainingData = new byte[0];
  137. LittleEndian.putShort( data, offset, getOptions() );
  138. LittleEndian.putShort( data, offset + 2, getRecordId() );
  139. if (remainingData == null) remainingData = new byte[0];
  140. int blipSize = field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize();
  141. int remainingBytes = remainingData.length + 36 + blipSize;
  142. LittleEndian.putInt( data, offset + 4, remainingBytes );
  143. data[offset + 8] = field_1_blipTypeWin32;
  144. data[offset + 9] = field_2_blipTypeMacOS;
  145. for ( int i = 0; i < 16; i++ )
  146. data[offset + 10 + i] = field_3_uid[i];
  147. LittleEndian.putShort( data, offset + 26, field_4_tag );
  148. LittleEndian.putInt( data, offset + 28, field_5_size );
  149. LittleEndian.putInt( data, offset + 32, field_6_ref );
  150. LittleEndian.putInt( data, offset + 36, field_7_offset );
  151. data[offset + 40] = field_8_usage;
  152. data[offset + 41] = field_9_name;
  153. data[offset + 42] = field_10_unused2;
  154. data[offset + 43] = field_11_unused3;
  155. int bytesWritten = 0;
  156. if (field_12_blipRecord != null)
  157. {
  158. bytesWritten = field_12_blipRecord.serialize( offset + 44, data, new NullEscherSerializationListener() );
  159. }
  160. if (remainingData == null)
  161. remainingData = new byte[0];
  162. System.arraycopy( remainingData, 0, data, offset + 44 + bytesWritten, remainingData.length );
  163. int pos = offset + 8 + 36 + remainingData.length + bytesWritten;
  164. listener.afterRecordSerialize(pos, getRecordId(), pos - offset, this);
  165. return pos - offset;
  166. }
  167. /**
  168. * Returns the number of bytes that are required to serialize this record.
  169. *
  170. * @return Number of bytes
  171. */
  172. public int getRecordSize()
  173. {
  174. return 8 + 1 + 1 + 16 + 2 + 4 + 4 + 4 + 1 + 1 + 1 + 1 + field_12_blipRecord.getRecordSize() + (remainingData == null ? 0 : remainingData.length);
  175. }
  176. /**
  177. * The short name for this record
  178. */
  179. public String getRecordName()
  180. {
  181. return "BSE";
  182. }
  183. /**
  184. * The expected blip type under windows (failure to match this blip type will result in
  185. * Excel converting to this format).
  186. */
  187. public byte getBlipTypeWin32()
  188. {
  189. return field_1_blipTypeWin32;
  190. }
  191. /**
  192. * Set the expected win32 blip type
  193. */
  194. public void setBlipTypeWin32( byte blipTypeWin32 )
  195. {
  196. this.field_1_blipTypeWin32 = blipTypeWin32;
  197. }
  198. /**
  199. * The expected blip type under MacOS (failure to match this blip type will result in
  200. * Excel converting to this format).
  201. */
  202. public byte getBlipTypeMacOS()
  203. {
  204. return field_2_blipTypeMacOS;
  205. }
  206. /**
  207. * Set the expected MacOS blip type
  208. */
  209. public void setBlipTypeMacOS( byte blipTypeMacOS )
  210. {
  211. this.field_2_blipTypeMacOS = blipTypeMacOS;
  212. }
  213. /**
  214. * 16 byte MD4 checksum.
  215. */
  216. public byte[] getUid()
  217. {
  218. return field_3_uid;
  219. }
  220. /**
  221. * 16 byte MD4 checksum.
  222. */
  223. public void setUid( byte[] uid )
  224. {
  225. this.field_3_uid = uid;
  226. }
  227. /**
  228. * unused
  229. */
  230. public short getTag()
  231. {
  232. return field_4_tag;
  233. }
  234. /**
  235. * unused
  236. */
  237. public void setTag( short tag )
  238. {
  239. this.field_4_tag = tag;
  240. }
  241. /**
  242. * Blip size in stream.
  243. */
  244. public int getSize()
  245. {
  246. return field_5_size;
  247. }
  248. /**
  249. * Blip size in stream.
  250. */
  251. public void setSize( int size )
  252. {
  253. this.field_5_size = size;
  254. }
  255. /**
  256. * The reference count of this blip.
  257. */
  258. public int getRef()
  259. {
  260. return field_6_ref;
  261. }
  262. /**
  263. * The reference count of this blip.
  264. */
  265. public void setRef( int ref )
  266. {
  267. this.field_6_ref = ref;
  268. }
  269. /**
  270. * File offset in the delay stream.
  271. */
  272. public int getOffset()
  273. {
  274. return field_7_offset;
  275. }
  276. /**
  277. * File offset in the delay stream.
  278. */
  279. public void setOffset( int offset )
  280. {
  281. this.field_7_offset = offset;
  282. }
  283. /**
  284. * Defines the way this blip is used.
  285. */
  286. public byte getUsage()
  287. {
  288. return field_8_usage;
  289. }
  290. /**
  291. * Defines the way this blip is used.
  292. */
  293. public void setUsage( byte usage )
  294. {
  295. this.field_8_usage = usage;
  296. }
  297. /**
  298. * The length in characters of the blip name.
  299. */
  300. public byte getName()
  301. {
  302. return field_9_name;
  303. }
  304. /**
  305. * The length in characters of the blip name.
  306. */
  307. public void setName( byte name )
  308. {
  309. this.field_9_name = name;
  310. }
  311. public byte getUnused2()
  312. {
  313. return field_10_unused2;
  314. }
  315. public void setUnused2( byte unused2 )
  316. {
  317. this.field_10_unused2 = unused2;
  318. }
  319. public byte getUnused3()
  320. {
  321. return field_11_unused3;
  322. }
  323. public void setUnused3( byte unused3 )
  324. {
  325. this.field_11_unused3 = unused3;
  326. }
  327. public EscherBlipRecord getBlipRecord()
  328. {
  329. return field_12_blipRecord;
  330. }
  331. public void setBlipRecord( EscherBlipRecord field_12_blipRecord )
  332. {
  333. this.field_12_blipRecord = field_12_blipRecord;
  334. }
  335. /**
  336. * Any remaining data in this record.
  337. */
  338. public byte[] getRemainingData()
  339. {
  340. return remainingData;
  341. }
  342. /**
  343. * Any remaining data in this record.
  344. */
  345. public void setRemainingData( byte[] remainingData )
  346. {
  347. this.remainingData = remainingData;
  348. }
  349. /**
  350. * Calculate the string representation of this object
  351. */
  352. public String toString()
  353. {
  354. String nl = System.getProperty( "line.separator" );
  355. String extraData;
  356. ByteArrayOutputStream b = new ByteArrayOutputStream();
  357. try
  358. {
  359. HexDump.dump( this.remainingData, 0, b, 0 );
  360. extraData = b.toString();
  361. }
  362. catch ( Exception e )
  363. {
  364. extraData = e.toString();
  365. }
  366. return getClass().getName() + ":" + nl +
  367. " RecordId: 0x" + HexDump.toHex( RECORD_ID ) + nl +
  368. " Options: 0x" + HexDump.toHex( getOptions() ) + nl +
  369. " BlipTypeWin32: " + field_1_blipTypeWin32 + nl +
  370. " BlipTypeMacOS: " + field_2_blipTypeMacOS + nl +
  371. " SUID: " + HexDump.toHex(field_3_uid) + nl +
  372. " Tag: " + field_4_tag + nl +
  373. " Size: " + field_5_size + nl +
  374. " Ref: " + field_6_ref + nl +
  375. " Offset: " + field_7_offset + nl +
  376. " Usage: " + field_8_usage + nl +
  377. " Name: " + field_9_name + nl +
  378. " Unused2: " + field_10_unused2 + nl +
  379. " Unused3: " + field_11_unused3 + nl +
  380. " blipRecord: " + field_12_blipRecord + nl +
  381. " Extra Data:" + nl + extraData;
  382. }
  383. /**
  384. * Retrieve the string representation given a blip id.
  385. */
  386. public String getBlipType( byte b )
  387. {
  388. switch ( b )
  389. {
  390. case BT_ERROR:
  391. return " ERROR";
  392. case BT_UNKNOWN:
  393. return " UNKNOWN";
  394. case BT_EMF:
  395. return " EMF";
  396. case BT_WMF:
  397. return " WMF";
  398. case BT_PICT:
  399. return " PICT";
  400. case BT_JPEG:
  401. return " JPEG";
  402. case BT_PNG:
  403. return " PNG";
  404. case BT_DIB:
  405. return " DIB";
  406. default:
  407. if ( b < 32 )
  408. return " NotKnown";
  409. else
  410. return " Client";
  411. }
  412. }
  413. }