Module: FakeIO

Includes:
Enumerable, File::Constants
Defined in:
lib/fake_io.rb,
lib/fake_io/version.rb

Overview

The FakeIO module provides an API for communicating with controlled resources, that is still compatible with the standard IO class.

To utilize the FakeIO module, simply include it into a class and define either #io_read and/or #io_write, to handle the reading and writing of data.

The #io_open method handles optionally opening and assigning the file descriptor for the IO stream. The #io_close method handles optionally closing the IO stream.

Constant Summary collapse

VERSION =

fake_io version

'0.1.0'

Instance Attribute Summary collapse

Abstract Methods collapse

Instance Method Summary collapse

Instance Attribute Details

#autoclose=(value) ⇒ Object (writeonly)

Note:

For compatibility with IO.

Sets whether the IO stream will be auto-closed when finalized.



894
895
896
# File 'lib/fake_io.rb', line 894

def autoclose=(value)
  @autoclose = value
end

#close_on_exec=(value) ⇒ Object (writeonly)

Note:

For compatibility with IO.

Sets the close-on-exec flag.



908
909
910
# File 'lib/fake_io.rb', line 908

def close_on_exec=(value)
  @close_on_exec = value
end

#eofObject (readonly) Also known as: eof?

The end-of-file indicator



25
26
27
# File 'lib/fake_io.rb', line 25

def eof
  @eof
end

#external_encodingEncoding

The external encoding to convert all read data into.

Returns:

  • (Encoding)


32
33
34
# File 'lib/fake_io.rb', line 32

def external_encoding
  @external_encoding
end

#internal_encodingEncoding?

The internal encoding to convert all internal data to.

Returns:

  • (Encoding, nil)


37
38
39
# File 'lib/fake_io.rb', line 37

def internal_encoding
  @internal_encoding
end

#pidInteger? (readonly)

The PID associated with the IO stream.

Returns:

  • (Integer, nil)

    Returns the PID number or nil. Returns nil by default.



764
765
766
# File 'lib/fake_io.rb', line 764

def pid
  @pid
end

#posObject Also known as: tell

The position within the IO stream



20
21
22
# File 'lib/fake_io.rb', line 20

def pos
  @pos
end

#syncBoolean

The sync flag.

Returns:

  • (Boolean)

    Returns the sync mode, for compatibility with IO.



956
957
958
# File 'lib/fake_io.rb', line 956

def sync
  @sync
end

Instance Method Details

#advise(advice, offset = 0, len = 0) ⇒ nil

Note:

Not implemented by default.

Announce an intention to access data from the current file in a specific pattern.

Parameters:

  • advice (:normal, :sequential, :random, :willneed, :dontneed, :noreuse)

    The advice mode.

  • offset (Integer) (defaults to: 0)

    The offset within the file.

  • len (Integer) (defaults to: 0)

    The length

Returns:

  • (nil)

See Also:



80
81
82
# File 'lib/fake_io.rb', line 80

def advise(advice,offset=0,len=0)
  # no-op
end

#autoclose?true

Note:

For compatibility with IO.

Returns:

  • (true)


901
902
903
# File 'lib/fake_io.rb', line 901

def autoclose?
  @autoclose
end

#binmodeIO

Note:

For compatibility with IO.

Returns:

  • (IO)


877
878
879
880
# File 'lib/fake_io.rb', line 877

def binmode
  @binmode = true
  return self
end

#binmode?Boolean

Note:

For compatibility with IO.

Returns:

  • (Boolean)


887
888
889
# File 'lib/fake_io.rb', line 887

def binmode?
  @binmode == true
end

#bytesObject

Deprecated.

Removed in Ruby 3.0.

Deprecated alias to #each_bytes.



502
503
504
# File 'lib/fake_io.rb', line 502

def bytes
  each_byte
end

#charsObject

Deprecated.

Removed in Ruby 3.0.

Deprecated alias to #each_char.



531
532
533
# File 'lib/fake_io.rb', line 531

def chars
  each_char
end

#closeObject

