1/* 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) Close() error { 68 p.Buffer.Reset() 69 return nil 70} 71 72// Flushing a memory buffer is a no-op 73func (p *TMemoryBuffer) Flush() error { 74 return nil 75} 76 77func (p *TMemoryBuffer) RemainingBytes() (num_bytes uint64) { 78 return uint64(p.Buffer.Len()) 79} 80