Desktop tool for browsing account info from EVE-Online
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.

TextBuffer.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*--
  2. $Id: TextBuffer.java,v 1.10 2007/11/10 05:29:00 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom.input;
  46. import org.jdom.*;
  47. /**
  48. * A non-public utility class similar to StringBuffer but optimized for XML
  49. * parsing where the common case is that you get only one chunk of characters
  50. * per text section. TextBuffer stores the first chunk of characters in a
  51. * String, which can just be returned directly if no second chunk is received.
  52. * Subsequent chunks are stored in a supplemental char array (like StringBuffer
  53. * uses). In this case, the returned text will be the first String chunk,
  54. * concatenated with the subsequent chunks stored in the char array. This
  55. * provides optimal performance in the common case, while still providing very
  56. * good performance in the uncommon case. Furthermore, avoiding StringBuffer
  57. * means that no extra unused char array space will be kept around after parsing
  58. * is through.
  59. *
  60. * @version $Revision: 1.10 $, $Date: 2007/11/10 05:29:00 $
  61. * @author Bradley S. Huffman
  62. * @author Alex Rosen
  63. */
  64. class TextBuffer {
  65. private static final String CVS_ID =
  66. "@(#) $RCSfile: TextBuffer.java,v $ $Revision: 1.10 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1 $";
  67. /** The first part of the text value (the "prefix"). If null, the
  68. * text value is the empty string. */
  69. private String prefixString;
  70. /** The rest of the text value (the "suffix"). Only the first
  71. * code>arraySize</code> characters are valid. */
  72. private char[] array;
  73. /** The size of the rest of the text value. If zero, then only
  74. * code>prefixString</code> contains the text value. */
  75. private int arraySize;
  76. /** Constructor */
  77. TextBuffer() {
  78. array = new char[4096]; // initial capacity
  79. arraySize = 0;
  80. }
  81. /** Append the specified text to the text value of this buffer. */
  82. void append(char[] source, int start, int count) {
  83. if (prefixString == null) {
  84. // This is the first chunk, so we'll store it in the prefix string
  85. prefixString = new String(source, start, count);
  86. }
  87. else {
  88. // This is a subsequent chunk, so we'll add it to the char array
  89. ensureCapacity(arraySize + count);
  90. System.arraycopy(source, start, array, arraySize, count);
  91. arraySize += count;
  92. }
  93. }
  94. /** Returns the size of the text value. */
  95. int size() {
  96. if (prefixString == null) {
  97. return 0;
  98. }
  99. else {
  100. return prefixString.length() + arraySize;
  101. }
  102. }
  103. /** Clears the text value and prepares the TextBuffer for reuse. */
  104. void clear() {
  105. arraySize = 0;
  106. prefixString = null;
  107. }
  108. boolean isAllWhitespace() {
  109. if ((prefixString == null) || (prefixString.length() == 0)) {
  110. return true;
  111. }
  112. int size = prefixString.length();
  113. for(int i = 0; i < size; i++) {
  114. if ( !Verifier.isXMLWhitespace(prefixString.charAt(i))) {
  115. return false;
  116. }
  117. }
  118. for(int i = 0; i < arraySize; i++) {
  119. if ( !Verifier.isXMLWhitespace(array[i])) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. /** Returns the text value stored in the buffer. */
  126. public String toString() {
  127. if (prefixString == null) {
  128. return "";
  129. }
  130. String str = "";
  131. if (arraySize == 0) {
  132. // Char array is empty, so the text value is just prefixString.
  133. str = prefixString;
  134. }
  135. else {
  136. // Char array is not empty, so the text value is prefixString
  137. // plus the char array.
  138. str = new StringBuffer(prefixString.length() + arraySize)
  139. .append(prefixString)
  140. .append(array, 0, arraySize)
  141. .toString();
  142. }
  143. return str;
  144. }
  145. // Ensure that the char array has room for at least "csize" characters.
  146. private void ensureCapacity(int csize) {
  147. int capacity = array.length;
  148. if (csize > capacity) {
  149. char[] old = array;
  150. int nsize = capacity;
  151. while (csize > nsize) {
  152. nsize += (capacity/2);
  153. }
  154. array = new char[nsize];
  155. System.arraycopy(old, 0, array, 0, arraySize);
  156. }
  157. }
  158. }