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.

SSTSerializer.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.hssf.record.common.UnicodeString;
  17. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  18. import org.apache.poi.util.IntMapper;
  19. /**
  20. * This class handles serialization of SST records. It utilizes the record processor
  21. * class write individual records. This has been refactored from the SSTRecord class.
  22. *
  23. * @author Glen Stampoultzis (glens at apache.org)
  24. */
  25. final class SSTSerializer {
  26. private final int _numStrings;
  27. private final int _numUniqueStrings;
  28. private final IntMapper<UnicodeString> strings;
  29. /** Offsets from the beginning of the SST record (even across continuations) */
  30. private final int[] bucketAbsoluteOffsets;
  31. /** Offsets relative the start of the current SST or continue record */
  32. private final int[] bucketRelativeOffsets;
  33. public SSTSerializer( IntMapper<UnicodeString> strings, int numStrings, int numUniqueStrings )
  34. {
  35. this.strings = strings;
  36. _numStrings = numStrings;
  37. _numUniqueStrings = numUniqueStrings;
  38. int infoRecs = ExtSSTRecord.getNumberOfInfoRecsForStrings(strings.size());
  39. this.bucketAbsoluteOffsets = new int[infoRecs];
  40. this.bucketRelativeOffsets = new int[infoRecs];
  41. }
  42. public void serialize(ContinuableRecordOutput out) {
  43. out.writeInt(_numStrings);
  44. out.writeInt(_numUniqueStrings);
  45. for ( int k = 0; k < strings.size(); k++ )
  46. {
  47. if (k % ExtSSTRecord.DEFAULT_BUCKET_SIZE == 0)
  48. {
  49. int rOff = out.getTotalSize();
  50. int index = k/ExtSSTRecord.DEFAULT_BUCKET_SIZE;
  51. if (index < ExtSSTRecord.MAX_BUCKETS) {
  52. //Excel only indexes the first 128 buckets.
  53. bucketAbsoluteOffsets[index] = rOff;
  54. bucketRelativeOffsets[index] = rOff;
  55. }
  56. }
  57. UnicodeString s = getUnicodeString(k);
  58. s.serialize(out);
  59. }
  60. }
  61. private UnicodeString getUnicodeString( int index )
  62. {
  63. return getUnicodeString(strings, index);
  64. }
  65. private static UnicodeString getUnicodeString( IntMapper<UnicodeString> strings, int index )
  66. {
  67. return ( strings.get( index ) );
  68. }
  69. public int[] getBucketAbsoluteOffsets()
  70. {
  71. return bucketAbsoluteOffsets;
  72. }
  73. public int[] getBucketRelativeOffsets()
  74. {
  75. return bucketRelativeOffsets;
  76. }
  77. }