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.

StandardRecord.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 java.io.IOException;
  17. import org.apache.poi.util.LittleEndianByteArrayOutputStream;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. /**
  20. * Subclasses of this class (the majority of BIFF records) are non-continuable.
  21. * This allows for some simplification of serialization logic
  22. */
  23. public abstract class StandardRecord extends Record {
  24. protected abstract int getDataSize();
  25. protected StandardRecord() {}
  26. protected StandardRecord(StandardRecord other) {}
  27. @Override
  28. public final int getRecordSize() {
  29. return 4 + getDataSize();
  30. }
  31. /**
  32. * Write the data content of this BIFF record including the sid and record
  33. * length.
  34. * <p>
  35. * The subclass must write the exact number of bytes as reported by
  36. * {@link org.apache.poi.hssf.record.Record#getRecordSize()}}
  37. */
  38. @Override
  39. public final int serialize(int offset, byte[] data) {
  40. int dataSize = getDataSize();
  41. int recSize = 4 + dataSize;
  42. try (LittleEndianByteArrayOutputStream out =
  43. new LittleEndianByteArrayOutputStream(data, offset, recSize)) {
  44. out.writeShort(getSid());
  45. out.writeShort(dataSize);
  46. serialize(out);
  47. if (out.getWriteIndex() - offset != recSize) {
  48. throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): "
  49. + "Incorrect number of bytes written - expected " + recSize + " but got "
  50. + (out.getWriteIndex() - offset));
  51. }
  52. } catch (IOException ioe) {
  53. // should never happen in practice
  54. throw new IllegalStateException(ioe);
  55. }
  56. return recSize;
  57. }
  58. /**
  59. * Write the data content of this BIFF record. The 'ushort sid' and 'ushort
  60. * size' header fields have already been written by the superclass.
  61. * <p>
  62. * The number of bytes written must equal the record size reported by
  63. * {@link org.apache.poi.hssf.record.Record#getRecordSize()}} minus four (
  64. * record header consisting of a 'ushort sid' and 'ushort reclength' has
  65. * already been written by their superclass).
  66. *
  67. * @param out
  68. * the output object
  69. */
  70. protected abstract void serialize(LittleEndianOutput out);
  71. public abstract StandardRecord copy();
  72. }