module Net::SSH::BufferedIo

def self.extended(object) #:nodoc:

:nodoc:
initialized.
argument. It ensures that the modules instance variables are all properly
Called when the #extend is called on an object, with this module as the
def self.extended(object) #:nodoc:
  # need to use __send__ because #send is overridden in Socket
  object.__send__(:initialize_buffered_io)
end

def available

(See #read_available.)
Returns the number of bytes available to be read from the input buffer.
def available
  input.available
end

def enqueue(data)

is called. Note that the data is _not_ sent immediately by this method!
Enqueues data in the output buffer, to be written when #send_pending
def enqueue(data)
  output.append(data)
end

def fill(n=8192)

if no data was available to be read.
the data to the input buffer. It returns the number of bytes read, or 0
Tries to read up to +n+ bytes of data from the remote end, and appends
def fill(n=8192)
  input.consume!
  data = recv(n)
  debug { "read #{data.length} bytes" }
  input.append(data)
  return data.length
end

def initialize_buffered_io

Module#include to add this module.
explicitly in the +initialize+ method of any class that uses
Object#extend (see Net::SSH::BufferedIo.extended), but must be called
is called automatically when the module is mixed into an object via
Initializes the intput and output buffers for this object. This method
def initialize_buffered_io
  @input = Net::SSH::Buffer.new
  @output = Net::SSH::Buffer.new
end

def input; @input; end

def input; @input; end

def output; @output; end

def output; @output; end

def pending_write?

+false+ otherwise.
Returns +true+ if there is data waiting in the output buffer, and
def pending_write?
  output.length > 0
end

def read_available(length=nil)

all available data is read from the buffer. (See #available.)
Read up to +length+ bytes from the input buffer. If +length+ is nil,
def read_available(length=nil)
  input.read(length || available)
end

def read_buffer #:nodoc:

:nodoc:
def read_buffer #:nodoc:
  input.to_s
end

def send_pending

data was sent, and +false+ otherwise.
Sends as much of the pending output as possible. Returns +true+ if any
def send_pending
  if output.length > 0
    sent = send(output.to_s, 0)
    debug { "sent #{sent} bytes" }
    output.consume!(sent)
    return sent > 0
  else
    return false
  end
end

def wait_for_pending_sends

buffer is empty.
Calls #send_pending repeatedly, if necessary, blocking until the output
def wait_for_pending_sends
  send_pending
  while output.length > 0
    result = Net::SSH::Compat.io_select(nil, [self]) or next
    next unless result[1].any?
    send_pending
  end
end

def write_buffer #:nodoc:

:nodoc:
def write_buffer #:nodoc:
  output.to_s
end