PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/SSTSerializer.java

http://github.com/openmicroscopy/bioformats
Java | 139 lines | 61 code | 18 blank | 60 comment | 7 complexity | 7688b5b952c30a8c519d6dcfc16c5174 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.record;
  38. import loci.poi.util.IntMapper;
  39. import loci.poi.util.LittleEndian;
  40. /**
  41. * This class handles serialization of SST records. It utilizes the record processor
  42. * class write individual records. This has been refactored from the SSTRecord class.
  43. *
  44. * @author Glen Stampoultzis (glens at apache.org)
  45. */
  46. class SSTSerializer
  47. {
  48. // todo: make private again
  49. private IntMapper strings;
  50. private SSTRecordHeader sstRecordHeader;
  51. /** Offsets from the beginning of the SST record (even across continuations) */
  52. int[] bucketAbsoluteOffsets;
  53. /** Offsets relative the start of the current SST or continue record */
  54. int[] bucketRelativeOffsets;
  55. int startOfSST, startOfRecord;
  56. public SSTSerializer( IntMapper strings, int numStrings, int numUniqueStrings )
  57. {
  58. this.strings = strings;
  59. this.sstRecordHeader = new SSTRecordHeader( numStrings, numUniqueStrings );
  60. int infoRecs = ExtSSTRecord.getNumberOfInfoRecsForStrings(strings.size());
  61. this.bucketAbsoluteOffsets = new int[infoRecs];
  62. this.bucketRelativeOffsets = new int[infoRecs];
  63. }
  64. /**
  65. * Create a byte array consisting of an SST record and any
  66. * required Continue records, ready to be written out.
  67. * <p>
  68. * If an SST record and any subsequent Continue records are read
  69. * in to create this instance, this method should produce a byte
  70. * array that is identical to the byte array produced by
  71. * concatenating the input records' data.
  72. *
  73. * @return the byte array
  74. */
  75. public int serialize(int offset, byte[] data )
  76. {
  77. UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats();
  78. sstRecordHeader.writeSSTHeader( stats, data, 0 + offset, 0 );
  79. int pos = offset + SSTRecord.SST_RECORD_OVERHEAD;
  80. for ( int k = 0; k < strings.size(); k++ )
  81. {
  82. if (k % ExtSSTRecord.DEFAULT_BUCKET_SIZE == 0)
  83. {
  84. int index = k/ExtSSTRecord.DEFAULT_BUCKET_SIZE;
  85. if (index < ExtSSTRecord.MAX_BUCKETS) {
  86. //Excel only indexes the first 128 buckets.
  87. bucketAbsoluteOffsets[index] = pos-offset;
  88. bucketRelativeOffsets[index] = pos-offset;
  89. }
  90. }
  91. UnicodeString s = getUnicodeString(k);
  92. pos += s.serialize(stats, pos, data);
  93. }
  94. //Check to see if there is a hanging continue record length
  95. if (stats.lastLengthPos != -1) {
  96. short lastRecordLength = (short)(pos - stats.lastLengthPos-2);
  97. if (lastRecordLength > 8224)
  98. throw new InternalError();
  99. LittleEndian.putShort(data, stats.lastLengthPos, lastRecordLength);
  100. }
  101. return pos - offset;
  102. }
  103. private UnicodeString getUnicodeString( int index )
  104. {
  105. return getUnicodeString(strings, index);
  106. }
  107. private static UnicodeString getUnicodeString( IntMapper strings, int index )
  108. {
  109. return ( (UnicodeString) strings.get( index ) );
  110. }
  111. public int[] getBucketAbsoluteOffsets()
  112. {
  113. return bucketAbsoluteOffsets;
  114. }
  115. public int[] getBucketRelativeOffsets()
  116. {
  117. return bucketRelativeOffsets;
  118. }
  119. }