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 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.LittleEndianByteArrayOutputStream;
  17. import org.apache.poi.util.LittleEndianOutput;
  18. /**
  19. * Subclasses of this class (the majority of BIFF records) are non-continuable. This allows for
  20. * some simplification of serialization logic
  21. *
  22. * @author Josh Micich
  23. */
  24. public abstract class StandardRecord extends Record {
  25. protected abstract int getDataSize();
  26. public final int getRecordSize() {
  27. return 4 + getDataSize();
  28. }
  29. @Override
  30. public final int serialize(int offset, byte[] data) {
  31. int dataSize = getDataSize();
  32. int recSize = 4 + dataSize;
  33. LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize);
  34. out.writeShort(getSid());
  35. out.writeShort(dataSize);
  36. serialize(out);
  37. if (out.getWriteIndex() - offset != recSize) {
  38. throw new IllegalStateException("Error in serialization of (" + getClass().getName() + "): "
  39. + "Incorrect number of bytes written - expected "
  40. + recSize + " but got " + (out.getWriteIndex() - offset));
  41. }
  42. return recSize;
  43. }
  44. /**
  45. * Write the data content of this BIFF record. The 'ushort sid' and 'ushort size' header fields
  46. * have already been written by the superclass.<br/>
  47. *
  48. * The subclass must write the exact number of bytes as reported by {@link org.apache.poi.hssf.record.Record#getRecordSize()}}
  49. */
  50. protected abstract void serialize(LittleEndianOutput out);
  51. }