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.

OldTextPiece.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.model;
  16. import java.util.Arrays;
  17. import org.apache.poi.util.Internal;
  18. import org.apache.poi.util.NotImplemented;
  19. /**
  20. * Lightweight representation of a text piece.
  21. * Works in the character domain, not the byte domain, so you
  22. * need to have turned byte references into character
  23. * references before getting here.
  24. */
  25. @Internal
  26. public class OldTextPiece extends TextPiece {
  27. private final byte[] rawBytes;
  28. public OldTextPiece(OldTextPiece other) {
  29. super(other);
  30. rawBytes = (other.rawBytes == null) ? null : other.rawBytes.clone();
  31. }
  32. /**
  33. * @param start Beginning offset in main document stream, in characters.
  34. * @param end Ending offset in main document stream, in characters.
  35. * @param text The raw bytes of our text
  36. */
  37. public OldTextPiece(int start, int end, byte[] text, PieceDescriptor pd) {
  38. super(start, end, text, pd);
  39. this.rawBytes = text;
  40. }
  41. /**
  42. * @return nothing, ever. Always throws an UnsupportedOperationException
  43. */
  44. @NotImplemented
  45. @Override
  46. public boolean isUnicode() {
  47. throw new UnsupportedOperationException();
  48. }
  49. @Override
  50. public StringBuilder getStringBuilder() {
  51. return (StringBuilder) _buf;
  52. }
  53. @Override
  54. public byte[] getRawBytes() {
  55. return rawBytes.clone();
  56. }
  57. /**
  58. * Returns part of the string.
  59. * Works only in characters, not in bytes!
  60. *
  61. * @param start Local start position, in characters
  62. * @param end Local end position, in characters
  63. */
  64. @Override
  65. @Deprecated
  66. @NotImplemented
  67. public String substring(int start, int end) {
  68. throw new UnsupportedOperationException();
  69. }
  70. /**
  71. * Not implemented for OldTextPiece.
  72. * Always throws UnsupportedOperationException
  73. */
  74. @Override
  75. @Deprecated
  76. @NotImplemented
  77. public void adjustForDelete(int start, int length) {
  78. throw new UnsupportedOperationException();
  79. }
  80. /**
  81. * Returns the length, in bytes
  82. */
  83. @Override
  84. public int bytesLength() {
  85. return rawBytes.length;
  86. }
  87. @Override
  88. public int hashCode() {
  89. assert false : "hashCode not designed";
  90. return 42; // any arbitrary constant will do
  91. }
  92. @Override
  93. public boolean equals(Object other) {
  94. return other instanceof OldTextPiece &&
  95. Arrays.equals(rawBytes, ((OldTextPiece)other).rawBytes);
  96. }
  97. public String toString() {
  98. return "OldTextPiece from " + getStart() + " to " + getEnd() + " ("
  99. + getPieceDescriptor() + ")";
  100. }
  101. @Override
  102. public OldTextPiece copy() {
  103. return new OldTextPiece(this);
  104. }
  105. }