美文网首页
golang实现java string的序列化

golang实现java string的序列化

作者: EasyNetCN | 来源:发表于2020-09-18 23:13 被阅读0次

golang写入字符串

const (
    STREAM_MAGIC   = 0xaced
    STREAM_VERSION = 5
    TC_STRING      = 0x74
)

func main() {
    f, _ := os.Create("test.out")
    defer f.Close()

    f.Write(ShortBytes(STREAM_MAGIC))
    f.Write(ShortBytes(STREAM_VERSION))
    f.Write([]byte{TC_STRING})

    str := "85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28"

    strLen := len(str)

    f.Write(ShortBytes(uint16(strLen)))
    f.Write([]byte(str))
}

func ShortBytes(i uint16) []byte {
    bytes := make([]byte, 2)

    binary.BigEndian.PutUint16(bytes, i)

    return bytes
}

java读取字符串

public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        readTest();
    }

    private static void readTest() throws IOException, ClassNotFoundException {
        try (var fis = new FileInputStream("test.out"); var is = new ObjectInputStream(fis)) {
            var uuid = is.readObject();

            System.out.print(uuid);

        }
    }
}

相关文章

网友评论

      本文标题:golang实现java string的序列化

      本文链接:https://www.haomeiwen.com/subject/sumsyktx.html