Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SprmUtils.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.hwpf.sprm;
  16. import java.util.List;
  17. import org.apache.poi.util.IOUtils;
  18. import org.apache.poi.util.Internal;
  19. import org.apache.poi.util.LittleEndian;
  20. import org.apache.poi.util.LittleEndianConsts;
  21. @Internal
  22. public final class SprmUtils
  23. {
  24. //arbitrarily selected; may need to increase
  25. private static final int MAX_RECORD_LENGTH = 100_000;
  26. public SprmUtils()
  27. {
  28. }
  29. public static byte[] shortArrayToByteArray(short[] convert)
  30. {
  31. byte[] buf = IOUtils.safelyAllocate(convert.length * (long)LittleEndianConsts.SHORT_SIZE, MAX_RECORD_LENGTH);
  32. for (int x = 0; x < convert.length; x++)
  33. {
  34. LittleEndian.putShort(buf, x * LittleEndianConsts.SHORT_SIZE, convert[x]);
  35. }
  36. return buf;
  37. }
  38. public static int addSpecialSprm(short instruction, byte[] varParam, List<byte[]> list)
  39. {
  40. byte[] sprm = new byte[varParam.length + 4];
  41. System.arraycopy(varParam, 0, sprm, 4, varParam.length);
  42. LittleEndian.putShort(sprm, 0, instruction);
  43. LittleEndian.putShort(sprm, 2, (short)(varParam.length + 1));
  44. list.add(sprm);
  45. return sprm.length;
  46. }
  47. public static int addSprm( short instruction, boolean param,
  48. List<byte[]> list )
  49. {
  50. return addSprm( instruction, param ? 1 : 0, null, list );
  51. }
  52. public static int addSprm(short instruction, int param, byte[] varParam, List<byte[]> list)
  53. {
  54. int type = (instruction & 0xe000) >> 13;
  55. byte[] sprm = null;
  56. switch(type)
  57. {
  58. case 0:
  59. case 1:
  60. sprm = new byte[3];
  61. sprm[2] = (byte)param;
  62. break;
  63. case 2:
  64. sprm = new byte[4];
  65. LittleEndian.putShort(sprm, 2, (short)param);
  66. break;
  67. case 3:
  68. sprm = new byte[6];
  69. LittleEndian.putInt(sprm, 2, param);
  70. break;
  71. case 4:
  72. case 5:
  73. sprm = new byte[4];
  74. LittleEndian.putShort(sprm, 2, (short)param);
  75. break;
  76. case 6:
  77. assert(varParam != null);
  78. sprm = new byte[3 + varParam.length];
  79. sprm[2] = (byte)varParam.length;
  80. System.arraycopy(varParam, 0, sprm, 3, varParam.length);
  81. break;
  82. case 7:
  83. sprm = new byte[5];
  84. // this is a three byte int so it has to be handled special
  85. byte[] temp = new byte[4];
  86. LittleEndian.putInt(temp, 0, param);
  87. System.arraycopy(temp, 0, sprm, 2, 3);
  88. break;
  89. default:
  90. //should never happen
  91. throw new RuntimeException("Invalid sprm type");
  92. }
  93. LittleEndian.putShort(sprm, 0, instruction);
  94. list.add(sprm);
  95. return sprm.length;
  96. }
  97. public static byte[] getGrpprl(List<byte[]> sprmList, int size)
  98. {
  99. // spit out the final grpprl
  100. byte[] grpprl = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH);
  101. int listSize = sprmList.size() - 1;
  102. int index = 0;
  103. for (; listSize >= 0; listSize--)
  104. {
  105. byte[] sprm = sprmList.remove(0);
  106. System.arraycopy(sprm, 0, grpprl, index, sprm.length);
  107. index += sprm.length;
  108. }
  109. return grpprl;
  110. }
  111. public static int convertBrcToInt(short[] brc)
  112. {
  113. byte[] buf = new byte[4];
  114. LittleEndian.putShort(buf, 0, brc[0]);
  115. LittleEndian.putShort(buf, LittleEndianConsts.SHORT_SIZE, brc[1]);
  116. return LittleEndian.getInt(buf);
  117. }
  118. }