Project

General

Profile

1
<?php
2
/*
3
 * Licensed to the Apache Software Foundation (ASF) under one
4
 * or more contributor license agreements. See the NOTICE file
5
 * distributed with this work for additional information
6
 * regarding copyright ownership. The ASF licenses this file
7
 * to you under the Apache License, Version 2.0 (the
8
 * "License"); you may not use this file except in compliance
9
 * with the License. You may obtain a copy of the License at
10
 *
11
 *   http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing,
14
 * software distributed under the License is distributed on an
15
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
 * KIND, either express or implied. See the License for the
17
 * specific language governing permissions and limitations
18
 * under the License.
19
 *
20
 * @package thrift.transport
21
 */
22

    
23

    
24
/**
25
 * A memory buffer is a tranpsort that simply reads from and writes to an
26
 * in-memory string buffer. Anytime you call write on it, the data is simply
27
 * placed into a buffer, and anytime you call read, data is read from that
28
 * buffer.
29
 *
30
 * @package thrift.transport
31
 */
32
class TMemoryBuffer extends TTransport {
33

    
34
  /**
35
   * Constructor. Optionally pass an initial value
36
   * for the buffer.
37
   */
38
  public function __construct($buf = '') {
39
    $this->buf_ = $buf;
40
  }
41

    
42
  protected $buf_ = '';
43

    
44
  public function isOpen() {
45
    return true;
46
  }
47

    
48
  public function open() {}
49

    
50
  public function close() {}
51

    
52
  public function write($buf) {
53
    $this->buf_ .= $buf;
54
  }
55

    
56
  public function read($len) {
57
    if (strlen($this->buf_) === 0) {
58
      throw new TTransportException('TMemoryBuffer: Could not read ' .
59
                                    $len . ' bytes from buffer.',
60
                                    TTransportException::UNKNOWN);
61
    }
62

    
63
    if (strlen($this->buf_) <= $len) {
64
      $ret = $this->buf_;
65
      $this->buf_ = '';
66
      return $ret;
67
    }
68

    
69
    $ret = substr($this->buf_, 0, $len);
70
    $this->buf_ = substr($this->buf_, $len);
71

    
72
    return $ret;
73
  }
74

    
75
  function getBuffer() {
76
    return $this->buf_;
77
  }
78

    
79
  public function available() {
80
    return strlen($this->buf_);
81
  }
82
}
(4-4/12)