forked from colinmarc/sequencefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwritable.go
44 lines (36 loc) · 1.08 KB
/
writable.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
package sequencefile
import (
"bytes"
"encoding/binary"
"fmt"
)
const (
BytesWritableClassName = "org.apache.hadoop.io.BytesWritable"
TextClassName = "org.apache.hadoop.io.Text"
IntWritableClassName = "org.apache.hadoop.io.IntWritable"
LongWritableClassName = "org.apache.hadoop.io.LongWritable"
)
// BytesWritable unwraps a hadoop BytesWritable and returns the actual bytes.
func BytesWritable(b []byte) []byte {
return b[4:]
}
// Text unwraps a Text and returns the deserialized string.
func Text(b []byte) string {
buf := bytes.NewBuffer(b)
n, err := ReadVInt(buf)
if err != nil {
panic(fmt.Sprintf("sequencefile: unwrapping Text: %s", err))
}
if int(n) != buf.Len() {
panic("sequencefile: unwrapping Text: bad length")
}
return string(buf.Bytes())
}
// IntWritable unwraps an IntWritable and returns the deserialized int32.
func IntWritable(b []byte) int32 {
return int32(binary.BigEndian.Uint32(b))
}
// LongWritable unwraps an LongWritable and returns the deserialized int64.
func LongWritable(b []byte) int64 {
return int64(binary.BigEndian.Uint64(b))
}