blob: 83fdf29f5cb32c98a55e26b234b3c4104231bdea [file] [log] [blame]
khenaidooc6c7bda2020-06-17 17:20:18 -04001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package thrift
21
khenaidood948f772021-08-11 17:49:24 -040022import (
23 "errors"
24 "io"
25)
khenaidooc6c7bda2020-06-17 17:20:18 -040026
27type RichTransport struct {
28 TTransport
29}
30
31// Wraps Transport to provide TRichTransport interface
32func NewTRichTransport(trans TTransport) *RichTransport {
33 return &RichTransport{trans}
34}
35
36func (r *RichTransport) ReadByte() (c byte, err error) {
37 return readByte(r.TTransport)
38}
39
40func (r *RichTransport) WriteByte(c byte) error {
41 return writeByte(r.TTransport, c)
42}
43
44func (r *RichTransport) WriteString(s string) (n int, err error) {
45 return r.Write([]byte(s))
46}
47
48func (r *RichTransport) RemainingBytes() (num_bytes uint64) {
49 return r.TTransport.RemainingBytes()
50}
51
52func readByte(r io.Reader) (c byte, err error) {
53 v := [1]byte{0}
54 n, err := r.Read(v[0:1])
khenaidood948f772021-08-11 17:49:24 -040055 if n > 0 && (err == nil || errors.Is(err, io.EOF)) {
khenaidooc6c7bda2020-06-17 17:20:18 -040056 return v[0], nil
57 }
58 if n > 0 && err != nil {
59 return v[0], err
60 }
61 if err != nil {
62 return 0, err
63 }
64 return v[0], nil
65}
66
67func writeByte(w io.Writer, c byte) error {
68 v := [1]byte{c}
69 _, err := w.Write(v[0:1])
70 return err
71}