Skip to content

Commit

Permalink
Add an InputStreamSource for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 27, 2017
1 parent 7dedd74 commit d858e5f
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions builtins/src/main/java/org/jline/builtins/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

public interface Source {

Expand All @@ -27,7 +28,7 @@ class URLSource implements Source {
final String name;

public URLSource(URL url, String name) {
this.url = url;
this.url = Objects.requireNonNull(url);
this.name = name;
}

Expand All @@ -48,12 +49,11 @@ class PathSource implements Source {
final String name;

public PathSource(File file, String name) {
this.path = file.toPath();
this.name = name;
this(Objects.requireNonNull(file).toPath(), name);
}

public PathSource(Path path, String name) {
this.path = path;
this.path = Objects.requireNonNull(path);
this.name = name;
}

Expand All @@ -69,19 +69,39 @@ public InputStream read() throws IOException {

}

class StdInSource implements Source {
class InputStreamSource implements Source {
final InputStream in;
final String name;

public InputStreamSource(InputStream in, boolean close, String name) {
Objects.requireNonNull(in);
if (close) {
this.in = in;
} else {
this.in = new FilterInputStream(in) {
@Override
public void close() throws IOException {
}
};
}
this.name = name;
}

@Override
public String getName() {
return null;
return name;
}

@Override
public InputStream read() throws IOException {
return new FilterInputStream(System.in) {
@Override
public void close() throws IOException {
}
};
return in;
}
}

class StdInSource extends InputStreamSource {

public StdInSource() {
super(System.in, false, null);
}

}
Expand Down

0 comments on commit d858e5f

Please sign in to comment.