You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubRecord.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import org.apache.poi.util.HexDump;
  17. import org.apache.poi.util.LittleEndianInput;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. import org.apache.poi.util.LittleEndianOutputStream;
  20. import java.io.ByteArrayOutputStream;
  21. /**
  22. * Subrecords are part of the OBJ class.
  23. */
  24. public abstract class SubRecord {
  25. protected SubRecord() {
  26. // no fields to initialise
  27. }
  28. /**
  29. * read a sub-record from the supplied stream
  30. *
  31. * @param in the stream to read from
  32. * @param cmoOt the objectType field of the containing CommonObjectDataSubRecord,
  33. * we need it to propagate to next sub-records as it defines what data follows
  34. * @return the created sub-record
  35. */
  36. public static SubRecord createSubRecord(LittleEndianInput in, int cmoOt) {
  37. int sid = in.readUShort();
  38. int secondUShort = in.readUShort(); // Often (but not always) the datasize for the sub-record
  39. switch (sid) {
  40. case CommonObjectDataSubRecord.sid:
  41. return new CommonObjectDataSubRecord(in, secondUShort);
  42. case EmbeddedObjectRefSubRecord.sid:
  43. return new EmbeddedObjectRefSubRecord(in, secondUShort);
  44. case GroupMarkerSubRecord.sid:
  45. return new GroupMarkerSubRecord(in, secondUShort);
  46. case EndSubRecord.sid:
  47. return new EndSubRecord(in, secondUShort);
  48. case NoteStructureSubRecord.sid:
  49. return new NoteStructureSubRecord(in, secondUShort);
  50. case LbsDataSubRecord.sid:
  51. return new LbsDataSubRecord(in, secondUShort, cmoOt);
  52. }
  53. return new UnknownSubRecord(in, sid, secondUShort);
  54. }
  55. /**
  56. * @return the size of the data for this record (which is always 4 bytes less than the total
  57. * record size). Note however, that ushort encoded after the record sid is usually but not
  58. * always the data size.
  59. */
  60. protected abstract int getDataSize();
  61. public byte[] serialize() {
  62. int size = getDataSize() + 4;
  63. ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
  64. serialize(new LittleEndianOutputStream(baos));
  65. if (baos.size() != size) {
  66. throw new RuntimeException("write size mismatch");
  67. }
  68. return baos.toByteArray();
  69. }
  70. public abstract void serialize(LittleEndianOutput out);
  71. public abstract Object clone();
  72. /**
  73. * Wether this record terminates the sub-record stream.
  74. * There are two cases when this method must be overridden and return <code>true</code>
  75. * - EndSubRecord (sid = 0x00)
  76. * - LbsDataSubRecord (sid = 0x12)
  77. *
  78. * @return whether this record is the last in the sub-record stream
  79. */
  80. public boolean isTerminating(){
  81. return false;
  82. }
  83. private static final class UnknownSubRecord extends SubRecord {
  84. private final int _sid;
  85. private final byte[] _data;
  86. public UnknownSubRecord(LittleEndianInput in, int sid, int size) {
  87. _sid = sid;
  88. byte[] buf = new byte[size];
  89. in.readFully(buf);
  90. _data = buf;
  91. }
  92. protected int getDataSize() {
  93. return _data.length;
  94. }
  95. public void serialize(LittleEndianOutput out) {
  96. out.writeShort(_sid);
  97. out.writeShort(_data.length);
  98. out.write(_data);
  99. }
  100. public Object clone() {
  101. return this;
  102. }
  103. public String toString() {
  104. StringBuffer sb = new StringBuffer(64);
  105. sb.append(getClass().getName()).append(" [");
  106. sb.append("sid=").append(HexDump.shortToHex(_sid));
  107. sb.append(" size=").append(_data.length);
  108. sb.append(" : ").append(HexDump.toHex(_data));
  109. sb.append("]\n");
  110. return sb.toString();
  111. }
  112. }
  113. }