blob: 04c253804d39791299e4db7abf34fe4e5f435640 [file] [log] [blame]
Jens Geyerb0d3c3f2013-09-09 21:10:45 +02001/*
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
Jens Geyer7a094832013-09-08 00:36:22 +020020package thrift
21
22type TSerializer struct {
23 Transport TTransport
24 Protocol TProtocol
25}
26
27type TStruct interface {
28 Write(p TProtocol) error
29 Read(p TProtocol) error
30}
31
32func NewTSerializer() *TSerializer {
33 var transport TTransport
34 transport = NewTMemoryBufferLen(1024)
35
36 protocol := NewTBinaryProtocolFactoryDefault().GetProtocol(transport)
37
38 return &TSerializer{
39 transport,
40 protocol}
41}
42
43func (t *TSerializer) WriteString(msg TStruct) (s string, err error) {
44 s = ""
45 err = nil
46
47 if err = msg.Write(t.Protocol); err != nil {
48 return
49 }
50
51 if err = t.Protocol.Flush(); err != nil {
52 return
53 }
54 if err = t.Transport.Flush(); err != nil {
55 return
56 }
57
58 var buf []byte
59 var place int
60 buf = make([]byte, 1024)
61 if place, err = t.Transport.Read(buf); err != nil {
62 return
63 }
64
65 s = string(buf[:place])
66 return
67}
68
69func (t *TSerializer) Write(msg TStruct) (b []byte, err error) {
70 err = nil
71
72 if err = msg.Write(t.Protocol); err != nil {
73 return
74 }
75
76 if err = t.Protocol.Flush(); err != nil {
77 return
78 }
79
80 if err = t.Transport.Flush(); err != nil {
81 return
82 }
83
84 var buf []byte
85 var place int
86 buf = make([]byte, 1024)
87 if place, err = t.Transport.Read(buf); err != nil {
88 return
89 }
90
91 b = buf[:place]
92 return
93}