Skip to content

Commit

Permalink
Merge pull request #1513 from ratnikov/null-file-resource
Browse files Browse the repository at this point in the history
Introduce EmptyfileResource that should replace the JRubyNotFoundFile.NOT_FOUND
  • Loading branch information
enebo committed Mar 4, 2014
2 parents d8860e1 + 2b7b417 commit 2fca72c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
85 changes: 85 additions & 0 deletions 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;
}
}
9 changes: 5 additions & 4 deletions core/src/main/java/org/jruby/util/JRubyFile.java
Expand Up @@ -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;
Expand All @@ -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));
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/util/JRubyNonExistentFile.java
Expand Up @@ -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() {
Expand Down Expand Up @@ -124,4 +125,4 @@ public File[] listFiles(final FileFilter filter) {
public File[] listFiles(final FilenameFilter filter) {
return new File[0];
}
}
}

0 comments on commit 2fca72c

Please sign in to comment.