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.

HemfPlusRecordIterator.java 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.hemf.record.emfplus;
  16. import java.io.IOException;
  17. import java.util.Iterator;
  18. import org.apache.poi.util.LittleEndianInputStream;
  19. import org.apache.poi.util.RecordFormatException;
  20. public class HemfPlusRecordIterator implements Iterator<HemfPlusRecord> {
  21. private final LittleEndianInputStream leis;
  22. private final int startIdx;
  23. private final int limit;
  24. private HemfPlusRecord currentRecord;
  25. public HemfPlusRecordIterator(LittleEndianInputStream leis) {
  26. this(leis, -1);
  27. }
  28. public HemfPlusRecordIterator(LittleEndianInputStream leis, int limit) {
  29. this.leis = leis;
  30. this.limit = limit;
  31. startIdx = leis.getReadIndex();
  32. //queue the first non-header record
  33. currentRecord = _next();
  34. }
  35. @Override
  36. public boolean hasNext() {
  37. return currentRecord != null;
  38. }
  39. @Override
  40. public HemfPlusRecord next() {
  41. HemfPlusRecord toReturn = currentRecord;
  42. final boolean isEOF = (limit == -1 || leis.getReadIndex()-startIdx >= limit);
  43. // (currentRecord instanceof HemfPlusMisc.EmfEof)
  44. currentRecord = isEOF ? null : _next();
  45. return toReturn;
  46. }
  47. private HemfPlusRecord _next() {
  48. if (currentRecord != null && HemfPlusRecordType.eof == currentRecord.getEmfPlusRecordType()) {
  49. return null;
  50. }
  51. // A 16-bit unsigned integer that identifies this record type
  52. int recordId = leis.readUShort();
  53. // A 16-bit unsigned integer that provides information about how the operation is
  54. // to be performed, and about the structure of the record.
  55. int flags = leis.readUShort();
  56. // A 32-bit unsigned integer that specifies the 32-bit-aligned size of the entire
  57. // record in bytes, including the 12-byte record header and record-specific data.
  58. int recordSize = (int)leis.readUInt();
  59. // A 32-bit unsigned integer that specifies the 32-bit-aligned number of bytes of data
  60. // in the record-specific data that follows. This number does not include the size of
  61. // the invariant part of this record.
  62. int dataSize = (int)leis.readUInt();
  63. HemfPlusRecordType type = HemfPlusRecordType.getById(recordId);
  64. if (type == null) {
  65. throw new RecordFormatException("Undefined record of type:"+recordId);
  66. }
  67. final HemfPlusRecord record = type.constructor.get();
  68. try {
  69. long readBytes = record.init(leis, dataSize, recordId, flags);
  70. assert (readBytes <= recordSize-12);
  71. leis.skipFully((int)(recordSize-12-readBytes));
  72. } catch (IOException e) {
  73. throw new RecordFormatException(e);
  74. }
  75. return record;
  76. }
  77. @Override
  78. public void remove() {
  79. throw new UnsupportedOperationException("Remove not supported");
  80. }
  81. }