Class: StringIO
- Inherits:
-
Object
- Object
- StringIO
- Defined in:
- lib/core_ext/stringio.rb
Overview
This extends the ‘StringIO` class from Ruby’s standard library. It adds some methods to handle byte-wise input from a ‘StringIO` object.
Class Method Summary collapse
-
.alloc(size) ⇒ StringIO
Creates a new instance of ‘StringIO` with the given size and fills it with zero-bytes.
Instance Method Summary collapse
-
#byte ⇒ Fixnum
Reads a single byte from the current position of the byte stream.
-
#cstring ⇒ String
Reads a zero-byte terminated string from the current position of the byte stream.
-
#float ⇒ Float
Reads a floating-point integer (32 bit) from the current position of the byte stream.
-
#get ⇒ String
Reads the whole remaining content of the byte stream from the current position to the end.
-
#long ⇒ Fixnum
Reads an unsigned long integer (32 bit) from the current position of the byte stream.
-
#remaining ⇒ Fixnum
Returns the remaining number of bytes from the current position to the end of the byte stream.
-
#short ⇒ Fixnum
Reads an unsigned short integer (16 bit) from the current position of the byte stream.
-
#signed_long ⇒ Fixnum
Reads a signed long integer (32 bit) from the current position of the byte stream.
Class Method Details
.alloc(size) ⇒ StringIO
Creates a new instance of ‘StringIO` with the given size and fills it with zero-bytes.
20 21 22 |
# File 'lib/core_ext/stringio.rb', line 20 def self.alloc(size) new "\0" * size end |
Instance Method Details
#byte ⇒ Fixnum
Reads a single byte from the current position of the byte stream
27 28 29 |
# File 'lib/core_ext/stringio.rb', line 27 def byte read(1)[0].ord end |
#cstring ⇒ String
Reads a zero-byte terminated string from the current position of the byte stream
This reads the stream up until the first occurrence of a zero-byte or the end of the stream. The zero-byte is not included in the returned string.
87 88 89 90 91 |
# File 'lib/core_ext/stringio.rb', line 87 def cstring string = gets("\0")[0..-2] string.force_encoding 'UTF-8' if string.respond_to? :force_encoding string end |
#float ⇒ Float
Reads a floating-point integer (32 bit) from the current position of the byte stream
35 36 37 |
# File 'lib/core_ext/stringio.rb', line 35 def float read(4).unpack('e')[0] end |
#get ⇒ String
Reads the whole remaining content of the byte stream from the current position to the end
44 45 46 |
# File 'lib/core_ext/stringio.rb', line 44 def get read remaining end |
#long ⇒ Fixnum
Reads an unsigned long integer (32 bit) from the current position of the byte stream
52 53 54 |
# File 'lib/core_ext/stringio.rb', line 52 def long read(4).unpack('V')[0] end |
#remaining ⇒ Fixnum
Returns the remaining number of bytes from the current position to the end of the byte stream
60 61 62 |
# File 'lib/core_ext/stringio.rb', line 60 def remaining size - pos end |
#short ⇒ Fixnum
Reads an unsigned short integer (16 bit) from the current position of the byte stream
68 69 70 |
# File 'lib/core_ext/stringio.rb', line 68 def short read(2).unpack('v')[0] end |
#signed_long ⇒ Fixnum
Reads a signed long integer (32 bit) from the current position of the byte stream
76 77 78 |
# File 'lib/core_ext/stringio.rb', line 76 def signed_long read(4).unpack('l')[0] end |