PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/hwpf/model/Xst.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 149 lines | 87 code | 21 blank | 41 comment | 11 complexity | 1c9ab6a0acff3b14af8410d387d1cc1b MD5 | raw file
Possible License(s): Apache-2.0
  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.hwpf.model;
  20. import java.util.Arrays;
  21. import org.apache.poi.util.LittleEndian;
  22. /**
  23. * The Xst structure is a string. The string is prepended by its length and is
  24. * not null-terminated.
  25. * <p>
  26. * Documentation quoted from Page 424 of 621. [MS-DOC] -- v20110315 Word (.doc)
  27. * Binary File Format
  28. *
  29. * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
  30. */
  31. public class Xst
  32. {
  33. /**
  34. * An unsigned integer that specifies the number of characters that are
  35. * contained in the rgtchar array.
  36. */
  37. private int _cch;
  38. /**
  39. * An array of 16-bit Unicode characters that make up a string.
  40. */
  41. private char[] _rgtchar;
  42. public Xst()
  43. {
  44. _cch = 0;
  45. _rgtchar = new char[0];
  46. }
  47. public Xst( byte[] data, int startOffset )
  48. {
  49. int offset = startOffset;
  50. _cch = LittleEndian.getUShort( data, offset );
  51. offset += LittleEndian.SHORT_SIZE;
  52. _rgtchar = new char[_cch];
  53. for ( int x = 0; x < _cch; x++ )
  54. {
  55. _rgtchar[x] = (char) LittleEndian.getShort( data, offset );
  56. offset += LittleEndian.SHORT_SIZE;
  57. }
  58. }
  59. public Xst( String str )
  60. {
  61. _cch = str.length();
  62. _rgtchar = str.toCharArray();
  63. }
  64. @Override
  65. public boolean equals( Object obj )
  66. {
  67. if ( this == obj )
  68. return true;
  69. if ( obj == null )
  70. return false;
  71. if ( getClass() != obj.getClass() )
  72. return false;
  73. Xst other = (Xst) obj;
  74. if ( _cch != other._cch )
  75. return false;
  76. if ( !Arrays.equals( _rgtchar, other._rgtchar ) )
  77. return false;
  78. return true;
  79. }
  80. public String getAsJavaString()
  81. {
  82. return new String( _rgtchar );
  83. }
  84. /**
  85. * An unsigned integer that specifies the number of characters that are
  86. * contained in the rgtchar array.
  87. */
  88. public int getCch()
  89. {
  90. return _cch;
  91. }
  92. /**
  93. * An array of 16-bit Unicode characters that make up a string.
  94. */
  95. public char[] getRgtchar()
  96. {
  97. return _rgtchar;
  98. }
  99. public int getSize()
  100. {
  101. return LittleEndian.SHORT_SIZE + _rgtchar.length * 2;
  102. }
  103. @Override
  104. public int hashCode()
  105. {
  106. final int prime = 31;
  107. int result = 1;
  108. result = prime * result + _cch;
  109. result = prime * result + Arrays.hashCode( _rgtchar );
  110. return result;
  111. }
  112. public void serialize( byte[] data, int startOffset )
  113. {
  114. int offset = startOffset;
  115. LittleEndian.putUShort( data, offset, _cch );
  116. offset += LittleEndian.SHORT_SIZE;
  117. for ( char c : _rgtchar )
  118. {
  119. LittleEndian.putShort( data, offset, (short) c );
  120. offset += LittleEndian.SHORT_SIZE;
  121. }
  122. }
  123. @Override
  124. public String toString()
  125. {
  126. return new String( "Xst [" + _cch + "; " + _rgtchar + "]" );
  127. }
  128. }