Skip to content

Commit

Permalink
Showing 10 changed files with 69 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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;
@@ -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);
}

26 changes: 26 additions & 0 deletions core/src/main/java/org/jruby/util/AbstractFileResource.java
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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:/";

@@ -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
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/util/EmptyFileResource.java
Original file line number Diff line number Diff line change
@@ -91,8 +91,8 @@ public JRubyFile hackyGetJRubyFile() {
}

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

@Override
20 changes: 14 additions & 6 deletions core/src/main/java/org/jruby/util/FileResource.java
Original file line number Diff line number Diff line change
@@ -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}.
@@ -38,9 +39,16 @@ 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 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. 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
Original file line number Diff line number Diff line change
@@ -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;

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

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

@Override
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/util/JarFileResource.java
Original file line number Diff line number Diff line change
@@ -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;
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 3 additions & 7 deletions core/src/main/java/org/jruby/util/RegularFileResource.java
Original file line number Diff line number Diff line change
@@ -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;

@@ -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
25 changes: 8 additions & 17 deletions core/src/main/java/org/jruby/util/URLResource.java
Original file line number Diff line number Diff line change
@@ -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:/";
@@ -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) {

0 comments on commit 57c2513

Please sign in to comment.