Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor FileResource.inputStream to be easier to use
for any client code which needs to convert a uri-like path to an inputstream
the way to go is ```JRubyFile.create(runtime, path).inputStream()```
  • Loading branch information
mkristian committed Dec 3, 2014
1 parent 83044c2 commit 36e2ed4
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 46 deletions.
@@ -1,5 +1,6 @@
package org.jruby.runtime.load;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -235,8 +236,11 @@ public ResourceLibrary(String searchName, String scriptName, FileResource resour

@Override
public void load(Ruby runtime, boolean wrap) {
InputStream is = resource.openInputStream();
if (is == null) {
InputStream is = null;
try {
is = new BufferedInputStream(resource.inputStream(), 32768);
}
catch(IOException e) {
throw runtime.newLoadError("no such file to load -- " + searchName, searchName);
}

Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/org/jruby/util/AbstractFileResource.java
@@ -0,0 +1,26 @@
package org.jruby.util;

import java.io.IOException;
import java.io.InputStream;

abstract class AbstractFileResource implements FileResource {

@Override
public InputStream inputStream() throws ResourceException {
if (!exists()) {
throw new ResourceException.NotFound(absolutePath());
}
if (isDirectory()) {
throw new ResourceException.FileIsDirectory(absolutePath());
}
try {
return openInputStream();
}
catch (IOException e) {
throw new ResourceException.IOError(e);
}
}

abstract InputStream openInputStream() throws IOException;

}
10 changes: 3 additions & 7 deletions core/src/main/java/org/jruby/util/ClasspathResource.java
Expand Up @@ -10,7 +10,7 @@
import org.jruby.util.io.ChannelDescriptor;
import org.jruby.util.io.ModeFlags;

public class ClasspathResource implements FileResource {
public class ClasspathResource extends AbstractFileResource {

public static final String CLASSPATH = "classpath:/";

Expand Down Expand Up @@ -137,12 +137,8 @@ public JRubyFile hackyGetJRubyFile() {
}

@Override
public InputStream openInputStream() {
try {
return getResourceURL(uri).openStream();
} catch (IOException ioE) {
return null;
}
InputStream openInputStream() throws IOException {
return getResourceURL(uri).openStream();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/util/EmptyFileResource.java
Expand Up @@ -91,8 +91,8 @@ public JRubyFile hackyGetJRubyFile() {
}

@Override
public InputStream openInputStream() {
return null;
public InputStream inputStream() throws ResourceException {
throw new ResourceException.NotFound("");
}

@Override
Expand Down
19 changes: 13 additions & 6 deletions core/src/main/java/org/jruby/util/FileResource.java
@@ -1,10 +1,11 @@
package org.jruby.util;

import java.io.InputStream;

import jnr.posix.FileStat;
import jnr.posix.POSIX;

import org.jruby.util.io.ChannelDescriptor;
import org.jruby.util.io.ModeFlags;
import java.io.InputStream;

/**
* This is a shared interface for files loaded as {@link java.io.File} and {@link java.util.zip.ZipEntry}.
Expand Down Expand Up @@ -38,9 +39,15 @@ public interface FileResource {
// otherwise.
JRubyFile hackyGetJRubyFile();

// Opens a new input stream to read the contents of a resource and returns it.
// Note that implementations may be allocating native memory for the stream, so
// callers need to close this when they are done with it.
InputStream openInputStream();

/**
* opens input-stream to the underlying resource. this is place where
* the input-stream gets opened. users of this method should follow the pattern:
* close the stream where you open it.
*
* @return just opened InputStream
* @throws ResourceException is the file does not exists or if the resource is a directory
*/
InputStream inputStream() throws ResourceException;
ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws ResourceException;
}
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/util/JarDirectoryResource.java
Expand Up @@ -4,6 +4,7 @@
import org.jruby.Ruby;
import org.jruby.util.io.ChannelDescriptor;
import org.jruby.util.io.ModeFlags;
import java.io.IOException;
import java.io.InputStream;

/**
Expand Down Expand Up @@ -61,8 +62,8 @@ public boolean isRoot() {
}

@Override
public InputStream openInputStream() {
return null;
InputStream openInputStream() throws IOException {
throw new ResourceException.FileIsDirectory(path);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/util/JarFileResource.java
Expand Up @@ -5,6 +5,7 @@
import org.jruby.util.io.ChannelDescriptor;
import org.jruby.util.io.ModeFlags;

import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -60,12 +61,12 @@ public String[] list() {
}

@Override
public InputStream openInputStream() {
InputStream openInputStream() {
return index.getInputStream(entry);
}

@Override
public ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws ResourceException {
return new ChannelDescriptor(Channels.newChannel(openInputStream()), flags);
return new ChannelDescriptor(Channels.newChannel(inputStream()), flags);
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/util/JarResource.java
Expand Up @@ -9,7 +9,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

abstract class JarResource implements FileResource {
abstract class JarResource extends AbstractFileResource {
private static Pattern PREFIX_MATCH = Pattern.compile("^(?:jar:)?(?:file:)?(.*)$");

private static final JarCache jarCache = new JarCache();
Expand Down
10 changes: 3 additions & 7 deletions core/src/main/java/org/jruby/util/RegularFileResource.java
Expand Up @@ -20,7 +20,7 @@
/**
* Represents a "regular" file, backed by regular file system.
*/
class RegularFileResource implements FileResource {
class RegularFileResource extends AbstractFileResource {
private final JRubyFile file;
private final POSIX posix;

Expand Down Expand Up @@ -136,12 +136,8 @@ public JRubyFile hackyGetJRubyFile() {
}

@Override
public InputStream openInputStream() {
try {
return new java.io.BufferedInputStream(new FileInputStream(file), 32768);
} catch (FileNotFoundException fnfe) {
return null;
}
InputStream openInputStream() throws IOException {
return new FileInputStream(file);
}

@Override
Expand Down
25 changes: 8 additions & 17 deletions core/src/main/java/org/jruby/util/URLResource.java
Expand Up @@ -15,9 +15,11 @@
import jnr.posix.FileStat;

import org.jruby.util.io.ChannelDescriptor;
import org.jruby.Ruby;
import org.jruby.exceptions.RaiseException;
import org.jruby.util.io.ModeFlags;

public class URLResource implements FileResource {
public class URLResource extends AbstractFileResource {

public static String URI = "uri:";
public static String CLASSLOADER = "classloader:/";
Expand Down Expand Up @@ -130,27 +132,16 @@ public JRubyFile hackyGetJRubyFile() {
}

@Override
public InputStream openInputStream()
{
try
{
if (pathname != null) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(pathname);
}
return url.openStream();
}
catch (IOException e)
{
return null;
InputStream openInputStream() throws IOException {
if (pathname != null) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(pathname);
}
return url.openStream();
}

@Override
public ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws ResourceException {
if (!exists()) {
throw new ResourceException.NotFound(absolutePath());
}
return new ChannelDescriptor(openInputStream(), flags);
return new ChannelDescriptor(inputStream(), flags);
}

public static FileResource createClassloaderURI(String pathname) {
Expand Down

0 comments on commit 36e2ed4

Please sign in to comment.