|
From: <cr...@us...> - 2008-09-21 05:07:28
|
Revision: 4568
http://jnode.svn.sourceforge.net/jnode/?rev=4568&view=rev
Author: crawley
Date: 2008-09-21 05:07:23 +0000 (Sun, 21 Sep 2008)
Log Message:
-----------
Added a method to the CommandIO to get the base Closeable for the object.
For example, this can be used to get a KeyboardReader / ConsoleWriter.
Modified Paths:
--------------
trunk/core/src/core/org/jnode/util/IOUtils.java
trunk/shell/src/shell/org/jnode/shell/io/BaseCommandIO.java
trunk/shell/src/shell/org/jnode/shell/io/CommandIO.java
trunk/shell/src/shell/org/jnode/shell/io/CommandIOMarker.java
trunk/shell/src/shell/org/jnode/shell/io/CommandInput.java
trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java
trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java
Modified: trunk/core/src/core/org/jnode/util/IOUtils.java
===================================================================
--- trunk/core/src/core/org/jnode/util/IOUtils.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/core/src/core/org/jnode/util/IOUtils.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -77,6 +77,32 @@
}
}
+ public static Closeable findBaseStream(Closeable stream) {
+ if (stream instanceof ConsoleStream) {
+ return stream;
+ } else if (stream instanceof ProxyStream<?>) {
+ try {
+ return findBaseStream(((ProxyStream<?>) stream).getRealStream());
+ } catch (ProxyStreamException ex) {
+ return null;
+ }
+ } else if (stream instanceof OutputStreamWriter) {
+ return findBaseStream(findOutputStream((OutputStreamWriter) stream));
+ } else if (stream instanceof InputStreamReader) {
+ return findBaseStream(findInputStream((InputStreamReader) stream));
+ } else if (stream instanceof ReaderInputStream) {
+ return findBaseStream(((ReaderInputStream) stream).getReader());
+ } else if (stream instanceof WriterOutputStream) {
+ return findBaseStream(((WriterOutputStream) stream).getWriter());
+ } else if (stream instanceof FilterInputStream) {
+ return findBaseStream(findInputStream((FilterInputStream) stream));
+ } else if (stream instanceof FilterOutputStream) {
+ return findBaseStream(findOutputStream((FilterOutputStream) stream));
+ } else {
+ return stream;
+ }
+ }
+
private static InputStream findInputStream(final FilterInputStream inputStream) {
return new PrivilegedAction<InputStream>() {
public InputStream run() {
Modified: trunk/shell/src/shell/org/jnode/shell/io/BaseCommandIO.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/BaseCommandIO.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/shell/src/shell/org/jnode/shell/io/BaseCommandIO.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -20,15 +20,18 @@
*/
package org.jnode.shell.io;
+import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.Charset;
+import org.jnode.util.IOUtils;
+
abstract class BaseCommandIO implements CommandIO {
private String assignedEncoding;
- private final Object systemObject;
+ private final Closeable systemObject;
- BaseCommandIO(Object systemObject) {
+ BaseCommandIO(Closeable systemObject) {
this.systemObject = systemObject;
}
@@ -46,11 +49,22 @@
return Charset.defaultCharset().name();
}
- public final Object getSystemObject() {
+ public final Closeable getSystemObject() {
return systemObject;
}
+
+ @Override
+ public Closeable findBaseStream() {
+ return IOUtils.findBaseStream(systemObject);
+ }
- public abstract boolean isTTY();
+ public final boolean isTTY() {
+ if (systemObject == null) {
+ return false;
+ } else {
+ return IOUtils.isTTY(systemObject);
+ }
+ }
public abstract void close() throws IOException;
Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandIO.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandIO.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandIO.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -20,6 +20,7 @@
*/
package org.jnode.shell.io;
+import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -103,6 +104,16 @@
public boolean isTTY();
/**
+ * Obtain the 'base' stream object for this CommandIO. This will be
+ * as close as we can get to the real device Reader, Writer, InputStream
+ * or OutputStream.
+ *
+ * @return the base stream, or <code>null</code> if there is a problem
+ * resolving the stream.
+ */
+ public Closeable findBaseStream();
+
+ /**
* Close this CommandIO's associated byte and / or character streams,
* including any that were returned via the get... methods.
*
Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandIOMarker.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandIOMarker.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandIOMarker.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -50,11 +50,6 @@
public int getDirection() {
return DIRECTION_UNSPECIFIED;
}
-
- @Override
- public boolean isTTY() {
- return false;
- }
public void close() {
// do nothing.
Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandInput.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandInput.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandInput.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -30,7 +30,6 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
-import org.jnode.util.IOUtils;
import org.jnode.util.ReaderInputStream;
/**
@@ -76,16 +75,6 @@
return DIRECTION_IN;
}
- @Override
- public boolean isTTY() {
- Object obj = getSystemObject();
- if (obj instanceof Reader) {
- return IOUtils.isTTY((Reader) obj);
- } else {
- return IOUtils.isTTY((InputStream) obj);
- }
- }
-
public void close() throws IOException {
if (inputStream != null) {
inputStream.close();
Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandInputOutput.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -31,7 +31,6 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
-import org.jnode.util.IOUtils;
import org.jnode.util.ReaderInputStream;
import org.jnode.util.WriterOutputStream;
@@ -121,18 +120,8 @@
public final int getDirection() {
return DIRECTION_INOUT;
}
-
+
@Override
- public boolean isTTY() {
- Object obj = getSystemObject();
- if (obj instanceof Writer) {
- return IOUtils.isTTY((Writer) obj);
- } else {
- return IOUtils.isTTY((OutputStream) obj);
- }
- }
-
- @Override
public void close() throws IOException {
flush();
Modified: trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java 2008-09-21 04:44:44 UTC (rev 4567)
+++ trunk/shell/src/shell/org/jnode/shell/io/CommandOutput.java 2008-09-21 05:07:23 UTC (rev 4568)
@@ -30,7 +30,6 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
-import org.jnode.util.IOUtils;
import org.jnode.util.WriterOutputStream;
/**
@@ -98,16 +97,6 @@
return DIRECTION_OUT;
}
- @Override
- public boolean isTTY() {
- Object obj = getSystemObject();
- if (obj instanceof Writer) {
- return IOUtils.isTTY((Writer) obj);
- } else {
- return IOUtils.isTTY((OutputStream) obj);
- }
- }
-
public void close() throws IOException {
flush();
if (printWriter != null) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|