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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /**
  29. * @param start Beginning offset in main document stream, in characters.
  30. * @param end Ending offset in main document stream, in characters.
  31. * @param text The raw bytes of our text
  32. */
  33. public OldTextPiece(int start, int end, byte[] text, PieceDescriptor pd) {
  34. super(start, end, text, pd);
  35. this.rawBytes = text;
  36. }
  37. /**
  38. * @return nothing, ever. Always throws an UnsupportedOperationException
  39. * @throws UnsupportedOperationException
  40. */
  41. @NotImplemented
  42. @Override
  43. public boolean isUnicode() {
  44. throw new UnsupportedOperationException();
  45. }
  46. @Override
  47. public StringBuilder getStringBuilder() {
  48. return (StringBuilder) _buf;
  49. }
  50. @Override
  51. public byte[] getRawBytes() {
  52. byte[] buf = new byte[rawBytes.length];
  53. System.arraycopy(rawBytes, 0, buf, 0, rawBytes.length);
  54. return buf;
  55. }
  56. /**
  57. * Returns part of the string.
  58. * Works only in characters, not in bytes!
  59. *
  60. * @param start Local start position, in characters
  61. * @param end Local end position, in characters
  62. * @throws UnsupportedOperationException
  63. */
  64. @Deprecated
  65. @NotImplemented
  66. public String substring(int start, int end) {
  67. throw new UnsupportedOperationException();
  68. }
  69. /**
  70. * Not implemented for OldTextPiece.
  71. * Always throws UnsupportedOperationException
  72. */
  73. @Deprecated
  74. @NotImplemented
  75. public void adjustForDelete(int start, int length) {
  76. throw new UnsupportedOperationException();
  77. }
  78. /**
  79. * Returns the length, in bytes
  80. */
  81. public int bytesLength() {
  82. return rawBytes.length;
  83. }
  84. @Override
  85. public int hashCode() {
  86. assert false : "hashCode not designed";
  87. return 42; // any arbitrary constant will do
  88. }
  89. @Override
  90. public boolean equals(Object other) {
  91. return other instanceof OldTextPiece &&
  92. Arrays.equals(rawBytes, ((OldTextPiece)other).rawBytes);
  93. }
  94. public String toString() {
  95. return "OldTextPiece from " + getStart() + " to " + getEnd() + " ("
  96. + getPieceDescriptor() + ")";
  97. }
  98. }