Class: Svn::Stream

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/svn/streams.rb

Defined Under Namespace

Modules: C

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.release(ptr) ⇒ Object



18
19
20
21
22
# File 'lib/svn/streams.rb', line 18

def release( ptr )
  Error.check_and_raise(
      C.close( ptr )
    )
end

.wrap_io(io, pool = RootPool) ⇒ Object

Wraps an IO object to be used as a subversion stream



11
12
13
14
15
16
# File 'lib/svn/streams.rb', line 11

def wrap_io( io, pool=RootPool )
  stream = new( C.create( Svn::Utils.wrap(io), pool ) )
  C.set_write( stream, C::WriteToIO )
  C.set_read( stream, C::ReadFromIO )
  return stream
end

Instance Method Details

#read(size = 8192) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/svn/streams.rb', line 99

def read( size=8192 )
  # setup the pointers
  @in_out_len ||= FFI::MemoryPointer.new( :size_t )
  @in_out_len.write_ulong( size )

  # make sure a buffer for reading exists and save it for reuse
  if @read_buf.nil? or @read_buf.size < size
    @read_buf = FFI::Buffer.alloc_out( size )
  end

  # call read to fill the buffer
  Error.check_and_raise(
      C.read( self, @read_buf, @in_out_len )
    )

  @read_buf.read_bytes( @in_out_len.read_ulong )
end

#read_allObject Also known as: to_s

reads the stream contents into a String object



118
119
120
121
122
123
124
# File 'lib/svn/streams.rb', line 118

def read_all
  content = String.new
  while bytes = read and !bytes.empty?
    content << bytes
  end
  content
end

#to_counted_stringObject

reads the entire stream and creates a CountedString from the contents

Note that this function copies the entire stream into Ruby memory and then copies it again into C memory. There is probably a more efficient way to do this, by allocating a big string and re-allocing when the size required overruns that memory



133
134
135
# File 'lib/svn/streams.rb', line 133

def to_counted_string
  CountedString.from_string( read_all )
end

#to_string_ioObject

reads the stream contents into a StringIO object



138
139
140
141
142
143
144
145
# File 'lib/svn/streams.rb', line 138

def to_string_io
  content = StringIO.new
  while bytes = read and !bytes.empty?
    content.write( bytes )
  end
  content.rewind
  content
end