diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2015-07-23 23:53:35 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2015-07-23 23:53:35 +0200 |
commit | ba6713e21232d0a80d665e7e5541c7c3a28eb99e (patch) | |
tree | 97e702ea3afd5d9efbec3d0c70193b761a375361 | |
parent | faf64e8bb35ed93fe9a691f9899389a65e35ae20 (diff) |
Mimic stdlib
-rw-r--r-- | misc/main.go | 5 | ||||
-rw-r--r-- | tde/tde.go | 22 |
2 files changed, 12 insertions, 15 deletions
diff --git a/misc/main.go b/misc/main.go index dcf9b98..fbc967b 100644 --- a/misc/main.go +++ b/misc/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "log" - "io/ioutil" "dim13.org/asn1/tde" ) @@ -212,7 +211,7 @@ func Ask(c tde.TDE, out []byte) []byte { log.Println("Ask") dump(out) c.Write(out) - in, _ := ioutil.ReadAll(c) + in, _ := tde.ReadAll(c) log.Printf("Packet length: 0x%.2X\n", len(in)) dump(in) return in @@ -220,7 +219,7 @@ func Ask(c tde.TDE, out []byte) []byte { func Status(c tde.TDE) { log.Println("Status") - in, _ := ioutil.ReadAll(c) + in, _ := tde.ReadAll(c) log.Printf("Packet length: 0x%.2X\n", len(in)) dump(in) out := status @@ -17,10 +17,7 @@ func (t TDE) Write(p []byte) (n int, err error) { if err := binary.Write(t.Conn, nbo, size); err != nil { return 0, err } - if err := binary.Write(t.Conn, nbo, p); err != nil { - return 0, err - } - return int(size), nil + return t.Conn.Write(p) } func (t TDE) Read(p []byte) (n int, err error) { @@ -28,14 +25,15 @@ func (t TDE) Read(p []byte) (n int, err error) { if err := binary.Read(t.Conn, nbo, &size); err != nil { return 0, err } - if int(size) < len(p) { - p = p[:size] - err = io.EOF - } - if err := binary.Read(t.Conn, nbo, p); err != nil { - return 0, err - } - return int(size), err + return t.Conn.Read(p[:size]) +} + +const maxLen = 240 + +func ReadAll(r io.Reader) ([]byte, error) { + p := make([]byte, maxLen) + n, err := r.Read(p) + return p[:n], err } func Dial(service string) (TDE, error) { |