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.1KB

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