欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

golang实现java uuid的序列化方法

程序员文章站 2022-03-21 12:53:49
目前只实现了java生成的固定的uuid:85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28的序列化package mainimport ( "encoding/binary"...

目前只实现了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);

    }
  }
}

到此这篇关于golang实现java uuid的序列化方法的文章就介绍到这了,更多相关golang实现java uuid序列化内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: golang java uuid