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.

ByteVector.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.hyphenation;
  19. import java.io.Serializable;
  20. /**
  21. * This class implements a simple byte vector with access to the
  22. * underlying array.
  23. *
  24. * @author Carlos Villegas <cav@uniscope.co.jp>
  25. */
  26. public class ByteVector implements Serializable {
  27. private static final long serialVersionUID = 1554572867863466772L;
  28. /**
  29. * Capacity increment size
  30. */
  31. private static final int DEFAULT_BLOCK_SIZE = 2048;
  32. private int blockSize;
  33. /**
  34. * The encapsulated array
  35. */
  36. private byte[] array;
  37. /**
  38. * Points to next free item
  39. */
  40. private int n;
  41. /**
  42. * Construct byte vector instance with default block size.
  43. */
  44. public ByteVector() {
  45. this(DEFAULT_BLOCK_SIZE);
  46. }
  47. /**
  48. * Construct byte vector instance.
  49. * @param capacity initial block size
  50. */
  51. public ByteVector(int capacity) {
  52. if (capacity > 0) {
  53. blockSize = capacity;
  54. } else {
  55. blockSize = DEFAULT_BLOCK_SIZE;
  56. }
  57. array = new byte[blockSize];
  58. n = 0;
  59. }
  60. /**
  61. * Construct byte vector instance.
  62. * @param a byte array to use
  63. * TODO should n should be initialized to a.length to be consistent with
  64. * CharVector behavior? [GA]
  65. */
  66. public ByteVector(byte[] a) {
  67. blockSize = DEFAULT_BLOCK_SIZE;
  68. array = a;
  69. n = 0;
  70. }
  71. /**
  72. * Construct byte vector instance.
  73. * @param a byte array to use
  74. * @param capacity initial block size
  75. * TODO should n should be initialized to a.length to be consistent with
  76. * CharVector behavior? [GA]
  77. */
  78. public ByteVector(byte[] a, int capacity) {
  79. if (capacity > 0) {
  80. blockSize = capacity;
  81. } else {
  82. blockSize = DEFAULT_BLOCK_SIZE;
  83. }
  84. array = a;
  85. n = 0;
  86. }
  87. /**
  88. * Obtain byte vector array.
  89. * @return byte array
  90. */
  91. public byte[] getArray() {
  92. return array;
  93. }
  94. /**
  95. * Obtain number of items in array.
  96. * @return number of items
  97. */
  98. public int length() {
  99. return n;
  100. }
  101. /**
  102. * Obtain capacity of array.
  103. * @return current capacity of array
  104. */
  105. public int capacity() {
  106. return array.length;
  107. }
  108. /**
  109. * Pet byte at index.
  110. * @param index the index
  111. * @param val a byte
  112. */
  113. public void put(int index, byte val) {
  114. array[index] = val;
  115. }
  116. /**
  117. * Get byte at index.
  118. * @param index the index
  119. * @return a byte
  120. */
  121. public byte get(int index) {
  122. return array[index];
  123. }
  124. /**
  125. * This is to implement memory allocation in the array. Like malloc().
  126. * @param size to allocate
  127. * @return previous length
  128. */
  129. public int alloc(int size) {
  130. int index = n;
  131. int len = array.length;
  132. if (n + size >= len) {
  133. byte[] aux = new byte[len + blockSize];
  134. System.arraycopy(array, 0, aux, 0, len);
  135. array = aux;
  136. }
  137. n += size;
  138. return index;
  139. }
  140. /**
  141. * Trim byte vector to current length.
  142. */
  143. public void trimToSize() {
  144. if (n < array.length) {
  145. byte[] aux = new byte[n];
  146. System.arraycopy(array, 0, aux, 0, n);
  147. array = aux;
  148. }
  149. }
  150. }