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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /**
  18. * Title: Record
  19. * Description: All HSSF Records inherit from this class.
  20. * @author Andrew C. Oliver
  21. * @author Marc Johnson (mjohnson at apache dot org)
  22. * @author Jason Height (jheight at chariot dot net dot au)
  23. */
  24. public abstract class Record extends RecordBase {
  25. protected Record() {
  26. // no fields to initialise
  27. }
  28. /**
  29. * called by the class that is responsible for writing this sucker.
  30. * Subclasses should implement this so that their data is passed back in a
  31. * byte array.
  32. *
  33. * @return byte array containing instance data
  34. */
  35. public final byte[] serialize() {
  36. byte[] retval = new byte[ getRecordSize() ];
  37. serialize(0, retval);
  38. return retval;
  39. }
  40. /**
  41. * get a string representation of the record (for biffview/debugging)
  42. */
  43. public String toString() {
  44. return super.toString();
  45. }
  46. /**
  47. * return the non static version of the id for this record.
  48. */
  49. public abstract short getSid();
  50. public Object clone() {
  51. if (false) {
  52. // TODO - implement clone in a more standardised way
  53. try {
  54. return super.clone();
  55. } catch (CloneNotSupportedException e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. throw new RuntimeException("The class "+getClass().getName()+" needs to define a clone method");
  60. }
  61. /**
  62. * Clone the current record, via a call to serialize
  63. * it, and another to create a new record from the
  64. * bytes.
  65. * May only be used for classes which don't have
  66. * internal counts / ids in them. For those which
  67. * do, a full model-aware cloning is needed, which
  68. * allocates new ids / counts as needed.
  69. */
  70. public Record cloneViaReserialise() {
  71. // Do it via a re-serialization
  72. // It's a cheat, but it works...
  73. byte[] b = serialize();
  74. RecordInputStream rinp = new RecordInputStream(new ByteArrayInputStream(b));
  75. rinp.nextRecord();
  76. Record[] r = RecordFactory.createRecord(rinp);
  77. if(r.length != 1) {
  78. throw new IllegalStateException("Re-serialised a record to clone it, but got " + r.length + " records back!");
  79. }
  80. return r[0];
  81. }
  82. }