1 11 package org.eclipse.core.internal.indexing; 12 13 16 public class Buffer { 17 protected byte[] contents; 18 private static final byte[] ZEROES = new byte[1024]; 19 20 24 public Buffer(byte[] contents) { 25 this.contents = contents; 26 } 27 28 31 public Buffer(int n) { 32 contents = new byte[n]; 33 } 34 35 38 public Buffer(Insertable anObject) { 39 this.contents = anObject.toByteArray(); 40 } 41 42 public void clear() { 43 clear(contents, 0, contents.length); 44 } 45 46 private static void clear(byte[] buffer, int offset, int length) { 47 int n = length; 48 int p = offset; 49 while (n > 0) { 50 int m = Math.min(ZEROES.length, n); 51 System.arraycopy(ZEROES, 0, buffer, p, m); 52 p += m; 53 n -= m; 54 } 55 } 56 57 public void clear(int offset, int length) { 58 clear(contents, offset, length); 59 } 60 61 private static int compare(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { 62 if (length1 < length2) { 63 return -compare(buffer2, offset2, length2, buffer1, offset1, length1); 64 } 65 for (int i = 0; i < length2; i++) { 66 int j1 = buffer1[offset1 + i] & 255; 67 int j2 = buffer2[offset2 + i] & 255; 68 if (j1 > j2) 69 return 1; 70 if (j1 < j2) 71 return -1; 72 } 73 if (length1 > length2) 74 return 1; 75 return 0; 76 } 77 78 public static int compare(Buffer buffer1, int offset1, int length1, Buffer buffer2, int offset2, int length2) { 79 return compare(buffer1.contents, offset1, length1, buffer2.contents, offset2, length2); 80 } 81 82 public void copyInternal(int fromOffset, int toOffset, int length) { 83 System.arraycopy(contents, fromOffset, contents, toOffset, length); 84 } 85 86 public void copyTo(byte[] buffer) { 87 int n = Math.min(buffer.length, contents.length); 88 System.arraycopy(contents, 0, buffer, 0, n); 89 } 90 91 public void copyFrom(byte[] buffer) { 92 int n = Math.min(buffer.length, contents.length); 93 System.arraycopy(buffer, 0, contents, 0, n); 94 } 95 96 public byte[] get() { 97 return get(0, contents.length); 98 } 99 100 public byte[] get(int offset, int length) { 101 byte[] result = new byte[length]; 102 System.arraycopy(contents, offset, result, 0, length); 103 return result; 104 } 105 106 public Field getField(int offset, int length) { 107 return new Field(this, offset, length); 108 } 109 110 public byte getByte(int offset) { 111 return contents[offset]; 112 } 113 114 public int getInt(int offset, int length) { 115 return (int) getLong(offset, length); 116 } 117 118 public int getUInt(int offset, int length) { 119 int shift = Math.max(0, 32 - (length * 8)); 120 int mask = (-1 >>> shift) & Integer.MAX_VALUE; 121 return getInt(offset, length) & mask; 122 } 123 124 public long getLong(int offset, int length) { 125 if (length <= 0) 126 return 0; 127 long v = contents[offset]; 128 for (int i = offset + 1; i < offset + length; i++) { 129 v = (v << 8) | (contents[i] & 255); 130 } 131 return v; 132 } 133 134 public byte[] getByteArray() { 135 return contents; 136 } 137 138 public int length() { 139 return contents.length; 140 } 141 142 public void put(int offset, byte value) { 143 contents[offset] = value; 144 } 145 146 public void put(int offset, byte[] source) { 147 System.arraycopy(source, 0, contents, offset, source.length); 148 } 149 150 public void put(int offset, int length, byte[] source) { 151 int n = Math.min(length, source.length); 152 System.arraycopy(source, 0, contents, offset, n); 153 } 154 155 public void put(int offset, int length, long n) { 156 long v = n; 157 int i = offset + length; 158 while (i > offset) { 159 i--; 160 contents[i] = (byte) v; 161 v = (v >>> 8); 162 } 163 } 164 165 public void put(int offset, int length, int n) { 166 put(offset, length, (long) n); 167 } 168 169 public void put(int offset, Insertable source) { 170 put(offset, source.toByteArray()); 171 } 172 173 } 174 | Popular Tags |