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
|
||||
|
||||
import "net"
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"v2ray.com/core/common/errors"
|
||||
)
|
||||
|
||||
type MultiBufferWriter interface {
|
||||
WriteMultiBuffer(MultiBuffer) error
|
||||
@ -10,6 +15,36 @@ type MultiBufferReader interface {
|
||||
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.
|
||||
type MultiBuffer []*Buffer
|
||||
|
||||
|
10
loader.go
10
loader.go
@ -2,10 +2,11 @@ package core
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"v2ray.com/core/common"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user