[m-rev.] For review: Java implementation of IO library
James Goddard
goddardjames at yahoo.com
Fri Jan 16 15:55:59 AEDT 2004
--- Fergus Henderson <fjh at cs.mu.OZ.AU> wrote: > On 15-Jan-2004, James Goddard
>
> I had a bit more of a think about get_input_channel().
> Rather than hard-coding the knowledge of how to get to the FileChannel
> field, it might be better to just dynamically search all the fields for it,
> using code roughly along the lines of what is below.
>
> // This program uses reflection to dump out the fields of
> // System.out and System.in
> [...]
I had a play around with that stuff, and got it to return any FileChannels it
found. It finds them ok, but when you print the value it just gives you null.
The reason for this is that the FileChannel is instantiated lazily, so it
doesn't actually exist until you call getChannel(). (If you're interested,
I've attached the source for the program that shows this)
So anyway, given that it seems we need to call getChannel() after all, I've
discovered a neater way to find it, using FileDescriptors, which are from JDK
1.0. It only requires a few lines of common code for fetching both
FileChannels. Tell me what you think:
diff -u io.m io.m
--- io.m 15 Jan 2004 06:38:28 -0000
+++ io.m 16 Jan 2004 04:38:38 -0000
@@ -4848,9 +4848,9 @@
private java.io.RandomAccessFile randomaccess = null;
/* binary_stdin only */
- private java.io.BufferedInputStream binary_input = null;
+ private java.io.FileInputStream binary_input = null;
/* binary_stdout only */
- private java.io.BufferedOutputStream binary_output = null;
+ private java.io.FileOutputStream binary_output = null;
int position = 0;
private java.lang.Object channel = null;
@@ -4913,9 +4913,10 @@
input = new java.io.
InputStreamReader(stream);
} else {
- binary_input = new java.io.BufferedInputStream(
- stream);
- channel = get_in_channel();
+ /* open stdin as binary */
+ binary_input = new java.io.FileInputStream(
+ java.io.FileDescriptor.in);
+ channel = get_channel(binary_input);
}
}
@@ -4929,9 +4930,10 @@
output = new java.io.
OutputStreamWriter(stream);
} else {
- binary_output = new java.io.
- BufferedOutputStream(stream);
- channel = get_out_channel();
+ /* open stdout as binary */
+ binary_output = new java.io.FileOutputStream(
+ java.io.FileDescriptor.out);
+ channel = get_channel(binary_output);
}
}
/*
+ ** get_channel():
+ ** Given some object, attempts to call getChannel() using
+ ** Reflection and returns the result, or null if getChannel() is
+ ** not available.
+ */
+ static java.lang.Object/*FileChannel*/ get_channel(java.lang.Object o)
+ {
+ try {
+ // Simulate
+ // return o.getChannel();
+ // using reflection.
+ java.lang.reflect.Method getChannel_mth =
+ o.getClass().getMethod(
+ ""getChannel"", null);
+
+ return getChannel_mth.invoke(o, null);
+ }
+ catch (java.lang.Exception e) {
+ return null;
+ }
+ }
+
http://personals.yahoo.com.au - Yahoo! Personals
New people, new possibilities. FREE for a limited time.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ChannelFinder.java
Type: application/octet-stream
Size: 1731 bytes
Desc: ChannelFinder.java
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20040116/f3b39306/attachment.obj>
More information about the reviews
mailing list