1 4 package gnu.lists; 5 import java.io.*; 6 7 8 9 public class S8Vector extends SimpleVector 10 implements Externalizable 11 12 , Comparable 13 14 { 15 byte[] data; 16 protected static byte[] empty = new byte[0]; 17 18 public S8Vector () 19 { 20 data = empty; 21 } 22 23 public S8Vector(int size, byte value) 24 { 25 byte[] array = new byte[size]; 26 data = array; 27 this.size = size; 28 while (--size >= 0) 29 array[size] = value; 30 } 31 32 public S8Vector(int size) 33 { 34 this.data = new byte[size]; 35 this.size = size; 36 } 37 38 public S8Vector (byte[] data) 39 { 40 this.data = data; 41 size = data.length; 42 } 43 44 public S8Vector(Sequence seq) 45 { 46 data = new byte[seq.size()]; 47 addAll(seq); 48 } 49 50 51 public int getBufferLength() 52 { 53 return data.length; 54 } 55 56 public void setBufferLength(int length) 57 { 58 int oldLength = data.length; 59 if (oldLength != length) 60 { 61 byte[] tmp = new byte[length]; 62 System.arraycopy(data, 0, tmp, 0, 63 oldLength < length ? oldLength : length); 64 data = tmp; 65 } 66 } 67 68 protected Object getBuffer() { return data; } 69 70 public final byte byteAt(int index) 71 { 72 if (index > size) 73 throw new IndexOutOfBoundsException (); 74 return data[index]; 75 } 76 77 public final byte byteAtBuffer(int index) 78 { 79 return data[index]; 80 } 81 82 public final int intAtBuffer(int index) 83 { 84 return data[index]; 85 } 86 87 public final Object get(int index) 88 { 89 if (index > size) 90 throw new IndexOutOfBoundsException (); 91 return Convert.toObject(data[index]); 92 } 93 94 public final Object getBuffer(int index) 95 { 96 return Convert.toObject(data[index]); 97 } 98 99 public Object setBuffer(int index, Object value) 100 { 101 byte old = data[index]; 102 data[index] = Convert.toByte(value); 103 return Convert.toObject(old); 104 } 105 106 public final void setByteAt(int index, byte value) 107 { 108 if (index > size) 109 throw new IndexOutOfBoundsException (); 110 data[index] = value; 111 } 112 113 public final void setByteAtBuffer(int index, byte value) 114 { 115 data[index] = value; 116 } 117 118 protected void clearBuffer(int start, int count) 119 { 120 while (--count >= 0) 121 data[start++] = 0; 122 } 123 124 public int getElementKind() 125 { 126 return INT_S8_VALUE; 127 } 128 129 public String getTag() { return "s8"; } 130 131 public boolean consumeNext (int ipos, Consumer out) 132 { 133 int index = ipos >>> 1; 134 if (index >= size) 135 return false; 136 out.writeInt(data[index]); 137 return true; 138 } 139 140 public void consumePosRange (int iposStart, int iposEnd, Consumer out) 141 { 142 if (out.ignoring()) 143 return; 144 int i = iposStart >>> 1; 145 int end = iposEnd >>> 1; 146 if (end > size) 147 end = size; 148 for (; i < end; i++) 149 out.writeInt(data[i]); 150 } 151 152 public int compareTo(Object obj) 153 { 154 return compareToInt(this, (S8Vector) obj); 155 } 156 157 161 public void writeExternal(ObjectOutput out) throws IOException 162 { 163 int size = this.size; 164 out.writeInt(size); 165 for (int i = 0; i < size; i++) 166 out.writeByte(data[i]); 167 } 168 169 public void readExternal(ObjectInput in) 170 throws IOException, ClassNotFoundException 171 { 172 int size = in.readInt(); 173 byte[] data = new byte[size]; 174 for (int i = 0; i < size; i++) 175 data[i] = in.readByte(); 176 this.data = data; 177 this.size = size; 178 } 179 } 180 | Popular Tags |