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.

Record.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.commons.io.input.UnsynchronizedByteArrayInputStream;
  18. import org.apache.poi.common.Duplicatable;
  19. import org.apache.poi.common.usermodel.GenericRecord;
  20. import org.apache.poi.util.GenericRecordJsonWriter;
  21. /**
  22. * All HSSF Records inherit from this class.
  23. */
  24. public abstract class Record extends RecordBase implements Duplicatable, GenericRecord {
  25. protected Record() {}
  26. protected Record(Record other) {}
  27. /**
  28. * called by the class that is responsible for writing this sucker.
  29. * Subclasses should implement this so that their data is passed back in a
  30. * byte array.
  31. *
  32. * @return byte array containing instance data
  33. */
  34. public final byte[] serialize() {
  35. byte[] retval = new byte[ getRecordSize() ];
  36. serialize(0, retval);
  37. return retval;
  38. }
  39. /**
  40. * get a string representation of the record (for biffview/debugging)
  41. */
  42. @Override
  43. public final String toString() {
  44. return GenericRecordJsonWriter.marshal(this);
  45. }
  46. /**
  47. * return the non static version of the id for this record.
  48. *
  49. * @return he id for this record
  50. */
  51. public abstract short getSid();
  52. /**
  53. * Clone the current record, via a call to serialize
  54. * it, and another to create a new record from the
  55. * bytes.
  56. * May only be used for classes which don't have
  57. * internal counts / ids in them. For those which
  58. * do, a full model-aware cloning is needed, which
  59. * allocates new ids / counts as needed.
  60. *
  61. * @return the cloned current record
  62. */
  63. public Record cloneViaReserialise() {
  64. // Do it via a re-serialization
  65. // It's a cheat, but it works...
  66. byte[] b = serialize();
  67. RecordInputStream rinp = null;
  68. try {
  69. rinp = new RecordInputStream(UnsynchronizedByteArrayInputStream.builder().setByteArray(b).get());
  70. } catch (IOException e) {
  71. // not possible with ByteArray but still declared in the API
  72. throw new IllegalStateException(e);
  73. }
  74. rinp.nextRecord();
  75. Record[] r = RecordFactory.createRecord(rinp);
  76. if(r.length != 1) {
  77. throw new IllegalStateException("Re-serialised a record to clone it, but got " + r.length + " records back!");
  78. }
  79. return r[0];
  80. }
  81. public abstract Record copy();
  82. @Override
  83. public abstract HSSFRecordTypes getGenericRecordType();
  84. }