目前只实现了java生成的固定的uuid:85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28的序列化
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/Shopify/sarama"
"github.com/google/uuid"
)
const (
DATE_TIME_PATTERN = ""
STREAM_MAGIC = 0xaced
STREAM_VERSION = 5
TC_STRING = 0x74
TC_OBJECT = 0x73
TC_CLASSDESC = 0x72
SC_SERIALIZABLE = 0x02
TC_ENDBLOCKDATA = 0x78
TC_NULL = 0x70
)
func main() {
uuidTest()
}
func uuidTest() {
f, _ := os.Create("uuid-go.out")
defer f.Close()
f.Write(ShortBytes(STREAM_MAGIC))
f.Write(ShortBytes(STREAM_VERSION))
f.Write([]byte{TC_OBJECT})
f.Write([]byte{TC_CLASSDESC})
className := "java.util.UUID"
classNameLen := len(className)
f.Write(ShortBytes(uint16(classNameLen)))
f.Write([]byte(className))
sid := -4856846361193249489
f.Write(LongBytes(uint64(sid)))
//flags
f.Write([]byte{2})
//fields length
f.Write(ShortBytes(2))
//field type code
f.Write([]byte{'J'})
f1 := "leastSigBits"
f1Len := len(f1)
f.Write(ShortBytes(uint16(f1Len)))
f.Write([]byte(f1))
//filed type code
f.Write([]byte{'J'})
f2 := "mostSigBits"
f2Len := len(f2)
f.Write(ShortBytes(uint16(f2Len)))
f.Write([]byte(f2))
f.Write([]byte{TC_ENDBLOCKDATA})
f.Write([]byte{TC_NULL})
leastSigBits := -8121893460813967576
f.Write(LongBytes(uint64(leastSigBits)))
mostSigBits := -8810284723775779300
f.Write(LongBytes(uint64(mostSigBits)))
}
func ShortBytes(i uint16) []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, i)
return bytes
}
func LongBytes(i uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, i)
return bytes
}
func BigEndian() { // 大端序
// 二进制形式:0000 0000 0000 0000 0001 0002 0003 0004
var testInt int32 = 0x01020304 // 十六进制表示
fmt.Printf("%d use big endian: \n", testInt)
var testBytes []byte = make([]byte, 4)
binary.BigEndian.PutUint32(testBytes, uint32(testInt)) //大端序模式
fmt.Println("int32 to bytes:", testBytes)
convInt := binary.BigEndian.Uint32(testBytes) //大端序模式的字节转为int32
fmt.Printf("bytes to int32: %d\n\n", convInt)
}
func LittleEndian() { // 小端序
//二进制形式: 0000 0000 0000 0000 0001 0002 0003 0004
var testInt int32 = 0x01020304 // 16进制
fmt.Printf("%d use little endian: \n", testInt)
var testBytes []byte = make([]byte, 4)
binary.LittleEndian.PutUint32(testBytes, uint32(testInt)) //小端序模式
fmt.Println("int32 to bytes:", testBytes)
convInt := binary.LittleEndian.Uint32(testBytes) //小端序模式的字节转换
fmt.Printf("bytes to int32: %d\n\n", convInt)
}
func Int64ToBytes(i int64) []byte {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
java读取测试
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
readUUIDTest();
}
private static void readUUIDTest() throws IOException, ClassNotFoundException {
try (var fis = new FileInputStream("uuid-go.out"); var is = new ObjectInputStream(fis)) {
var uuid = is.readObject();
System.out.print(uuid);
}
}
}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“golang实现java uuid的序列化方法”评论...
更新动态
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]