Closes the IO stream.



1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/fake_io.rb', line 1010

def close
  io_close

  @fd = nil

  @read   = false
  @write  = false
  @closed = true
  return nil
end

#close_on_exec?Boolean

Note:

For compatibility with IO.

Indicates whether the close-on-exec flag is set.

Returns:

  • (Boolean)


917
918
919
# File 'lib/fake_io.rb', line 917

def close_on_exec?
  @close_on_exec
end

#close_readObject

Closes the read end of a duplex IO stream.



978
979
980
981
982
983
984
# File 'lib/fake_io.rb', line 978

def close_read
  if @write then @read = false
  else           close
  end

  return nil
end

#close_writeObject

Closes the write end of a duplex IO stream.



989
990
991
992
993
994
995
# File 'lib/fake_io.rb', line 989

def close_write
  if @read then @write = false
  else          close
  end

  return nil
end

#closed?Boolean

Determines whether the IO stream is closed.

Returns:

  • (Boolean)

    Specifies whether the IO stream has been closed.



1003
1004
1005
# File 'lib/fake_io.rb', line 1003

def closed?
  @closed == true
end

#codepointsObject

Deprecated.

Removed in Ruby 3.0

Deprecated alias to #each_codepoint.



563
564
565
# File 'lib/fake_io.rb', line 563

def codepoints
  each_codepoint
end

#each_byte {|byte| ... } ⇒ Enumerator

Iterates over each byte in the IO stream.

Yields:

  • (byte)

    The given block will be passed each byte in the IO stream.

Yield Parameters:

  • byte (Integer)

    A byte from the IO stream.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.



490
491
492
493
494
# File 'lib/fake_io.rb', line 490

def each_byte(&block)
  return enum_for(__method__) unless block

  each_chunk { |chunk| chunk.each_byte(&block) }
end

#each_char {|char| ... } ⇒ Enumerator

Iterates over each character in the IO stream.

Yields:

  • (char)

    The given block will be passed each character in the IO stream.

Yield Parameters:

  • char (String)

    A character from the IO stream.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.



519
520
521
522
523
# File 'lib/fake_io.rb', line 519

def each_char(&block)
  return enum_for(__method__) unless block

  each_chunk { |chunk| chunk.each_char(&block) }
end

#each_chunk {|block| ... } ⇒ Enumerator

Iterates over each block within the IO stream.

Yields:

  • (block)

    The given block will be passed each block of data from the IO stream.

Yield Parameters:

  • block (String)

    A block of data from the IO stream.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.

Raises:

  • (IOError)

    The stream is closed for reading.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fake_io.rb', line 216

def each_chunk
  return enum_for(__method__) unless block_given?

  unless @read
    raise(IOError,"closed for reading")
  end

  unless io_buffer_empty?
    # read from the buffer first
    chunk = io_buffer_read
    chunk.force_encoding(external_encoding)

    yield chunk
  end

  until @eof
    begin
      # no data currently available, sleep and retry
      until (chunk = io_read)
        sleep(1)
      end
    rescue EOFError
      @eof = true
      break
    end

    chunk.force_encoding(internal_encoding) if internal_encoding
    chunk.force_encoding(external_encoding)

    unless chunk.empty?
      yield chunk
    else
      # short read
      @eof = true
    end
  end
end

#each_codepoint {|ord| ... } ⇒ Enumerator

Note:

Only available on Ruby > 1.9.

Passes the Integer ordinal of each character in the stream.

Yields:

  • (ord)

    The given block will be passed each codepoint.

Yield Parameters:

  • ord (String)

    The ordinal of a character from the stream.

Returns:

  • (Enumerator)

    If no block is given an Enumerator object will be returned.



551
552
553
554
555
# File 'lib/fake_io.rb', line 551

def each_codepoint
  return enum_for(__method__) unless block_given?

  each_char { |c| yield c.ord }
end

#each_line(separator = $/) {|line| ... } ⇒ Enumerator Also known as: each

Iterates over each line in the IO stream.

