class Thrift::BufferedTransport

Constants

DEFAULT_BUFFER

Public Class Methods

new(transport) click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
25 def initialize(transport)
26   @transport = transport
27   @wbuf = Bytes.empty_byte_buffer
28   @rbuf = Bytes.empty_byte_buffer
29   @index = 0
30 end

Public Instance Methods

close() click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
40 def close
41   flush
42   @transport.close
43 end
flush() click to toggle source
    # File lib/thrift/transport/buffered_transport.rb
 99 def flush
100   unless @wbuf.empty?
101     @transport.write(@wbuf)
102     @wbuf = Bytes.empty_byte_buffer
103   end
104   
105   @transport.flush
106 end
open() click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
36 def open
37   @transport.open
38 end
open?() click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
32 def open?
33   return @transport.open?
34 end
read(sz) click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
45 def read(sz)
46   @index += sz
47   ret = @rbuf.slice(@index - sz, sz) || Bytes.empty_byte_buffer
48 
49   if ret.length == 0
50     @rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
51     @index = sz
52     ret = @rbuf.slice(0, sz) || Bytes.empty_byte_buffer
53   end
54 
55   ret
56 end
read_byte() click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
58 def read_byte
59   # If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it.
60   if @index >= @rbuf.size
61     @rbuf = @transport.read(DEFAULT_BUFFER)
62     @index = 0
63   end
64 
65   # The read buffer has some data now, read a single byte. Using get_string_byte() avoids
66   # allocating a temp string of size 1 unnecessarily.
67   @index += 1
68   return Bytes.get_string_byte(@rbuf, @index - 1)
69 end
read_into_buffer(buffer, size) click to toggle source

Reads a number of bytes from the transport into the buffer passed.

buffer - The String (byte buffer) to write data to; this is assumed to have a BINARY encoding. size - The number of bytes to read from the transport and write to the buffer.

Returns the number of bytes read.

   # File lib/thrift/transport/buffered_transport.rb
77 def read_into_buffer(buffer, size)
78   i = 0
79   while i < size
80     # If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it.
81     if @index >= @rbuf.size
82       @rbuf = @transport.read(DEFAULT_BUFFER)
83       @index = 0
84     end
85 
86     # The read buffer has some data now, so copy bytes over to the output buffer.
87     byte = Bytes.get_string_byte(@rbuf, @index)
88     Bytes.set_string_byte(buffer, i, byte)
89     @index += 1
90     i += 1
91   end
92   i
93 end
write(buf) click to toggle source
   # File lib/thrift/transport/buffered_transport.rb
95 def write(buf)
96   @wbuf << Bytes.force_binary_encoding(buf)
97 end