Skip to content

Instantly share code, notes, and snippets.

@edwinf
Last active December 21, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwinf/6329520 to your computer and use it in GitHub Desktop.
Save edwinf/6329520 to your computer and use it in GitHub Desktop.
Java extension to the jruby runtime to open a file with the new Java.nio classes that open a file with read | write | delete share permissions.
/**
* Created with IntelliJ IDEA.
* User: efrey
* Date: 6/11/13
* Time: 11:00 AM
* To change this template use File | Settings | File Templates.
*
* http://bugs.sun.com/view_bug.do?bug_id=6357433
*
*
*/
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.util.io.ChannelDescriptor;
import org.jruby.util.io.ChannelStream;
import org.jruby.util.io.InvalidValueException;
import org.jruby.util.io.Stream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.Iterator;
@JRubyClass(name="FileExt", parent="File", include="FileTest")
public class RubyFileExt extends RubyObject {
public RubyFileExt(Ruby runtime, RubyClass metaClass) {
super(runtime, metaClass);
}
public RubyFileExt(RubyClass metaClass) {
super(metaClass);
}
@JRubyMethod
public static RubyIO getRubyFile(String path) throws IOException, InvalidValueException{
Path p = FileSystems.getDefault().getPath(path);
OpenOption[] options = new OpenOption[1];
options[0] = StandardOpenOption.READ;
Channel channel = FileChannel.open(p, options);
return new RubyIO(Ruby.getGlobalRuntime(), channel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment