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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 org.apache.poi.util.Internal;
  17. import org.apache.poi.util.NotImplemented;
  18. /**
  19. * Lightweight representation of a text piece.
  20. * Works in the character domain, not the byte domain, so you
  21. * need to have turned byte references into character
  22. * references before getting here.
  23. */
  24. @Internal
  25. public class OldTextPiece extends TextPiece {
  26. private final byte[] rawBytes;
  27. /**
  28. * @param start Beginning offset in main document stream, in characters.
  29. * @param end Ending offset in main document stream, in characters.
  30. * @param text The raw bytes of our text
  31. */
  32. public OldTextPiece(int start, int end, byte[] text, PieceDescriptor pd) {
  33. super(start, end, text, pd);
  34. this.rawBytes = text;
  35. if (end < start) {
  36. throw new IllegalStateException("Told we're of negative size! start=" + start + " end=" + end);
  37. }
  38. }
  39. /**
  40. * @return nothing, ever. Always throws an UnsupportedOperationException
  41. * @throws UnsupportedOperationException
  42. */
  43. @NotImplemented
  44. @Override
  45. public boolean isUnicode() {
  46. throw new UnsupportedOperationException();
  47. }
  48. public StringBuilder getStringBuilder() {
  49. return (StringBuilder) _buf;
  50. }
  51. @Override
  52. public byte[] getRawBytes() {
  53. byte[] buf = new byte[rawBytes.length];
  54. System.arraycopy(rawBytes, 0, buf, 0, rawBytes.length);
  55. return buf;
  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. * @throws UnsupportedOperationException
  64. */
  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. @Deprecated
  75. @NotImplemented
  76. public void adjustForDelete(int start, int length) {
  77. throw new UnsupportedOperationException();
  78. }
  79. /**
  80. * Returns the length, in bytes
  81. */
  82. public int bytesLength() {
  83. return rawBytes.length;
  84. }
  85. @Override
  86. public int hashCode() {
  87. assert false : "hashCode not designed";
  88. return 42; // any arbitrary constant will do
  89. }
  90. /**
  91. * Returns the character position we start at.
  92. */
  93. public int getCP() {
  94. return getStart();
  95. }
  96. public String toString() {
  97. return "OldTextPiece from " + getStart() + " to " + getEnd() + " ("
  98. + getPieceDescriptor() + ")";
  99. }
  100. }