Permalink
Browse files

Merge pull request #78 from matthewd/java-native

Use native ruby IO objects on JRuby too
  • Loading branch information...
2 parents 1c7bf20 + 9586290 commit 08fe2db94524829891997ed0572a1ac3cbf607d5 @matthewd matthewd committed on GitHub Aug 28, 2017
Showing with 27 additions and 76 deletions.
  1. +6 −41 lib/rb-inotify/notifier.rb
  2. +21 −35 spec/notifier_spec.rb
View
@@ -40,12 +40,6 @@ class Notifier
# @return [Fixnum]
attr_reader :fd
- # @return [Boolean] Whether or not this Ruby implementation supports
- # wrapping the native file descriptor in a Ruby IO wrapper.
- def self.supports_ruby_io?
- RUBY_PLATFORM !~ /java/
- end
-
# Creates a new {Notifier}.
#
# @return [Notifier]
@@ -82,9 +76,6 @@ def initialize
# @return [IO] An IO object wrapping the file descriptor
# @raise [NotImplementedError] if this is being called in JRuby
def to_io
- unless self.class.supports_ruby_io?
- raise NotImplementedError.new("INotify::Notifier#to_io is not supported under JRuby")
- end
@io ||= IO.new(@fd)
end
@@ -302,38 +293,12 @@ def read_events
# Same as IO#readpartial, or as close as we need.
def readpartial(size)
- # Use Ruby's readpartial if possible, to avoid blocking other threads.
- begin
- return to_io.readpartial(size) if self.class.supports_ruby_io?
- rescue Errno::EBADF, IOError
- # If the IO has already been closed, reading from it will cause
- # Errno::EBADF. In JRuby it can raise IOError with invalid or
- # closed file descriptor.
- return nil
- rescue IOError => ex
- return nil if ex.message =~ /stream closed/
- raise
- end
-
- tries = 0
- begin
- tries += 1
- buffer = FFI::MemoryPointer.new(:char, size)
- size_read = Native.read(fd, buffer, size)
- return buffer.read_string(size_read) if size_read >= 0
- end while FFI.errno == Errno::EINTR::Errno && tries <= 5
-
- raise SystemCallError.new("Error reading inotify events" +
- case FFI.errno
- when Errno::EAGAIN::Errno; ": no data available for non-blocking I/O"
- when Errno::EBADF::Errno; ": invalid or closed file descriptor"
- when Errno::EFAULT::Errno; ": invalid buffer"
- when Errno::EINVAL::Errno; ": invalid file descriptor"
- when Errno::EIO::Errno; ": I/O error"
- when Errno::EISDIR::Errno; ": file descriptor is a directory"
- else; ""
- end,
- FFI.errno)
+ to_io.readpartial(size)
+ rescue Errno::EBADF, IOError
+ # If the IO has already been closed, reading from it will cause
+ # Errno::EBADF. In JRuby it can raise IOError with invalid or
+ # closed file descriptor.
+ nil
end
end
end
View
@@ -111,48 +111,34 @@
end
end
- if INotify::Notifier.supports_ruby_io?
- describe :fd do
- it "returns an integer" do
- expect(@notifier.fd).to be_an(Integer)
- end
+ describe :fd do
+ it "returns an integer" do
+ expect(@notifier.fd).to be_an(Integer)
end
+ end
- describe :to_io do
- it "returns a ruby IO" do
- expect(@notifier.to_io).to be_an(::IO)
- end
-
- it "matches the fd" do
- expect(@notifier.to_io.fileno).to eq(@notifier.fd)
- end
+ describe :to_io do
+ it "returns a ruby IO" do
+ expect(@notifier.to_io).to be_an(::IO)
+ end
- it "caches its result" do
- expect(@notifier.to_io).to be(@notifier.to_io)
- end
+ it "matches the fd" do
+ expect(@notifier.to_io.fileno).to eq(@notifier.fd)
+ end
- it "is selectable" do
- events = recording(dir, :create)
- expect(select([@notifier.to_io], nil, nil, 0.2)).to be_nil
+ it "caches its result" do
+ expect(@notifier.to_io).to be(@notifier.to_io)
+ end
- dir.join("test.txt").write("hello world")
- expect(select([@notifier.to_io], nil, nil, 0.2)).to eq([[@notifier.to_io], [], []])
+ it "is selectable" do
+ events = recording(dir, :create)
+ expect(select([@notifier.to_io], nil, nil, 0.2)).to be_nil
- @notifier.process
- expect(select([@notifier.to_io], nil, nil, 0.2)).to be_nil
- end
- end
- else
- describe :fd do
- it "returns an integer" do
- expect(@notifier.fd).to be_an(Integer)
- end
- end
+ dir.join("test.txt").write("hello world")
+ expect(select([@notifier.to_io], nil, nil, 0.2)).to eq([[@notifier.to_io], [], []])
- describe :to_io do
- it "raises" do
- expect { @notifier.to_io }.to raise_exception(NotImplementedError)
- end
+ @notifier.process
+ expect(select([@notifier.to_io], nil, nil, 0.2)).to be_nil
end
end

0 comments on commit 08fe2db

Please sign in to comment.