Class: Thrift::MemoryBufferTransport

Inherits:
BaseTransport show all
Defined in:
lib/thrift/transport/memory_buffer_transport.rb

Constant Summary collapse

GARBAGE_BUFFER_SIZE =

4kB

4*(2**10)

Instance Method Summary collapse

Methods inherited from BaseTransport

#read_all, #set_context

Constructor Details

#initialize(buffer = nil) ⇒ MemoryBufferTransport

If you pass a string to this, you should #dup that string unless you want it to be modified by #read and #write – this behavior is no longer required. If you wish to change it go ahead, just make sure the specs pass



30
31
32
33
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 30

def initialize(buffer = nil)
  @buf = buffer ? Bytes.force_binary_encoding(buffer) : Bytes.empty_byte_buffer
  @index = 0
end

Instance Method Details

#availableObject



55
56
57
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 55

def available
  @buf.length - @index
end

#closeObject



42
43
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 42

def close
end

#flushObject



106
107
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 106

def flush
end

#inspect_bufferObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 109

def inspect_buffer
  out = []
  for idx in 0...(@buf.size)
    # if idx != 0
    #   out << " "
    # end
  
    if idx == @index
      out << ">"
    end
  
    out << @buf[idx].ord.to_s(16)
  end
  out.join(" ")
end

#openObject



39
40
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 39

def open
end

#open?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 35

def open?
  return true
end

#peekObject



45
46
47
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 45

def peek
  @index < @buf.size
end

#read(len) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 59

def read(len)
  data = @buf.slice(@index, len)
  @index += len
  @index = @buf.size if @index > @buf.size
  if @index >= GARBAGE_BUFFER_SIZE
    @buf = @buf.slice(@index..-1)
    @index = 0
  end
  if data.size < len
    raise EOFError, "Not enough bytes remain in buffer"
  end
  data
end

#read_byteObject

Raises:

  • (EOFError)


73
74
75
76
77
78
79
80
81
82
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 73

def read_byte
  raise EOFError.new("Not enough bytes remain in buffer") if @index >= @buf.size
  val = Bytes.get_string_byte(@buf, @index)
  @index += 1
  if @index >= GARBAGE_BUFFER_SIZE
    @buf = @buf.slice(@index..-1)
    @index = 0
  end
  val
end

#read_into_buffer(buffer, size) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 84

def read_into_buffer(buffer, size)
  i = 0
  while i < size
    raise EOFError.new("Not enough bytes remain in buffer") if @index >= @buf.size

    # The read buffer has some data now, so copy bytes over to the output buffer.
    byte = Bytes.get_string_byte(@buf, @index)
    Bytes.set_string_byte(buffer, i, byte)
    @index += 1
    i += 1
  end
  if @index >= GARBAGE_BUFFER_SIZE
    @buf = @buf.slice(@index..-1)
    @index = 0
  end
  i
end

#reset_buffer(new_buf = '') ⇒ Object

this method does not use the passed object directly but copies it



50
51
52
53
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 50

def reset_buffer(new_buf = '')
  @buf.replace Bytes.force_binary_encoding(new_buf)
  @index = 0
end

#write(wbuf) ⇒ Object



102
103
104
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 102

def write(wbuf)
  @buf << Bytes.force_binary_encoding(wbuf)
end