Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
# encoding: ascii-8bit
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
 
21
module Thrift
22
  class MemoryBufferTransport < BaseTransport
23
    GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB
24
 
25
    # If you pass a string to this, you should #dup that string
26
    # unless you want it to be modified by #read and #write
27
    #--
28
    # this behavior is no longer required. If you wish to change it
29
    # go ahead, just make sure the specs pass
30
    def initialize(buffer = nil)
31
      @buf = buffer || ''
32
      @index = 0
33
    end
34
 
35
    def open?
36
      return true
37
    end
38
 
39
    def open
40
    end
41
 
42
    def close
43
    end
44
 
45
    def peek
46
      @index < @buf.size
47
    end
48
 
49
    # this method does not use the passed object directly but copies it
50
    def reset_buffer(new_buf = '')
51
      @buf.replace new_buf
52
      @index = 0
53
    end
54
 
55
    def available
56
      @buf.length - @index
57
    end
58
 
59
    def read(len)
60
      data = @buf.slice(@index, len)
61
      @index += len
62
      @index = @buf.size if @index > @buf.size
63
      if @index >= GARBAGE_BUFFER_SIZE
64
        @buf = @buf.slice(@index..-1)
65
        @index = 0
66
      end
67
      if data.size < len
68
        raise EOFError, "Not enough bytes remain in buffer"
69
      end
70
      data
71
    end
72
 
73
    def write(wbuf)
74
      @buf << wbuf
75
    end
76
 
77
    def flush
78
    end
79
 
80
    def inspect_buffer
81
      out = []
82
      for idx in 0...(@buf.size)
83
        # if idx != 0
84
        #   out << " "
85
        # end
86
 
87
        if idx == @index
88
          out << ">"
89
        end
90
 
91
        out << @buf[idx].to_s(16)
92
      end
93
      out.join(" ")
94
    end
95
  end
96
end