Re: [Ikvm-developers] Trouble with C# Console.SetOut and Console.SetError and Java PrintSream
Brought to you by:
jfrijters
From: Jeroen F. <je...@su...> - 2013-10-29 15:57:58
|
Hi, For maximum compatibility System.out/err/in use the underlying streams and the Java encodings, so if you want to redirect you have to either do it at the stdout/stderr/stdin level or use Java's System.setOut(), System.setErr() or System.setIn(). Regards, Jeroen > -----Original Message----- > From: Dr O'Neil Delpratt [mailto:on...@sa...] > Sent: Tuesday, October 29, 2013 16:16 > To: ikv...@li... > Cc: me...@da...; Michael Kay > Subject: [Ikvm-developers] Trouble with C# Console.SetOut and > Console.SetError and Java PrintSream > > Hi, > > In C# it is possible to set the Console.Out and Console.Error to a > TextWriter. However, in my simple java program below a call to > System.out.println does not write to the the TextWriter, but instead > writes to the console window. Is this the expected behaviour for IKVM? > Does IKVM not map the java System.out to its equivelent under C#, i.e. > Console.out? I am using IKVM 0.46.0.2. > This problem is from a user support issue at: > https://saxonica.plan.io/boards/3/topics/5823 > > Java Program: > > public class ProgTest { > > public ProgTest(){} > > public void print(){ > java.io.PrintStream out = System.out; > out.println("Test of the java printstream"); > } > > public static void main(){ > ProgTest test = new ProgTest(); > test.print(); > } > } > > ------------------------------------- > C# Program snippet: > > class Program > { > static void Main(string[] args) > { > // Cache the default Console TextWriter so we can use it > again later. > TextWriter defaultWriter = Console.Out; > > // Create a RecordingTextWriter and set it to be used for > Console.Out and Console.Error > RecordingTextWriter writer = new RecordingTextWriter(); > Console.SetOut(writer); > Console.SetError(writer); > > // Perform some operations that will write to the Console, > which has been set to the RecordingTextWriter. > performOperation(); > > // Now that everything is done, set Console.Out back to the > default value and display the result: > Console.SetOut(defaultWriter); > Console.WriteLine("The RecordingTextWriter object recorded > the following string:\n" + writer.Record.ToString()); > } > > private static void performOperation() > { > // Will record to the RecordingTextWriter class > Console.WriteLine("I'm writing on the default Console. > Next, I want to see what Saxon writes on the default Console."); > > // Next, create some Saxon classes and make them dump > errors on the Console. > try > { > ProgTest test = new ProgTest(); > test.print(); > } > catch (Exception ex) > { > Console.Error.WriteLine("Oh no! There was an error!"); > Console.Error.WriteLine(ex.ToString()); > } > } > } > > public class RecordingTextWriter : TextWriter > { > public StringBuilder Record { get; private set; } > > public RecordingTextWriter() > { > Record = new StringBuilder(); > } > > // Override Write and WriteLine so that the values recorded go > to the Record StringBuilder. > public override void Write(string value) > { > Record.Append(value); > } > public override void WriteLine(string value) > { > Record.AppendLine(value); > } > public override void WriteLine() > { > Record.AppendLine(); > } > > public override Encoding Encoding > { > get { throw new NotImplementedException(); } > } > } > } > > Kind regards, > > O'Neil > > ------------------------------------------------------------------------ > ------ > Android is increasing in popularity, but the open development platform > that developers love is also attractive to malware creators. Download > this white paper to learn more about secure code signing practices that > can help keep Android apps secure. > http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clk > trk > _______________________________________________ > Ikvm-developers mailing list > Ikv...@li... > https://lists.sourceforge.net/lists/listinfo/ikvm-developers |