mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 15:36:41 -05:00
get rid off ioutil
This commit is contained in:
parent
ba16987a07
commit
4412d73b28
@ -1,6 +1,11 @@
|
|||||||
package buf
|
package buf
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"v2ray.com/core/common/errors"
|
||||||
|
)
|
||||||
|
|
||||||
type MultiBufferWriter interface {
|
type MultiBufferWriter interface {
|
||||||
WriteMultiBuffer(MultiBuffer) error
|
WriteMultiBuffer(MultiBuffer) error
|
||||||
@ -10,6 +15,36 @@ type MultiBufferReader interface {
|
|||||||
ReadMultiBuffer() (MultiBuffer, error)
|
ReadMultiBuffer() (MultiBuffer, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadAllToMultiBuffer(reader io.Reader) (MultiBuffer, error) {
|
||||||
|
mb := NewMultiBuffer()
|
||||||
|
|
||||||
|
for {
|
||||||
|
b := New()
|
||||||
|
err := b.AppendSupplier(ReadFrom(reader))
|
||||||
|
if !b.IsEmpty() {
|
||||||
|
mb.Append(b)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if errors.Cause(err) == io.EOF {
|
||||||
|
return mb, nil
|
||||||
|
}
|
||||||
|
mb.Release()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadAllToBytes(reader io.Reader) ([]byte, error) {
|
||||||
|
mb, err := ReadAllToMultiBuffer(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
b := make([]byte, mb.Len())
|
||||||
|
mb.Read(b)
|
||||||
|
mb.Release()
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
// MultiBuffer is a list of Buffers. The order of Buffer matters.
|
// MultiBuffer is a list of Buffers. The order of Buffer matters.
|
||||||
type MultiBuffer []*Buffer
|
type MultiBuffer []*Buffer
|
||||||
|
|
||||||
|
10
loader.go
10
loader.go
@ -2,10 +2,11 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/buf"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"v2ray.com/core/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigLoader is an utility to load V2Ray config from external source.
|
// ConfigLoader is an utility to load V2Ray config from external source.
|
||||||
@ -30,7 +31,10 @@ func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
|
|||||||
|
|
||||||
func loadProtobufConfig(input io.Reader) (*Config, error) {
|
func loadProtobufConfig(input io.Reader) (*Config, error) {
|
||||||
config := new(Config)
|
config := new(Config)
|
||||||
data, _ := ioutil.ReadAll(input)
|
data, err := buf.ReadAllToBytes(input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err := proto.Unmarshal(data, config); err != nil {
|
if err := proto.Unmarshal(data, config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user