golang实现java uuid的序列化方法
作者:EasyNetCN 时间:2022-12-30 11:30:57
目前只实现了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);
}
}
}
来源:https://www.jianshu.com/p/ebf9fe8d234d
标签:golang,java,uuid
0
投稿
猜你喜欢
c# 实现语音合成
2021-06-16 00:17:47
Java Swing null绝对布局的实现示例
2021-07-27 06:07:18
Android App中进行语言的切换
2022-07-07 05:15:47
Android开发实现控件双击事件的监听接口封装类
2023-02-15 00:56:18
springboot结合maven实现多模块打包
2022-01-16 07:13:51
Android编程学习之抽象类AbsListView用法实例分析
2023-03-18 12:19:49
每日六道java新手入门面试题,通往自由的道路
2023-11-13 16:22:29
Android 获取屏幕的多种宽高信息的示例代码
2021-07-10 06:07:10
Android Flutter实现GIF动画效果的方法详解
2023-02-06 02:46:37
Java多线程编程详细解释
2022-12-22 10:19:41
Java并发线程池实例分析讲解
2022-08-05 20:25:40
java 优雅关闭线程池的方案
2022-03-20 23:05:44
java实现的n*n矩阵求值及求逆矩阵算法示例
2022-07-19 06:45:06
详解Java中用于查找对象哈希码值的hashCode()函数
2023-09-17 02:52:19
详解Java的Hibernate框架中的Interceptor和Collection
2023-08-18 04:02:55
Android Compose Column列表不自动刷新问题
2022-08-10 07:48:02
android开机自启动APP及使用adb命令测试方法
2022-03-07 06:28:54
Java中的字节流文件读取教程(一)
2023-08-14 08:14:47
在Java中避免NullPointerException的解决方案
2023-10-17 04:47:00
c# linq的差集,并集,交集,去重代码(分享)
2022-04-18 05:46:11