blob: c48e08935e9419d355ba0ef30eb1b89695364d4d [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +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
20package thrift
21
22import (
23 "bytes"
24)
25
26// Memory buffer-based implementation of the TTransport interface.
27type TMemoryBuffer struct {
28 *bytes.Buffer
29 size int
30}
31
32type TMemoryBufferTransportFactory struct {
33 size int
34}
35
36func (p *TMemoryBufferTransportFactory) GetTransport(trans TTransport) TTransport {
37 if trans != nil {
38 t, ok := trans.(*TMemoryBuffer)
39 if ok && t.size > 0 {
40 return NewTMemoryBufferLen(t.size)
41 }
42 }
43 return NewTMemoryBufferLen(p.size)
44}
45
46func NewTMemoryBufferTransportFactory(size int) *TMemoryBufferTransportFactory {
47 return &TMemoryBufferTransportFactory{size: size}
48}
49
50func NewTMemoryBuffer() *TMemoryBuffer {
51 return &TMemoryBuffer{Buffer: &bytes.Buffer{}, size: 0}
52}
53
54func NewTMemoryBufferLen(size int) *TMemoryBuffer {
55 buf := make([]byte, 0, size)
56 return &TMemoryBuffer{Buffer: bytes.NewBuffer(buf), size: size}
57}
58
59func (p *TMemoryBuffer) IsOpen() bool {
60 return true
61}
62
63func (p *TMemoryBuffer) Open() error {
64 return nil
65}
66
67func (p *TMemoryBuffer) Peek() bool {
68 return p.IsOpen()
69}
70
71func (p *TMemoryBuffer) Close() error {
72 p.Buffer.Reset()
73 return nil
74}
75
76// Flushing a memory buffer is a no-op
77func (p *TMemoryBuffer) Flush() error {
78 return nil
79}