選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IndexRecord.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.GenericRecordUtil;
  19. import org.apache.poi.util.IntList;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. import org.apache.poi.util.RecordFormatException;
  22. /**
  23. * Occurs right after BOF, tells you where the DBCELL records are for a sheet Important for locating cells
  24. */
  25. public final class IndexRecord extends StandardRecord {
  26. public static final short sid = 0x020B;
  27. private int field_2_first_row; // first row on the sheet
  28. private int field_3_last_row_add1; // last row
  29. private int field_4_zero; // supposed to be zero
  30. private IntList field_5_dbcells; // array of offsets to DBCELL records
  31. public IndexRecord() {}
  32. public IndexRecord(IndexRecord other) {
  33. super(other);
  34. field_2_first_row = other.field_2_first_row;
  35. field_3_last_row_add1 = other.field_3_last_row_add1;
  36. field_4_zero = other.field_4_zero;
  37. field_5_dbcells = (other.field_5_dbcells == null) ? null : new IntList(other.field_5_dbcells);
  38. }
  39. public IndexRecord(RecordInputStream in) {
  40. int field_1_zero = in.readInt();
  41. if (field_1_zero != 0) {
  42. throw new RecordFormatException("Expected zero for field 1 but got " + field_1_zero);
  43. }
  44. field_2_first_row = in.readInt();
  45. field_3_last_row_add1 = in.readInt();
  46. field_4_zero = in.readInt();
  47. int nCells = in.remaining() / 4;
  48. field_5_dbcells = new IntList(nCells);
  49. for(int i=0; i<nCells; i++) {
  50. field_5_dbcells.add(in.readInt());
  51. }
  52. }
  53. public void setFirstRow(int row)
  54. {
  55. field_2_first_row = row;
  56. }
  57. public void setLastRowAdd1(int row)
  58. {
  59. field_3_last_row_add1 = row;
  60. }
  61. public void addDbcell(int cell)
  62. {
  63. if (field_5_dbcells == null)
  64. {
  65. field_5_dbcells = new IntList();
  66. }
  67. field_5_dbcells.add(cell);
  68. }
  69. public void setDbcell(int cell, int value)
  70. {
  71. field_5_dbcells.set(cell, value);
  72. }
  73. public int getFirstRow()
  74. {
  75. return field_2_first_row;
  76. }
  77. public int getLastRowAdd1()
  78. {
  79. return field_3_last_row_add1;
  80. }
  81. public int getNumDbcells()
  82. {
  83. if (field_5_dbcells == null)
  84. {
  85. return 0;
  86. }
  87. return field_5_dbcells.size();
  88. }
  89. public int getDbcellAt(int cellnum)
  90. {
  91. return field_5_dbcells.get(cellnum);
  92. }
  93. @Override
  94. public void serialize(LittleEndianOutput out) {
  95. out.writeInt(0);
  96. out.writeInt(getFirstRow());
  97. out.writeInt(getLastRowAdd1());
  98. out.writeInt(field_4_zero);
  99. for (int k = 0; k < getNumDbcells(); k++) {
  100. out.writeInt(getDbcellAt(k));
  101. }
  102. }
  103. @Override
  104. protected int getDataSize() {
  105. return 16 // 4 ints
  106. + getNumDbcells() * 4;
  107. }
  108. /**
  109. * @param blockCount the number of blocks to be indexed
  110. * @return the size of an IndexRecord when it needs to index the specified number of blocks
  111. */
  112. public static int getRecordSizeForBlockCount(int blockCount) {
  113. return 20 + 4 * blockCount;
  114. }
  115. @Override
  116. public short getSid() {
  117. return sid;
  118. }
  119. @Override
  120. public IndexRecord copy() {
  121. return new IndexRecord(this);
  122. }
  123. @Override
  124. public HSSFRecordTypes getGenericRecordType() {
  125. return HSSFRecordTypes.INDEX;
  126. }
  127. @Override
  128. public Map<String, Supplier<?>> getGenericProperties() {
  129. return GenericRecordUtil.getGenericProperties(
  130. "firstRow", this::getFirstRow,
  131. "lastRowAdd1", this::getLastRowAdd1,
  132. "dbcell_", (field_5_dbcells == null) ? () -> null : field_5_dbcells::toArray
  133. );
  134. }
  135. }