Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/torquebox/vfs/ext/io.rb

Class Method Summary collapse

Class Method Details

.read(name, *options) ⇒ Object

1.8: IO.read( portname, <length=$/ <,offset>> ) 1.9: IO.read( portname, <length=$/ <,offset>> <,options-for-open> )



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/torquebox/vfs/ext/io.rb', line 128

def read(name, *options)
  length, offset = options unless options.first.is_a?( Hash ) #1.9
  
  if ::File.exist_without_vfs?( name )
    read_without_vfs( name, *options )
  else
    # FIXME: we're ignoring the 1.9 options (encoding, mode, open_args)
    vfs_open( name ) do |f|
      f.seek( offset ) if offset
      f.read( length )
    end
  end
end

.read_without_vfsObject

alias_method :open_without_vfs, :open



25
# File 'lib/torquebox/vfs/ext/io.rb', line 25

alias_method :read_without_vfs, :read

.readlines(name, *options) ⇒ Object

1.8: IO.readlines( portname, separator=$/ ) 1.9: IO.readlines( portname, separator=$/ <, options-for-open> )

IO.readlines( portname, limit <, options-for-open> )
IO.readlines( portname, separator, limit <, options-for-open> )


147
148
149
150
151
152
153
154
155
# File 'lib/torquebox/vfs/ext/io.rb', line 147

def readlines(name, *options)
  if ::File.exist_without_vfs?( name )
    readlines_without_vfs( name, *options )
  else
    opts = options.pop if options.last.is_a?( Hash ) #1.9
    # FIXME: we're ignoring the 1.9 options (encoding, mode, open_args)
    vfs_open( name ).readlines( *options )
  end
end

.readlines_without_vfsObject



26
# File 'lib/torquebox/vfs/ext/io.rb', line 26

alias_method :readlines_without_vfs, :readlines

.vfs_open(fd, mode_str = 'r', &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/torquebox/vfs/ext/io.rb', line 28

def vfs_open(fd,mode_str='r', &block)
  append   = false
  truncate = false
  write    = false
  read     = false
  create   = false
  case ( mode_str )
    when /r/
      read   = true
      write  = false
      append = false
      create = false
    when /r\+/
      read   = true
      write  = true
      create = false
      append = false
    when /w/
      read     = false
      write    = true
      create   = true
      append   = false
      truncate = true
    when /w\+/
      read   = false
      write  = true
      create = true
      append = false
      truncate = true
    when /a/
      read   = false
      write  = true
      create = true
      append = true
    when /a\+/
      read   = true
      write  = true
      create = true
    when Fixnum
      if ( mode_str & File::RDONLY != 0 )
        read  = true
        write = false
      end
      if ( mode_str & File::WRONLY != 0  )
        read  = false
        write = true
      end
      if ( mode_str & File::RDWR  != 0)
        read  = true
        write = true
      end
      if ( mode_str & File::APPEND  != 0)
        append = true
      end
      if ( mode_str & File::TRUNC  != 0)
        append = false
        truncate = true
      end
      if ( mode_str & File::CREAT  != 0)
        create = true
      end
  end

  # VFS doesn't correctly handle relative paths when
  # retrieving the physical file so expand it
  fd = File.expand_path( fd )
  virtual_file = org.jboss.vfs.VFS.child( fd )

  if ( ! create && ! virtual_file.exists )
    raise Errno::ENOENT
  end

  if ( read && ! write )
    java_in = virtual_file.open_stream()
    ruby_io = java_in.to_io
  elsif ( write && ! read )
    physical_file = virtual_file.physical_file
    java_out = java.io::FileOutputStream.new( physical_file, append )
    ruby_io = java_out.to_io
  elsif ( read && write )
    raise Error.new( "Random-access on VFS not supported" )
  end

  file_io = TorqueBox::VFS::Ext::VirtualFile.new( ruby_io, fd )

  if ( block )
    begin
      block.call( file_io )
    ensure
      file_io.close
    end
  else
    return file_io
  end

end