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.

XSSFBUtils.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.xssf.binary;
  16. import java.nio.charset.Charset;
  17. import org.apache.poi.POIXMLException;
  18. import org.apache.poi.util.Internal;
  19. import org.apache.poi.util.LittleEndian;
  20. @Internal
  21. public class XSSFBUtils {
  22. /**
  23. * Reads an XLNullableWideString.
  24. * @param data data from which to read
  25. * @param offset in data from which to start
  26. * @param sb buffer to which to write. You must setLength(0) before calling!
  27. * @return number of bytes read
  28. * @throws XSSFBParseException if there was an exception during reading
  29. */
  30. static int readXLNullableWideString(byte[] data, int offset, StringBuilder sb) throws XSSFBParseException {
  31. long numChars = LittleEndian.getUInt(data, offset);
  32. if (numChars < 0) {
  33. throw new XSSFBParseException("too few chars to read");
  34. } else if (numChars == 0xFFFFFFFFL) { //this means null value (2.5.166), do not read any bytes!!!
  35. return 0;
  36. } else if (numChars > 0xFFFFFFFFL) {
  37. throw new XSSFBParseException("too many chars to read");
  38. }
  39. int numBytes = 2*(int)numChars;
  40. offset += 4;
  41. if (offset+numBytes > data.length) {
  42. throw new XSSFBParseException("trying to read beyond data length:" +
  43. "offset="+offset+", numBytes="+numBytes+", data.length="+data.length);
  44. }
  45. sb.append(new String(data, offset, numBytes, Charset.forName("UTF-16LE")));
  46. numBytes+=4;
  47. return numBytes;
  48. }
  49. /**
  50. * Reads an XLNullableWideString.
  51. * @param data data from which to read
  52. * @param offset in data from which to start
  53. * @param sb buffer to which to write. You must setLength(0) before calling!
  54. * @return number of bytes read
  55. * @throws XSSFBParseException if there was an exception while trying to read the string
  56. */
  57. public static int readXLWideString(byte[] data, int offset, StringBuilder sb) throws XSSFBParseException {
  58. long numChars = LittleEndian.getUInt(data, offset);
  59. if (numChars < 0) {
  60. throw new XSSFBParseException("too few chars to read");
  61. } else if (numChars > 0xFFFFFFFFL) {
  62. throw new XSSFBParseException("too many chars to read");
  63. }
  64. int numBytes = 2*(int)numChars;
  65. offset += 4;
  66. if (offset+numBytes > data.length) {
  67. throw new XSSFBParseException("trying to read beyond data length");
  68. }
  69. sb.append(new String(data, offset, numBytes, Charset.forName("UTF-16LE")));
  70. numBytes+=4;
  71. return numBytes;
  72. }
  73. static int castToInt(long val) {
  74. if (val < Integer.MAX_VALUE && val > Integer.MIN_VALUE) {
  75. return (int)val;
  76. }
  77. throw new POIXMLException("val ("+val+") can't be cast to int");
  78. }
  79. static short castToShort(int val) {
  80. if (val < Short.MAX_VALUE && val > Short.MIN_VALUE) {
  81. return (short)val;
  82. }
  83. throw new POIXMLException("val ("+val+") can't be cast to short");
  84. }
  85. //TODO: move to LittleEndian?
  86. static int get24BitInt( byte[] data, int offset) {
  87. int i = offset;
  88. int b0 = data[i++] & 0xFF;
  89. int b1 = data[i++] & 0xFF;
  90. int b2 = data[i] & 0xFF;
  91. return ( b2 << 16 ) + ( b1 << 8 ) + b0;
  92. }
  93. }