Permalink
Browse files

Merge pull request #1513 from ratnikov/null-file-resource

Introduce EmptyfileResource that should replace the JRubyNotFoundFile.NOT_FOUND
  • Loading branch information...
2 parents d8860e1 + 2b7b417 commit 2fca72c7833d6175786737fa9b3f75d1c75edb6d @enebo enebo committed Mar 4, 2014
View
85 core/src/main/java/org/jruby/util/EmptyFileResource.java
@@ -0,0 +1,85 @@
+package org.jruby.util;
+
+import jnr.posix.FileStat;
+import jnr.posix.POSIX;
+import org.jruby.exceptions.RaiseException;
+import org.jruby.runtime.ThreadContext;
+
+class EmptyFileResource implements FileResource {
+ // All empty resources are the same and immutable, so may as well
+ // cache the instance
+ private static final EmptyFileResource INSTANCE = new EmptyFileResource();
+
+ public static EmptyFileResource create(String pathname) {
+ return (pathname == null || "".equals(pathname)) ?
+ INSTANCE : null;
+ }
+
+ @Override
+ public String absolutePath() {
+ return "";
+ }
+
+ @Override
+ public boolean exists() {
+ return false;
+ }
+
+ @Override
+ public boolean isDirectory() {
+ return false;
+ }
+
+ @Override
+ public boolean isFile() {
+ return false;
+ }
+
+ @Override
+ public boolean canRead() {
+ return false;
+ }
+
+ @Override
+ public boolean canWrite() {
+ return false;
+ }
+
+ @Override
+ public boolean isSymLink() {
+ return false;
+ }
+
+ @Override
+ public String[] list() {
+ return new String[0];
+ }
+
+ @Override
+ public long lastModified() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public long length() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FileStat stat(POSIX posix) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FileStat lstat(POSIX posix) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public JRubyFile hackyGetJRubyFile() {
+ // It is somewhat weird that we're returning the NOT_EXIST instance that this resource is
+ // intending to replace. However, that should go away once we get rid of the hacky method, so
+ // should be okay for now.
+ return JRubyNonExistentFile.NOT_EXIST;
+ }
+}
View
9 core/src/main/java/org/jruby/util/JRubyFile.java
@@ -67,12 +67,11 @@ public static FileResource createResource(Ruby runtime, String pathname) {
}
public static FileResource createResource(String cwd, String pathname) {
- if (pathname == null) {
- // null pathnames do not exist
- return new RegularFileResource(JRubyNonExistentFile.NOT_EXIST);
+ FileResource emptyResource = EmptyFileResource.create(pathname);
+ if (emptyResource != null) {
+ return emptyResource;
}
- // Try as a jar resource first
FileResource jarResource = JarResource.create(pathname);
if (jarResource != null) {
return jarResource;
@@ -81,6 +80,8 @@ public static FileResource createResource(String cwd, String pathname) {
if (pathname.startsWith("file:")) {
pathname = pathname.substring(5);
}
+
+ // If any other special resource types fail, count it as a filesystem backed resource.
return new RegularFileResource(create(cwd, pathname));
}
View
3 core/src/main/java/org/jruby/util/JRubyNonExistentFile.java
@@ -37,6 +37,7 @@
/**
* @author nicksieger
*/
+@Deprecated // Replaced now with EmptyFileResource
public class JRubyNonExistentFile extends JRubyFile {
static final JRubyNonExistentFile NOT_EXIST = new JRubyNonExistentFile();
private JRubyNonExistentFile() {
@@ -124,4 +125,4 @@ public static File createTempFile(String prefix, String suffix) throws IOExcepti
public File[] listFiles(final FilenameFilter filter) {
return new File[0];
}
-}
+}

0 comments on commit 2fca72c

Please sign in to comment.