OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package serialize | 5 package serialize |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 ) | 9 ) |
10 | 10 |
11 // Buffer is the interface which corresponds to the subset of *bytes.Buffer | 11 // Buffer is the interface which corresponds to the subset of *bytes.Buffer |
12 // that this package requires. | 12 // that this package requires. |
13 type Buffer interface { | 13 type Buffer interface { |
14 String() string | 14 String() string |
| 15 Bytes() []byte |
| 16 Len() int |
| 17 |
15 Grow(int) | 18 Grow(int) |
16 | 19 |
17 Read([]byte) (int, error) | 20 Read([]byte) (int, error) |
18 ReadByte() (byte, error) | 21 ReadByte() (byte, error) |
19 | 22 |
20 Write([]byte) (int, error) | 23 Write([]byte) (int, error) |
21 WriteByte(c byte) error | 24 WriteByte(c byte) error |
22 WriteString(s string) (int, error) | 25 WriteString(s string) (int, error) |
23 } | 26 } |
24 | 27 |
(...skipping 14 matching lines...) Expand all Loading... |
39 // decode a reverse-ordered field (including the bytes of all fields after the | 42 // decode a reverse-ordered field (including the bytes of all fields after the |
40 // one we intend to parse) so that the parser can consume as many bytes as it | 43 // one we intend to parse) so that the parser can consume as many bytes as it |
41 // needs (and it only knows the number of bytes it needs as it decodes them). | 44 // needs (and it only knows the number of bytes it needs as it decodes them). |
42 // This InvertibleBuffer lets that happen on the fly without having to flip the | 45 // This InvertibleBuffer lets that happen on the fly without having to flip the |
43 // whole []byte. | 46 // whole []byte. |
44 // | 47 // |
45 // If you know you need it, you'll know it's the right thing. If you're not sure | 48 // If you know you need it, you'll know it's the right thing. If you're not sure |
46 // then you definitely don't need it! | 49 // then you definitely don't need it! |
47 type InvertibleBuffer interface { | 50 type InvertibleBuffer interface { |
48 Buffer | 51 Buffer |
49 » Invert() | 52 » SetInvert(inverted bool) |
50 } | 53 } |
51 | 54 |
52 type invertibleBuffer struct { | 55 type invertibleBuffer struct { |
53 Buffer | 56 Buffer |
54 invert bool | 57 invert bool |
55 } | 58 } |
56 | 59 |
57 // Invertible returns an InvertibleBuffer based on the Buffer. | 60 // Invertible returns an InvertibleBuffer based on the Buffer. |
58 func Invertible(b Buffer) InvertibleBuffer { | 61 func Invertible(b Buffer) InvertibleBuffer { |
59 return &invertibleBuffer{b, false} | 62 return &invertibleBuffer{b, false} |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 106 } |
104 | 107 |
105 func (ib *invertibleBuffer) ReadByte() (byte, error) { | 108 func (ib *invertibleBuffer) ReadByte() (byte, error) { |
106 ret, err := ib.Buffer.ReadByte() | 109 ret, err := ib.Buffer.ReadByte() |
107 if ib.invert { | 110 if ib.invert { |
108 ret = ret ^ 0xFF | 111 ret = ret ^ 0xFF |
109 } | 112 } |
110 return ret, err | 113 return ret, err |
111 } | 114 } |
112 | 115 |
113 func (ib *invertibleBuffer) Invert() { | 116 func (ib *invertibleBuffer) SetInvert(inverted bool) { |
114 » ib.invert = !ib.invert | 117 » ib.invert = inverted |
115 } | 118 } |
OLD | NEW |