forked from hoangtq219/murmur3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_buffer.go
191 lines (164 loc) · 4.85 KB
/
byte_buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package murmur3
type ByteBuffer struct {
HB []byte
Offset int
Mark int
Position int
Limit int
Capacity int
BigEndian bool
NativeByteOrder bool
}
func allocateByteBuffer(bufferSize int) *ByteBuffer {
return &ByteBuffer{
HB: make([]byte, bufferSize),
Offset: 0,
Mark: -1,
Position: 0,
Limit: bufferSize,
Capacity: bufferSize,
NativeByteOrder: true,
}
}
func (buf *ByteBuffer) putCharL(index int, val byte) {
buf.HB[index] = val
buf.HB[index+1] = val >> 8
}
func (buf *ByteBuffer) remaining() int {
return buf.Limit - buf.Position
}
func (buf *ByteBuffer) flip() {
buf.Limit = buf.Position
buf.Position = 0
buf.Mark = -1
}
func (buf *ByteBuffer) getLongB(index int) int64 {
return makeLong(get(buf.HB, index), get(buf.HB, index+1), get(buf.HB, index+2), get(buf.HB, index+3), get(buf.HB, index+4), get(buf.HB, index+5), get(buf.HB, index+6), get(buf.HB, index+7))
}
func (buf *ByteBuffer) getLongL(index int) int64 {
return makeLong(get(buf.HB, index+7), get(buf.HB, index+6), get(buf.HB, index+5), get(buf.HB, index+4), get(buf.HB, index+3), get(buf.HB, index+2), get(buf.HB, index+1), get(buf.HB, index))
}
func (buf *ByteBuffer) nextGetIndex(var1 int) int {
if buf.Limit-buf.Position < var1 {
handleWarnPrintf("Buffer Underflow Exception")
} else {
var2 := buf.Position
buf.Position += var1
return var2
}
return -1
}
func (buf *ByteBuffer) getLong() int64 {
if buf.BigEndian {
return buf.getLongB(buf.ix(buf.nextGetIndex(8)))
} else {
return buf.getLongL(buf.ix(buf.nextGetIndex(8)))
}
}
func (buf *ByteBuffer) capacity() int {
return buf.Capacity
}
func (buf *ByteBuffer) discardMark() {
buf.Mark = -1
}
func (buf *ByteBuffer) compact() {
buf.position(buf.remaining())
buf.limit(buf.capacity())
buf.discardMark()
}
func (buf *ByteBuffer) get(index int) byte {
return buf.HB[index]
}
func (buf *ByteBuffer) position(var1 int) {
if var1 <= buf.Limit && var1 >= 0 {
buf.Position = var1
if buf.Mark > buf.Position {
buf.Mark = -1
}
} else {
handleWarnPrintf("Illegal Argument Exception")
}
}
func (buf *ByteBuffer) limit(chunkSize int) {
if chunkSize <= buf.Capacity && chunkSize >= 0 {
buf.Limit = chunkSize
if buf.Position > buf.Limit {
buf.Position = buf.Limit
}
if buf.Mark > buf.Limit {
buf.Mark = -1
}
} else {
handleWarnPrintf("Illegal Argument Exception")
}
}
func (buf *ByteBuffer) ix(nextPutIndex int) int {
return buf.Offset + nextPutIndex
}
func (buf *ByteBuffer) nextPutIndex(var1 int) int {
if buf.Limit-buf.Position < var1 {
handleWarnPrintf("Buffer Overflow Exception!")
return -1
} else {
var2 := buf.Position
buf.Position += var1
return var2
}
}
func (buf *ByteBuffer) putLongB(index int, val int64) {
buf.HB[index] = long7(val)
buf.HB[index+1] = long6(val)
buf.HB[index+2] = long5(val)
buf.HB[index+3] = long4(val)
buf.HB[index+4] = long3(val)
buf.HB[index+5] = long2(val)
buf.HB[index+6] = long1(val)
buf.HB[index+7] = long0(val)
}
func (buf *ByteBuffer) putLongL(index int, val int64) {
buf.HB[index+7] = long7(val)
buf.HB[index+6] = long6(val)
buf.HB[index+5] = long5(val)
buf.HB[index+4] = long4(val)
buf.HB[index+3] = long3(val)
buf.HB[index+2] = long2(val)
buf.HB[index+1] = long1(val)
buf.HB[index] = long0(val)
}
// Puts a long into this sink.
func (buf *ByteBuffer) putLong(val int64) {
if buf.BigEndian {
buf.putLongB(buf.ix(buf.nextPutIndex(8)), val)
} else {
buf.putLongL(buf.ix(buf.nextPutIndex(8)), val)
}
}
func (buf *ByteBuffer) checkIndex(index int) int {
if index >= 0 && index < buf.Limit {
return index
} else {
handleWarnPrintf("Array Index Out Of Bounds Exception")
}
return -1
}
// AsInt() Returns the first four bytes of this hashcode's byte, converted to an int value little-endian order
func (buf *ByteBuffer) AsInt() int {
return int(int32(buf.HB[0])&255 | (int32(buf.HB[1])&255)<<8 | (int32(buf.HB[2])&255)<<16 | (int32(buf.HB[3])&255)<<24)
}
// AsIntBytes() Returns the first four bytes of this hash code as a byte array.
func (buf *ByteBuffer) AsIntBytes() []byte {
return []byte{buf.HB[3], buf.HB[2], buf.HB[1], buf.HB[0]}
}
// AsLongBytes() Returns the first eight bytes of this hash code as a byte array.
func (buf *ByteBuffer) AsLongBytes() []byte {
return []byte{buf.HB[7], buf.HB[6], buf.HB[5], buf.HB[4], buf.HB[3], buf.HB[2], buf.HB[1], buf.HB[0]}
}
// ToBytes() Returns the value of this hash code as a byte array.
func (buf *ByteBuffer) ToBytes() []byte {
return buf.HB
}
// ToString() Returns a string containing each byte of ToBytes(), in order, as a two-digit unsigned hexadecimal number in lower case.
// Note that if the output is considered to be a single hexadecimal number, this hash code's bytes are the big-endian representation of that number.
func (buf *ByteBuffer) ToString() string {
return toString(buf.HB)
}