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

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



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

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

Instance Method Details

#availableObject



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

def available
  @buf.length - @index
end

#closeObject



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

def close
end

#flushObject



113
114
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 113

def flush
end

#inspect_bufferObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 116

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



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

def open
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  return true
end

#peekObject



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

def peek
  @index < @buf.size
end

#read(len) ⇒ Object



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

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_all(size) ⇒ Object

Raises:



74
75
76
77
78
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 74

def read_all(size)
  raise TransportException.new(TransportException::NEGATIVE_SIZE, 'Negative size') unless size >= 0

  read(size)
end

#read_byteObject

Raises:

  • (EOFError)


80
81
82
83
84
85
86
87
88
89
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 80

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 91

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



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

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

#to_sObject



132
133
134
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 132

def to_s
  "memory"
end

#write(wbuf) ⇒ Object



109
110
111
# File 'lib/thrift/transport/memory_buffer_transport.rb', line 109

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