Yields:

  • (line)

    The given block will be passed each line in the IO stream.

Yield Parameters:

  • line (String)

    A line from the IO stream.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.

See Also:



582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/fake_io.rb', line 582

def each_line(separator=$/)
  return enum_for(__method__,separator) unless block_given?

  loop do
    begin
      line = gets(separator)
    rescue EOFError
      break
    end

    yield line
  end
end

#fcntl(command, argument) ⇒ Object

Raises:

  • (NotImplementedError)

    #fcntl was not implemented in FakeIO.



933
934
935
# File 'lib/fake_io.rb', line 933

def fcntl(command,argument)
  raise(NotImplementedError,"#{self.class}#fcntl was not implemented")
end

#filenoInteger

Note:

For compatibility with IO.

The file descriptor

Returns:

  • (Integer)


1028
1029
1030
# File 'lib/fake_io.rb', line 1028

def fileno
  @fd
end

#flushIO

Note:

For compatibility with IO.

Returns:

  • (IO)


963
964
965
# File 'lib/fake_io.rb', line 963

def flush
  self
end

#fsync0 Also known as: fdatasync

Note:

For compatibility with IO.

Immediately writes all buffered data.

Returns:

  • (0)


944
945
946
947
# File 'lib/fake_io.rb', line 944

def fsync
  flush
  return 0
end

#getbyteInteger

Note:

Only available on Ruby > 1.9.

Reads a byte from the IO stream.

Returns:

  • (Integer)

    A byte from the IO stream.



329
330
331
332
333
# File 'lib/fake_io.rb', line 329

def getbyte
  if (c = read(1))
    c.bytes.first
  end
end

#getcString

Reads a character from the IO stream.

Returns:

  • (String)

    A character from the IO stream.



341
342
343
# File 'lib/fake_io.rb', line 341

def getc
  read(1)
end

#gets(separator = $/) ⇒ String

Reads a string from the IO stream.

Parameters:

  • separator (String) (defaults to: $/)

    The separator character that designates the end of the string being read.

Returns:

  • (String)

    The string from the IO stream.



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/fake_io.rb', line 396

def gets(separator=$/)
  # increment the line number
  @lineno += 1

  # if no separator is given, read everything
  return read if separator.nil?

  line = String.new(encoding: external_encoding)

  while (c = read(1))
    line << c

    break if c == separator # separator reached
  end

  if line.empty?
    # a line should atleast contain the separator
    raise(EOFError,"end of file reached")
  end

  return line
end

#initializeObject

Initializes the IO stream.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fake_io.rb', line 42

def initialize
  @read   = true
  @write  = true

  @closed        = true
  @autoclose     = true
  @close_on_exec = true

  @binmode = false
  @tty     = false

  @external_encoding = Encoding.default_external
  @internal_encoding = Encoding.default_internal

  @sync = false

  open
end

#inspectString

Inspects the IO stream.

Returns:

  • (String)

    The inspected IO stream.



1058
1059
1060
# File 'lib/fake_io.rb', line 1058

def inspect
  "#<#{self.class}: #{@fd.inspect if @fd}>"
end

#io_closeObject (protected)

This method is abstract.

Place holder method used to close the IO stream.



1145
1146
# File 'lib/fake_io.rb', line 1145

def io_close
end

#io_openfd (protected)

This method is abstract.

Place holder method used to open the IO stream.

Returns:

  • (fd)

    The abstract file-descriptor that represents the stream.



1094
1095
# File 'lib/fake_io.rb', line 1094

def io_open
end

#io_readString (protected)

This method is abstract.

Place holder method used to read a block from the IO stream.

Returns:

  • (String)

    Available data to be read.

Raises:

  • (EOFError)

    The end of the stream has been reached.



1125
1126
# File 'lib/fake_io.rb', line 1125

def io_read
end

#io_seek(new_pos, whence) ⇒ Object (protected)

This method is abstract.

Place holder method used to seek to a position within the IO stream.

