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 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.layout.hyphenation;
  8. import java.io.Serializable;
  9. /**
  10. * This class implements a simple byte vector with access to the
  11. * underlying array.
  12. *
  13. * @author Carlos Villegas <cav@uniscope.co.jp>
  14. */
  15. public class ByteVector implements Serializable {
  16. /**
  17. * Capacity increment size
  18. */
  19. private static final int DEFAULT_BLOCK_SIZE = 2048;
  20. private int BLOCK_SIZE;
  21. /**
  22. * The encapsulated array
  23. */
  24. private byte[] array;
  25. /**
  26. * Points to next free item
  27. */
  28. private int n;
  29. public ByteVector() {
  30. this(DEFAULT_BLOCK_SIZE);
  31. }
  32. public ByteVector(int capacity) {
  33. if (capacity > 0)
  34. BLOCK_SIZE = capacity;
  35. else
  36. BLOCK_SIZE = DEFAULT_BLOCK_SIZE;
  37. array = new byte[BLOCK_SIZE];
  38. n = 0;
  39. }
  40. public ByteVector(byte[] a) {
  41. BLOCK_SIZE = DEFAULT_BLOCK_SIZE;
  42. array = a;
  43. n = 0;
  44. }
  45. public ByteVector(byte[] a, int capacity) {
  46. if (capacity > 0)
  47. BLOCK_SIZE = capacity;
  48. else
  49. BLOCK_SIZE = DEFAULT_BLOCK_SIZE;
  50. array = a;
  51. n = 0;
  52. }
  53. public byte[] getArray() {
  54. return array;
  55. }
  56. /**
  57. * return number of items in array
  58. */
  59. public int length() {
  60. return n;
  61. }
  62. /**
  63. * returns current capacity of array
  64. */
  65. public int capacity() {
  66. return array.length;
  67. }
  68. public void put(int index, byte val) {
  69. array[index] = val;
  70. }
  71. public byte get(int index) {
  72. return array[index];
  73. }
  74. /**
  75. * This is to implement memory allocation in the array. Like malloc().
  76. */
  77. public int alloc(int size) {
  78. int index = n;
  79. int len = array.length;
  80. if (n + size >= len) {
  81. byte[] aux = new byte[len + BLOCK_SIZE];
  82. System.arraycopy(array, 0, aux, 0, len);
  83. array = aux;
  84. }
  85. n += size;
  86. return index;
  87. }
  88. public void trimToSize() {
  89. if (n < array.length) {
  90. byte[] aux = new byte[n];
  91. System.arraycopy(array, 0, aux, 0, n);
  92. array = aux;
  93. }
  94. }
  95. }