mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-21 20:36:01 +03:00
CBOR
Signed-off-by: Bruce <weichou1229@gmail.com>
This commit is contained in:
parent
874d7b4987
commit
2c81132a50
BIN
playground/cbor/2.png
Normal file
BIN
playground/cbor/2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 MiB |
53
playground/cbor/cbor2_test.go
Normal file
53
playground/cbor/cbor2_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package cbor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/2tvenom/cbor"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Name string
|
||||
Content []byte
|
||||
}
|
||||
|
||||
func TestCborEncode2(t *testing.T) {
|
||||
// Read origin file
|
||||
b := new(bytes.Buffer)
|
||||
err := getImageBytes("./on.png", b)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
origin := b.Bytes()
|
||||
|
||||
//image := &Image{Name:"on",Content:origin}
|
||||
|
||||
// Encode
|
||||
fmt.Println("--------- Encode ---------")
|
||||
|
||||
var buf bytes.Buffer
|
||||
encoder := cbor.NewEncoder(&buf)
|
||||
ok, error := encoder.Marshal(origin)
|
||||
|
||||
//check binary string
|
||||
if !ok {
|
||||
fmt.Errorf("Error decoding %s", error)
|
||||
} else {
|
||||
fmt.Println("Size: ", len(buf.Bytes()))
|
||||
}
|
||||
|
||||
// Decode
|
||||
//fmt.Println("--------- Decode ---------",buf.Bytes())
|
||||
//var img []byte
|
||||
//ok, err = encoder.Unmarshal(buf.Bytes(), &img)
|
||||
//if !ok {
|
||||
// fmt.Printf("Error Unmarshal %s", err)
|
||||
// return
|
||||
//}
|
||||
////output
|
||||
//fmt.Printf("%v", img)
|
||||
|
||||
}
|
48
playground/cbor/cbor3_test.go
Normal file
48
playground/cbor/cbor3_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package cbor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"go.mozilla.org/cose"
|
||||
)
|
||||
|
||||
func TestCborEncode3(t *testing.T) {
|
||||
// Read origin file
|
||||
b := new(bytes.Buffer)
|
||||
err := getImageBytes("./on.png", b)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
origin := b.Bytes()
|
||||
|
||||
//image := &Image{Name:"on",Content:origin}
|
||||
fmt.Println("Size: ", len(origin))
|
||||
// Encode
|
||||
fmt.Println("--------- Encode ---------")
|
||||
|
||||
encoded, error := cose.Marshal(origin)
|
||||
|
||||
if error != nil {
|
||||
fmt.Errorf("Error decoding %s", error)
|
||||
} else {
|
||||
fmt.Println("Size: ", len(encoded))
|
||||
//fmt.Println("Content: ",string(encoded))
|
||||
}
|
||||
|
||||
// Decode
|
||||
fmt.Println("--------- Decode ---------")
|
||||
//var img Image
|
||||
unmarshal, err := cose.Unmarshal(encoded)
|
||||
if err != nil {
|
||||
fmt.Printf("Error Unmarshal %s", err)
|
||||
return
|
||||
} else {
|
||||
fmt.Println("Size: ", len(unmarshal.([]byte)))
|
||||
//fmt.Println("Content: ",unmarshal.([]byte))
|
||||
}
|
||||
writeToFile("3", unmarshal.([]byte))
|
||||
|
||||
}
|
104
playground/cbor/cbor_test.go
Normal file
104
playground/cbor/cbor_test.go
Normal file
@ -0,0 +1,104 @@
|
||||
package cbor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
func TestCborEncode(t *testing.T) {
|
||||
// Read origin file
|
||||
buf := new(bytes.Buffer)
|
||||
err := getImageBytes("./on.png", buf)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
origin := buf.Bytes()
|
||||
|
||||
fmt.Println("size:", len(origin))
|
||||
writeToFile("1", origin)
|
||||
|
||||
// Encode
|
||||
encoded, err := encodeBinaryValue(buf.Bytes())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("size:", len(encoded))
|
||||
writeToFile("2", encoded)
|
||||
|
||||
// Decode
|
||||
decoded, err := decodeBinaryValue(encoded)
|
||||
fmt.Println("size:", len(decoded))
|
||||
writeToFile("3", decoded)
|
||||
}
|
||||
|
||||
func encodeBinaryValue(b []byte) ([]byte, error) {
|
||||
var ch codec.CborHandle
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
enc := codec.NewEncoder(buf, &ch)
|
||||
err := enc.Encode(&b)
|
||||
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
func decodeBinaryValue(b []byte) ([]byte, error) {
|
||||
var ch codec.CborHandle
|
||||
var decoded []byte
|
||||
var bufReader = bufio.NewReader(bytes.NewReader(b))
|
||||
var dec = codec.NewDecoder(bufReader, &ch)
|
||||
var err = dec.Decode(&decoded)
|
||||
return decoded, err
|
||||
}
|
||||
|
||||
func getImageBytes(imgFile string, buf *bytes.Buffer) error {
|
||||
// Read existing image from file
|
||||
img, err := os.Open(imgFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
// TODO: Attach MediaType property, determine if decoding
|
||||
// early is required (to optimize edge processing)
|
||||
|
||||
// Expect "png" or "jpeg" image type
|
||||
imageData, imageType, err := image.Decode(img)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Finished with file. Reset file pointer
|
||||
img.Seek(0, 0)
|
||||
if imageType == "jpeg" {
|
||||
err = jpeg.Encode(buf, imageData, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if imageType == "png" {
|
||||
err = png.Encode(buf, imageData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeToFile(fileName string, b []byte) {
|
||||
f, _ := os.Create(fileName)
|
||||
n2, err := f.Write(b)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("wrote file %d bytes\n", n2)
|
||||
}
|
BIN
playground/cbor/on.png
Normal file
BIN
playground/cbor/on.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
Loading…
Reference in New Issue
Block a user