佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > golang實現(xiàn)java uuid的序列化方法

golang實現(xiàn)java uuid的序列化方法

熱門標(biāo)簽:服務(wù)外包 呼叫中心市場需求 鐵路電話系統(tǒng) 百度競價排名 AI電銷 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站

目前只實現(xiàn)了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) //大端序模式的字節(jié)轉(zhuǎn)為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) //小端序模式的字節(jié)轉(zhuǎn)換
  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);

    }
  }
}

到此這篇關(guān)于golang實現(xiàn)java uuid的序列化方法的文章就介紹到這了,更多相關(guān)golang實現(xiàn)java uuid序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • golang如何自定義json序列化應(yīng)用詳解
  • golang中json反序列化可能遇到的問題

標(biāo)簽:仙桃 銅川 湖南 蘭州 衡水 湘潭 崇左 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang實現(xiàn)java uuid的序列化方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    怀宁县| 黎川县| 浦东新区| 南川市| 镇雄县| 湄潭县| 云阳县| 和顺县| 察隅县| 宁明县| 乌兰浩特市| 同江市| 临夏县| 江西省| 周宁县| 孟州市| 昌宁县| 磐安县| 望城县| 海盐县| 岢岚县| 杂多县| 莎车县| 铜鼓县| 达尔| 博乐市| 龙南县| 皋兰县| 都昌县| 胶州市| 平塘县| 绍兴县| 东城区| 河东区| 马关县| 大名县| 资中县| 宝应县| 湘乡市| 江安县| 扬中市|