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.

UnknownRecordPlaceholder.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.hslf.record;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.util.GenericRecordUtil;
  21. import org.apache.poi.util.IOUtils;
  22. import org.apache.poi.util.LittleEndian;
  23. /**
  24. * If we come across a record we don't know about, we create one of
  25. * these. It allows us to keep track of what it contains, so we can
  26. * write it back out to disk unchanged
  27. *
  28. * @author Nick Burch
  29. */
  30. @SuppressWarnings("unused")
  31. public final class UnknownRecordPlaceholder extends RecordAtom
  32. {
  33. //arbitrarily selected; may need to increase
  34. private static final int MAX_RECORD_LENGTH = 20_000_000;
  35. private byte[] _contents;
  36. private long _type;
  37. /**
  38. * Create a new holder for a record we don't grok
  39. */
  40. protected UnknownRecordPlaceholder(byte[] source, int start, int len) {
  41. // Sanity Checking - including whole header, so treat
  42. // length as based of 0, not 8 (including header size based)
  43. if(len < 0) { len = 0; }
  44. // Treat as an atom, grab and hold everything
  45. _contents = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH);
  46. System.arraycopy(source,start,_contents,0,len);
  47. _type = LittleEndian.getUShort(_contents,2);
  48. }
  49. /**
  50. * Return the value we were given at creation
  51. */
  52. public long getRecordType() {
  53. return _type;
  54. }
  55. /**
  56. * Return the value as enum we were given at creation
  57. */
  58. public org.apache.poi.hslf.record.RecordTypes getRecordTypeEnum() {
  59. return RecordTypes.forTypeID((int)_type);
  60. }
  61. /**
  62. * Write the contents of the record back, so it can be written
  63. * to disk
  64. */
  65. public void writeOut(OutputStream out) throws IOException {
  66. out.write(_contents);
  67. }
  68. @Override
  69. public Map<String, Supplier<?>> getGenericProperties() {
  70. return GenericRecordUtil.getGenericProperties("contents", () -> _contents);
  71. }
  72. }