예제 #1
0
func p2h(w io.Writer, r io.Reader) error {
	br := bufio.NewReader(r)

	for {
		p, err := br.ReadBytes('\n')
		if err != nil {
			if err == io.EOF {
				return nil
			}
			return err
		}
		p = p[:len(p)-1] // remove \n

		hdr := mc.Header(p)

		_, err = w.Write(hdr)
		if err != nil {
			return err
		}
	}
}
예제 #2
0
func filter(w io.Writer, r io.Reader, path string) error {
	hdr := mc.Header([]byte(path))

	// as we decode, write everything to a buffer.
	buf := bytes.NewBuffer(nil)
	r = io.TeeReader(r, buf)

	return decode(r, func(codec *mux.Multicodec, value interface{}) error {
		defer buf.Reset()

		// c.Last is the last multicodec encoded or decoded.
		// skip headers which do not match.
		if !bytes.Equal(hdr, codec.Last.Header()) {
			return nil
		}

		_, err := io.Copy(w, buf)
		if err != nil && err != io.EOF {
			return err
		}
		return nil
	})
}
예제 #3
0
func init() {
	Header = mc.Header([]byte("/multicodec"))
}
예제 #4
0
package b64

import (
	"encoding/base64"
	"io"

	mc "github.com/multiformats/go-multicodec"
	base "github.com/multiformats/go-multicodec/base"
)

var (
	HeaderPath = "/base64/"
	Header     = mc.Header([]byte(HeaderPath))
	multic     = mc.NewMulticodecFromCodec(Codec(), Header)
)

type codec struct{}

type decoder struct {
	r io.Reader
}

func (d decoder) Decode(v interface{}) error {
	out, ok := v.([]byte)
	if !ok {
		return base.ErrExpectedByteSlice
	}

	_, err := d.r.Read(out)

	return err
예제 #5
0
func init() {
	Header = mc.Header([]byte("/msgio"))
}
예제 #6
0
func init() {
	HeaderPath = "/json"
	HeaderMsgioPath = "/json/msgio"
	Header = mc.Header([]byte(HeaderPath))
	HeaderMsgio = mc.Header([]byte(HeaderMsgioPath))
}
예제 #7
0
func init() {
	Header = mc.Header([]byte("/protobuf"))
	HeaderMsgio = mc.Header([]byte("/protobuf/msgio"))
}
예제 #8
0
func init() {
	HeaderPath = "/cbor"
	Header = mc.Header([]byte(HeaderPath))
}
예제 #9
0
func codecWithPath(path string) mc.Multicodec {
	hdr := mc.Header([]byte(path))
	codec := mux.CodecWithHeader(hdr, mux.StandardMux().Codecs)
	return codec
}
예제 #10
0
func header(w io.Writer, path string) error {
	_, err := w.Write(mc.Header([]byte(path)))
	return err
}