Class Thrift::BufferedTransport
In: lib/thrift/transport/buffered_transport.rb
lib/thrift/transport/buffered_transport.rb
Parent: BaseTransport

Methods

close   close   flush   flush   new   new   open   open   open?   open?   read   read   read_byte   read_byte   read_into_buffer   read_into_buffer   write   write  

Constants

DEFAULT_BUFFER = 4096
DEFAULT_BUFFER = 4096

Public Class methods

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 25
25:     def initialize(transport)
26:       @transport = transport
27:       @wbuf = Bytes.empty_byte_buffer
28:       @rbuf = Bytes.empty_byte_buffer
29:       @index = 0
30:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 25
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

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 40
40:     def close
41:       flush
42:       @transport.close
43:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 40
40:     def close
41:       flush
42:       @transport.close
43:     end

[Source]

     # File lib/thrift/transport/buffered_transport.rb, line 99
 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

[Source]

     # File lib/thrift/transport/buffered_transport.rb, line 99
 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

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 36
36:     def open
37:       @transport.open
38:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 36
36:     def open
37:       @transport.open
38:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 32
32:     def open?
33:       return @transport.open?
34:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 32
32:     def open?
33:       return @transport.open?
34:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 45
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

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 45
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

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 58
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

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 58
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

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.

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 77
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

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.

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 77
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

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 95
95:     def write(buf)
96:       @wbuf << Bytes.force_binary_encoding(buf)
97:     end

[Source]

    # File lib/thrift/transport/buffered_transport.rb, line 95
95:     def write(buf)
96:       @wbuf << Bytes.force_binary_encoding(buf)
97:     end

[Validate]