Skip to content

Commit 18da34f

Browse files
committed
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()```
1 parent 76a514e commit 18da34f

File tree

9 files changed

+66
-44
lines changed

9 files changed

+66
-44
lines changed

core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jruby.runtime.load;
22

3+
import java.io.BufferedInputStream;
34
import java.io.File;
45
import java.io.IOException;
56
import java.io.InputStream;
@@ -235,8 +236,11 @@ public ResourceLibrary(String searchName, String scriptName, FileResource resour
235236

236237
@Override
237238
public void load(Ruby runtime, boolean wrap) {
238-
InputStream is = resource.openInputStream();
239-
if (is == null) {
239+
InputStream is = null;
240+
try {
241+
is = new BufferedInputStream(resource.inputStream(), 32768);
242+
}
243+
catch(IOException e) {
240244
throw runtime.newLoadError("no such file to load -- " + searchName, searchName);
241245
}
242246

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jruby.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
abstract class AbstractFileResource implements FileResource {
7+
8+
@Override
9+
public InputStream inputStream() throws ResourceException {
10+
if (!exists()) {
11+
throw new ResourceException.NotFound(absolutePath());
12+
}
13+
if (!isDirectory()) {
14+
throw new ResourceException.FileIsDirectory(absolutePath());
15+
}
16+
try {
17+
return openInputStream();
18+
}
19+
catch (IOException e) {
20+
throw new ResourceException.IOError(e);
21+
}
22+
}
23+
24+
abstract InputStream openInputStream() throws IOException;
25+
26+
}

core/src/main/java/org/jruby/util/ClasspathResource.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.jruby.util.io.ChannelDescriptor;
1111
import org.jruby.util.io.ModeFlags;
1212

13-
public class ClasspathResource implements FileResource {
13+
public class ClasspathResource extends AbstractFileResource {
1414

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

@@ -137,12 +137,8 @@ public JRubyFile hackyGetJRubyFile() {
137137
}
138138

139139
@Override
140-
public InputStream openInputStream() {
141-
try {
142-
return getResourceURL(uri).openStream();
143-
} catch (IOException ioE) {
144-
return null;
145-
}
140+
InputStream openInputStream() throws IOException {
141+
return getResourceURL(uri).openStream();
146142
}
147143

148144
@Override

core/src/main/java/org/jruby/util/FileResource.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package org.jruby.util;
22

3+
import java.io.InputStream;
4+
35
import jnr.posix.FileStat;
4-
import jnr.posix.POSIX;
6+
57
import org.jruby.util.io.ChannelDescriptor;
68
import org.jruby.util.io.ModeFlags;
7-
import java.io.InputStream;
89

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

41-
// Opens a new input stream to read the contents of a resource and returns it.
42-
// Note that implementations may be allocating native memory for the stream, so
43-
// callers need to close this when they are done with it.
44-
InputStream openInputStream();
42+
43+
/**
44+
* opens input-stream to the underlying resource. this is place where
45+
* the input-stream gets opened. users of this method should follow the pattern:
46+
* close the stream where you open it.
47+
*
48+
* @return just opened InputStream
49+
* @throws ResourceException is the file does not exists or if the resource is a directory
50+
*/
51+
InputStream inputStream() throws ResourceException;
4552
ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws ResourceException;
4653
}

core/src/main/java/org/jruby/util/JarDirectoryResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jruby.Ruby;
55
import org.jruby.util.io.ChannelDescriptor;
66
import org.jruby.util.io.ModeFlags;
7+
import java.io.IOException;
78
import java.io.InputStream;
89

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

6364
@Override
64-
public InputStream openInputStream() {
65-
return null;
65+
InputStream openInputStream() throws IOException {
66+
throw new ResourceException.FileIsDirectory(path);
6667
}
6768

6869
@Override

core/src/main/java/org/jruby/util/JarFileResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.jruby.util.io.ChannelDescriptor;
66
import org.jruby.util.io.ModeFlags;
77

8+
import java.io.IOException;
89
import java.io.InputStream;
910
import java.nio.channels.Channels;
1011
import java.util.jar.JarEntry;
@@ -60,12 +61,12 @@ public String[] list() {
6061
}
6162

6263
@Override
63-
public InputStream openInputStream() {
64+
InputStream openInputStream() {
6465
return index.getInputStream(entry);
6566
}
6667

6768
@Override
6869
public ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws ResourceException {
69-
return new ChannelDescriptor(Channels.newChannel(openInputStream()), flags);
70+
return new ChannelDescriptor(Channels.newChannel(inputStream()), flags);
7071
}
7172
}

core/src/main/java/org/jruby/util/JarResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.regex.Matcher;
1010
import java.util.regex.Pattern;
1111

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

1515
private static final JarCache jarCache = new JarCache();

core/src/main/java/org/jruby/util/RegularFileResource.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Represents a "regular" file, backed by regular file system.
2222
*/
23-
class RegularFileResource implements FileResource {
23+
class RegularFileResource extends AbstractFileResource {
2424
private final JRubyFile file;
2525
private final POSIX posix;
2626

@@ -136,12 +136,8 @@ public JRubyFile hackyGetJRubyFile() {
136136
}
137137

138138
@Override
139-
public InputStream openInputStream() {
140-
try {
141-
return new java.io.BufferedInputStream(new FileInputStream(file), 32768);
142-
} catch (FileNotFoundException fnfe) {
143-
return null;
144-
}
139+
InputStream openInputStream() throws IOException {
140+
return new FileInputStream(file);
145141
}
146142

147143
@Override

core/src/main/java/org/jruby/util/URLResource.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import jnr.posix.FileStat;
1616

1717
import org.jruby.util.io.ChannelDescriptor;
18+
import org.jruby.Ruby;
19+
import org.jruby.exceptions.RaiseException;
1820
import org.jruby.util.io.ModeFlags;
1921

20-
public class URLResource implements FileResource {
22+
public class URLResource extends AbstractFileResource {
2123

2224
public static String URI = "uri:";
2325
public static String CLASSLOADER = "classloader:/";
@@ -130,27 +132,16 @@ public JRubyFile hackyGetJRubyFile() {
130132
}
131133

132134
@Override
133-
public InputStream openInputStream()
134-
{
135-
try
136-
{
137-
if (pathname != null) {
138-
return Thread.currentThread().getContextClassLoader().getResourceAsStream(pathname);
139-
}
140-
return url.openStream();
141-
}
142-
catch (IOException e)
143-
{
144-
return null;
135+
InputStream openInputStream() throws IOException {
136+
if (pathname != null) {
137+
return Thread.currentThread().getContextClassLoader().getResourceAsStream(pathname);
145138
}
139+
return url.openStream();
146140
}
147141

148142
@Override
149143
public ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws ResourceException {
150-
if (!exists()) {
151-
throw new ResourceException.NotFound(absolutePath());
152-
}
153-
return new ChannelDescriptor(openInputStream(), flags);
144+
return new ChannelDescriptor(inputStream(), flags);
154145
}
155146

156147
public static FileResource createClassloaderURI(String pathname) {

0 commit comments

Comments
 (0)