Parameters:

  • new_pos (Integer)

    The desired new position, relative to whence.

  • whence (File::SEEK_CUR, File::SEEK_DATA, File::SEEK_END, File::SEEK_HOLE, File::SEEK_SET)

Raises:

  • (NotImplementedError)

    By default a NotImplementedError exception will be raised.



1110
1111
1112
# File 'lib/fake_io.rb', line 1110

def io_seek(new_pos,whence)
  raise(NotImplementedError,"#{self.class}#io_seek is not implemented")
end

#io_write(data) ⇒ Object (protected)

This method is abstract.

Place holder method used to write data to the IO stream.

Parameters:

  • data (String)

    The data to write to the IO stream.



1136
1137
1138
# File 'lib/fake_io.rb', line 1136

def io_write(data)
  0
end

#ioctl(command, argument) ⇒ Object

Raises:

  • (NotImplementedError)

    #ioctl was not implemented in FakeIO.



925
926
927
# File 'lib/fake_io.rb', line 925

def ioctl(command,argument)
  raise(NotImplementedError,"#{self.class}#ioctl was not implemented")
end

#isattyBoolean

Note:

For compatibility with IO.

Indicates whether the IO stream is associated with a terminal device.

Returns:

  • (Boolean)


781
782
783
# File 'lib/fake_io.rb', line 781

def isatty
  @tty
end

#linenoInteger

