您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ContinuableRecordInput.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.hssf.record.cont;
  20. import org.apache.poi.hssf.record.RecordInputStream;
  21. import org.apache.poi.hssf.record.ContinueRecord;
  22. import org.apache.poi.util.LittleEndianInput;
  23. /**
  24. * A decorated {@link RecordInputStream} that can read primitive data types
  25. * (short, int, long, etc.) spanned across a {@link ContinueRecord } boundary.
  26. *
  27. * <p>
  28. * Most records construct themselves from {@link RecordInputStream}.
  29. * This class assumes that a {@link ContinueRecord} record break always occurs at the type boundary,
  30. * however, it is not always so.
  31. * </p>
  32. * Two attachments to <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=50779">Bugzilla 50779</a>
  33. * demonstrate that a CONTINUE break can appear right in between two bytes of a unicode character
  34. * or between two bytes of a <code>short</code>. The problematic portion of the data is
  35. * in a Asian Phonetic Settings Block (ExtRst) of a UnicodeString.
  36. * <p>
  37. * {@link RecordInputStream} greedily requests the bytes to be read and stumbles on such files with a
  38. * "Not enough data (1) to read requested (2) bytes" exception. The <code>ContinuableRecordInput</code>
  39. * class circumvents this "type boundary" rule and reads data byte-by-byte rolling over CONTINUE if necessary.
  40. * </p>
  41. *
  42. * <p>
  43. * YK: For now (March 2011) this class is only used to read
  44. * @link org.apache.poi.hssf.record.common.UnicodeString.ExtRst} blocks of a UnicodeString.
  45. *
  46. * </p>
  47. *
  48. * @author Yegor Kozlov
  49. */
  50. public class ContinuableRecordInput implements LittleEndianInput {
  51. private final RecordInputStream _in;
  52. public ContinuableRecordInput(RecordInputStream in){
  53. _in = in;
  54. }
  55. public int available(){
  56. return _in.available();
  57. }
  58. public byte readByte(){
  59. return _in.readByte();
  60. }
  61. public int readUByte(){
  62. return _in.readUByte();
  63. }
  64. public short readShort(){
  65. return _in.readShort();
  66. }
  67. public int readUShort(){
  68. int ch1 = readUByte();
  69. int ch2 = readUByte();
  70. return (ch2 << 8) + (ch1 << 0);
  71. }
  72. public int readInt(){
  73. int ch1 = _in.readUByte();
  74. int ch2 = _in.readUByte();
  75. int ch3 = _in.readUByte();
  76. int ch4 = _in.readUByte();
  77. return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0);
  78. }
  79. public long readLong(){
  80. int b0 = _in.readUByte();
  81. int b1 = _in.readUByte();
  82. int b2 = _in.readUByte();
  83. int b3 = _in.readUByte();
  84. int b4 = _in.readUByte();
  85. int b5 = _in.readUByte();
  86. int b6 = _in.readUByte();
  87. int b7 = _in.readUByte();
  88. return (((long)b7 << 56) +
  89. ((long)b6 << 48) +
  90. ((long)b5 << 40) +
  91. ((long)b4 << 32) +
  92. ((long)b3 << 24) +
  93. (b2 << 16) +
  94. (b1 << 8) +
  95. (b0 << 0));
  96. }
  97. public double readDouble(){
  98. return _in.readDouble();
  99. }
  100. public void readFully(byte[] buf){
  101. _in.readFully(buf);
  102. }
  103. public void readFully(byte[] buf, int off, int len){
  104. _in.readFully(buf, off, len);
  105. }
  106. }