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.

DBCellRecord.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.util.HexDump;
  17. import org.apache.poi.util.LittleEndianOutput;
  18. /**
  19. * Title: DBCell Record (0x00D7)<p/>
  20. * Description: Used by Excel and other MS apps to quickly find rows in the sheets.<P>
  21. * REFERENCE: PG 299/440 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  22. * @author Andrew C. Oliver (acoliver at apache dot org)
  23. * @author Jason Height
  24. */
  25. public final class DBCellRecord extends StandardRecord {
  26. public final static short sid = 0x00D7;
  27. public final static int BLOCK_SIZE = 32;
  28. public static final class Builder {
  29. private short[] _cellOffsets;
  30. private int _nCellOffsets;
  31. public Builder() {
  32. _cellOffsets = new short[4];
  33. }
  34. public void addCellOffset(int cellRefOffset) {
  35. if (_cellOffsets.length <= _nCellOffsets) {
  36. short[] temp = new short[_nCellOffsets * 2];
  37. System.arraycopy(_cellOffsets, 0, temp, 0, _nCellOffsets);
  38. _cellOffsets = temp;
  39. }
  40. _cellOffsets[_nCellOffsets] = (short) cellRefOffset;
  41. _nCellOffsets++;
  42. }
  43. public DBCellRecord build(int rowOffset) {
  44. short[] cellOffsets = new short[_nCellOffsets];
  45. System.arraycopy(_cellOffsets, 0, cellOffsets, 0, _nCellOffsets);
  46. return new DBCellRecord(rowOffset, cellOffsets);
  47. }
  48. }
  49. /**
  50. * offset from the start of this DBCellRecord to the start of the first cell in
  51. * the next DBCell block.
  52. */
  53. private final int field_1_row_offset;
  54. private final short[] field_2_cell_offsets;
  55. DBCellRecord(int rowOffset, short[]cellOffsets) {
  56. field_1_row_offset = rowOffset;
  57. field_2_cell_offsets = cellOffsets;
  58. }
  59. public DBCellRecord(RecordInputStream in) {
  60. field_1_row_offset = in.readUShort();
  61. int size = in.remaining();
  62. field_2_cell_offsets = new short[ size / 2 ];
  63. for (int i=0;i<field_2_cell_offsets.length;i++)
  64. {
  65. field_2_cell_offsets[ i ] = in.readShort();
  66. }
  67. }
  68. public String toString() {
  69. StringBuffer buffer = new StringBuffer();
  70. buffer.append("[DBCELL]\n");
  71. buffer.append(" .rowoffset = ").append(HexDump.intToHex(field_1_row_offset)).append("\n");
  72. for (int k = 0; k < field_2_cell_offsets.length; k++) {
  73. buffer.append(" .cell_").append(k).append(" = ")
  74. .append(HexDump.shortToHex(field_2_cell_offsets[ k ])).append("\n");
  75. }
  76. buffer.append("[/DBCELL]\n");
  77. return buffer.toString();
  78. }
  79. public void serialize(LittleEndianOutput out) {
  80. out.writeInt(field_1_row_offset);
  81. for (int k = 0; k < field_2_cell_offsets.length; k++) {
  82. out.writeShort(field_2_cell_offsets[ k ]);
  83. }
  84. }
  85. protected int getDataSize() {
  86. return 4 + field_2_cell_offsets.length * 2;
  87. }
  88. /**
  89. * @return the size of the group of <tt>DBCellRecord</tt>s needed to encode
  90. * the specified number of blocks and rows
  91. */
  92. public static int calculateSizeOfRecords(int nBlocks, int nRows) {
  93. // One DBCell per block.
  94. // 8 bytes per DBCell (non variable section)
  95. // 2 bytes per row reference
  96. return nBlocks * 8 + nRows * 2;
  97. }
  98. public short getSid() {
  99. return sid;
  100. }
  101. public Object clone() {
  102. // safe because immutable
  103. return this;
  104. }
  105. }