The current line-number (how many times #gets has been called).

Returns:

  • (Integer)

    The current line-number.

Raises:

  • (IOError)

    The stream was not opened for reading.



830
831
832
833
834
835
836
# File 'lib/fake_io.rb', line 830

def lineno
  unless @read
    raise(IOError,"not opened for reading")
  end

  return @lineno
end

#lineno=(number) ⇒ Integer

Manually sets the current line-number.

Parameters:

  • number (Integer)

    The new line-number.

Returns:

  • (Integer)

    The new line-number.

Raises:

  • (IOError)

    The stream was not opened for reading.



850
851
852
853
854
855
856
# File 'lib/fake_io.rb', line 850

def lineno=(number)
  unless @read
    raise(IOError,"not opened for reading")
  end

  return @lineno = number.to_i
end

#lines(*args) ⇒ Object

Deprecated.

Removed in Ruby 3.0.

Deprecated alias to #each_line.



604
605
606
# File 'lib/fake_io.rb', line 604

def lines(*args)
  each_line(*args)
end

#openIO (protected)

Opens the IO stream.

Returns:

  • (IO)

    The opened IO stream.



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/fake_io.rb', line 1070

def open
  @pos    = 0
  @lineno = 0
  @eof    = false

  io_buffer_clear!

  @fd = io_open
  @closed = false
  return self
end

#pread(maxlen, offset, outbuf) ⇒ String

Reads data at a given offset, without changing the current #pos.

Parameters:

  • maxlen (Integer)

    The maximum amount of data to read. If nil is given, the entire IO stream will be read.

  • offset (Integer)

    The offset to read the data at.

  • outbuf (#<<, nil)

    The optional buffer to append the data to.

Returns:

  • (String)

    The data read from the IO stream.

See Also:



665
666
667
668
669
670
671
672
# File 'lib/fake_io.rb', line 665

def pread(maxlen,offset,outbuf)
  old_pos = pos
  seek(offset)

  data = read(maxlen,outbuf)
  seek(old_pos)
  return data
end

Prints data to the IO stream.

Parameters:

  • arguments (Array)

    The data to print to the IO stream.

Returns:

  • (nil)


724
725
726
727
# File 'lib/fake_io.rb', line 724

def print(*arguments)
  arguments.each { |data| write(data) }
  return nil
end

#printf(format_string, *arguments) ⇒ nil

Prints a formatted string to the IO stream.

Parameters:

  • format_string (String)

    The format string to format the data.

  • arguments (Array)

    The data to format.

Returns:

  • (nil)


753
754
755
756
# File 'lib/fake_io.rb', line 753

def printf(format_string,*arguments)
  write(format_string % arguments)
  return nil
end

#putc(data) ⇒ String, Integer

Writes a byte or a character to the IO stream.

Parameters:

  • data (String, Integer)

    The byte or character to write.

Returns:

  • (String, Integer)

    The byte or character that was written.



706
707
708
709
710
711
712
713
714
# File 'lib/fake_io.rb', line 706

def putc(data)
  char = case data
         when String then data.chr
         else             data
         end

  write(char)
  return data
end

#puts(*arguments) ⇒ nil

Prints data with new-line characters to the IO stream.

Parameters:

  • arguments (Array)

    The data to print to the IO stream.

Returns:

  • (nil)


737
738
739
740
# File 'lib/fake_io.rb', line 737

def puts(*arguments)
  arguments.each { |data| write("#{data}#{$/}") }
  return nil
end

#pwrite(string, offset) ⇒ Integer

Writes data to the given offset, without changing the current #pos.

Parameters:

  • data (String)

    The data to write.

  • offset (Integer)

    The offset to write the data to.

Returns:

  • (Integer)

    The number of bytes written.

See Also:



688
689
690
691
692
693
694
695
# File 'lib/fake_io.rb', line 688

def pwrite(string,offset)
  old_pos = pos
  seek(offset)

  bytes_written = write(string)
  seek(old_pos)
  return bytes_written
end

#read(length = nil, buffer = nil) ⇒ String Also known as: sysread, read_nonblock

Reads data from the IO stream.

Parameters:

  • length (Integer, nil) (defaults to: nil)

    The maximum amount of data to read. If nil is given, the entire IO stream will be read.

  • buffer (#<<) (defaults to: nil)

    The optional buffer to append the data to.

Returns:

  • (String)

    The data read from the IO stream.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/fake_io.rb', line 267

def read(length=nil,buffer=nil)
  bytes_remaining = (length || Float::INFINITY)
  result = String.new(encoding: external_encoding)

  each_chunk do |chunk|
    if bytes_remaining < chunk.bytesize
      fragment  = chunk.byteslice(0,bytes_remaining)

      remaining_length = chunk.bytesize - bytes_remaining
      remaining_data   = chunk.byteslice(bytes_remaining,remaining_length)

      result << fragment
      io_buffer_append(remaining_data)
      @pos += bytes_remaining
      break
    else
      result << chunk
      bytes_read = chunk.bytesize
      bytes_remaining -= bytes_read
      @pos += bytes_read
    end

    # no more data to read
    break if bytes_remaining == 0
  end

  unless result.empty?
    buffer << result if buffer
    return result
  end
end

#readbyteInteger

Reads a byte from the IO stream.

Returns:

  • (Integer)

    A byte from the IO stream.

Raises:

  • (EOFError)

    The end-of-file has been reached.



447
448
449
450
451
452
453
# File 'lib/fake_io.rb', line 447

def readbyte
  unless (c = read(1))
    raise(EOFError,"end of file reached")
  end

  return c.bytes.first
end

#readcharInteger

Reads a character from the IO stream.

Returns:

  • (Integer)

    The character from the IO stream.

Raises:

  • (EOFError)

    The end-of-file has been reached.

See Also:



430
431
432
433
434
435
436
# File 'lib/fake_io.rb', line 430

def readchar
  unless (c = getc)
    raise(EOFError,"end of file reached")
  end

  return c
end

#readline(separator = $/) ⇒ String

Reads a line from the IO stream.

Parameters:

  • separator (String) (defaults to: $/)

    The separator character that designates the end of the string being read.

Returns:

  • (String)

    The string from the IO stream.

Raises:

  • (EOFError)

    The end-of-file has been reached.

See Also:



470
471
472
473
474
475
476
# File 'lib/fake_io.rb', line 470

def readline(separator=$/)
  unless (line = gets(separator))
    raise(EOFError,"end of file reached")
  end

  return line
end

#readlines(separator = $/) ⇒ Array<String>

Reads every line from the IO stream.

Returns:

  • (Array<String>)

    The lines in the IO stream.

See Also:



617
618
619
# File 'lib/fake_io.rb', line 617

def readlines(separator=$/)
  enum_for(:each_line,separator).to_a
end

#readpartial(length, buffer = nil) ⇒ String

Reads partial data from the IO stream.

Parameters:

  • length (Integer)

    The maximum amount of data to read.

  • buffer (#<<) (defaults to: nil)

    The optional buffer to append the data to.

Returns:

  • (String)

    The data read from the IO stream.

See Also:



316
317
318
# File 'lib/fake_io.rb', line 316

def readpartial(length,buffer=nil)
  read(length,buffer)
end

#reopen(*arguments) ⇒ Object

Raises:

  • (NotImplementedError)

    #reopen is not implemented.



971
972
973
# File 'lib/fake_io.rb', line 971

def reopen(*arguments)
  raise(NotImplementedError,"#{self.class}#reopen is not implemented")
end

#rewindIO

Note:

For compatibility with IO.

Returns:

  • (IO)

See Also:



865
866
867
868
869
870
# File 'lib/fake_io.rb', line 865

def rewind
  seek(0,SEEK_SET)

  @pos    = 0
  @lineno = 0
end

#seek(new_pos, whence = SEEK_SET) ⇒ 0

Parameters:

  • new_pos (Integer)

    The desired new position, relative to whence.

  • whence (File::SEEK_CUR, File::SEEK_DATA, File::SEEK_END, File::SEEK_HOLE, File::SEEK_SET) (defaults to: SEEK_SET)

Returns:

  • (0)


800
801
802
803
804
# File 'lib/fake_io.rb', line 800

def seek(new_pos,whence=SEEK_SET)
  io_seek(new_pos,whence)
  io_buffer_clear!
  return 0
end

#set_encoding(encoding) ⇒ Object #set_encoding(ext_inc, int_enc) ⇒ Object

Sets the encoding.

Overloads:

  • #set_encoding(encoding) ⇒ Object

    Parameters:

    • encoding (String)

      The encoding string, which can be the encoding name or the external and internal encoding names separated by a : or , character.

  • #set_encoding(ext_inc, int_enc) ⇒ Object

    Parameters:

    • ext_inc (Encoding)

      The new external encoding.

    • int_enc (Encoding)

      The new internal encoding.

Parameters:

  • arguments (Array)

Raises:

  • (TypeError)

    The given argument was not a String or Encoding object.

  • (ArgumentError)

    Either no arguments were given or more than three arguments were given.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fake_io.rb', line 107

def set_encoding(*arguments)
  if arguments.length == 1
    case arguments[0]
    when String
      string = arguments[0]

      if string.include?(':') || string.include?(',')
        ext_enc, int_enc = string.split(/[:,]\s*/,2)

        self.external_encoding = Encoding.find(ext_enc)
        self.internal_encoding = Encoding.find(int_enc)
      else
        self.external_encoding = Encoding.find(string)
      end
    when Encoding
      self.external_encoding = arguments[0]
    else
      raise(TypeError,"argument must be a String or Encoding object")
    end
  elsif arguments.length == 2 || arguments.length == 3
    self.external_encoding = arguments[0]
    self.internal_encoding = arguments[1]
  else
    raise(ArgumentError,"wrong number of arguments (given #{arguments.length}, expected 1..3)")
  end
end

#set_encoding_by_bomObject

Sets the #external_encoding based on the Byte Order Mark (BOM) bytes at the beginning of the file.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fake_io.rb', line 138

def set_encoding_by_bom
  case (b1 = getbyte)
  when 0x00 # possible UTF-32 (BE) 
    b2 = getbyte
    b3 = getbyte
    b4 = getbyte

    if (b2 == 0x00) && (b3 == 0xFE) && (b4 == 0xFF)
      # UTF-32 (BE) BOM (0x00 0x00 0xFE 0xFF) detected
      self.external_encoding = Encoding::UTF_32BE
    else
      ungetbyte(b4) if b4
      ungetbyte(b3) if b3
      ungetbyte(b2) if b2
      ungetbyte(b1) if b1
    end
  when 0x28 # possible UTF-7
    b2 = getbyte
    b3 = getbyte

    if (b2 == 0x2F) && (b3 == 0x76)
      # UTF-7 BOM (0x28 0x2F 0x76) detected
      self.external_encoding = Encoding::UTF_7
    else
      ungetbyte(b3) if b3
      ungetbyte(b2) if b2
      ungetbyte(b1) if b1
    end
  when 0xEF # possible UTF-8
    b2 = getbyte
    b3 = getbyte

    if (b2 == 0xBB) && (b3 == 0xBF)
      self.external_encoding = Encoding::UTF_8
    else
      ungetbyte(b3) if b3
      ungetbyte(b2) if b2
      ungetbyte(b1) if b1
    end
  when 0xFE # possible UTF-16 (BE)
    b2 = getbyte

    if (b2 == 0xFF)
      self.external_encoding = Encoding::UTF_16BE
    else
      ungetbyte(b2) if b2
      ungetbyte(b1) if b1
    end
  when 0xFF # possible UTF-16 (LE) or UTF-32 (LE)
    b2 = getbyte

    if (b2 == 0xFE)
      self.external_encoding = Encoding::UTF_16LE
    else
      ungetbyte(b2) if b2
      ungetbyte(b1) if b1
    end
  else
    ungetbyte(b1) if b1
  end
end

#statObject

Raises:

  • (NotImplementedError)

    #stat is not implemented.



770
771
772
# File 'lib/fake_io.rb', line 770

def stat
  raise(NotImplementedError,"#{self.class}#stat is not implemented")
end

#sysseek(offset, whence = SEEK_SET) ⇒ Object

See Also:



809
810
811
# File 'lib/fake_io.rb', line 809

def sysseek(offset,whence=SEEK_SET)
  seek(new_pos,whence)
end

#to_iInteger

Note:

For compatibility with IO.

The file descriptor

Returns:

  • (Integer)


1039
1040
1041
# File 'lib/fake_io.rb', line 1039

def to_i
  @fd
end

#to_ioIO

Note:

For compatibility with IO.

Returns:

  • (IO)


1048
1049
1050
# File 'lib/fake_io.rb', line 1048

def to_io
  self
end

#tty?Boolean

Returns:

  • (Boolean)

See Also:



788
789
790
# File 'lib/fake_io.rb', line 788

def tty?
  @tty
end

#ungetbyte(byte) ⇒ nil

Note:

Only available on Ruby > 1.9.

Un-reads a byte from the IO stream, append it to the read buffer.

Parameters:

  • byte (Integer, String)

    The byte to un-read.

Returns:

  • (nil)

    The byte was appended to the read buffer.



357
358
359
360
361
362
363
364
365
366
# File 'lib/fake_io.rb', line 357

def ungetbyte(byte)
  char = case byte
         when Integer then byte.chr
         else              byte.to_s
         end

  io_buffer_prepend(char)
  @pos -= char.bytesize
  return nil
end

#ungetc(char) ⇒ nil

Un-reads a character from the IO stream, append it to the read buffer.

Parameters:

  • char (#to_s)

    The character to un-read.

Returns:

  • (nil)

    The character was appended to the read buffer.



378
379
380
381
382
383
384
# File 'lib/fake_io.rb', line 378

def ungetc(char)
  char = char.to_s

  io_buffer_prepend(char)
  @pos -= char.bytesize
  return nil
end

#write(data) ⇒ Integer Also known as: syswrite, write_nonblock, <<

Writes data to the IO stream.

Parameters:

  • data (String)

    The data to write.

Returns:

  • (Integer)

    The number of bytes written.

Raises:

  • (IOError)

    The stream is closed for writing.



633
634
635
636
637
638
639
640
641
642
# File 'lib/fake_io.rb', line 633

def write(data)
  unless @write
    raise(IOError,"closed for writing")
  end

  data = data.to_s
  data.force_encoding(internal_encoding) if internal_encoding

  io_write(data)
end