package examples.pokerhands.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import examples.pokerhands.model.Hand;
import examples.pokerhands.valuation.Rater;
import examples.pokerhands.valuation.Valuation;

public class SimpleComparer {

	private Rater rater;

	public SimpleComparer() {
		super();
		rater = new Rater();
		rater.registerAll();
	}

	public boolean compare2Hands(InputStream in, OutputStream out) {
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
		Parser parser = new Parser();
		
		try {
			out.write("Enter hand 1: ".getBytes());
			out.flush();
			String line1 = reader.readLine();
			Hand hand1 = parser.parseHand(line1);
			Valuation v1 = rater.rate(hand1);
			
			out.write("Enter hand 2: ".getBytes());
			out.flush();
			String line2 = reader.readLine();
			Hand hand2 = parser.parseHand(line2);
			Valuation v2 = rater.rate(hand2);
			
			int result = v1.compareTo(v2);
			
			if(result > 0) {
				out.write("Winning hand: ".getBytes());
				out.write(hand1.toString().getBytes());
				out.write((" (" + v1.getRank().toString() + ")").getBytes());
			} else if(result < 0) {
				out.write("Winning hand: ".getBytes());
				out.write(hand2.toString().getBytes());		
				out.write((" (" + v2.getRank().toString() + ")").getBytes());
			} else {
				out.write("No winning hand".getBytes());
			}
			out.write("\n\n".getBytes());
			
			writer.flush();
		} catch (Exception e) {
			try {
				writer.write("Exception:" + e);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return false;
		}
		//FIXME close resource
		
		return true;
	}

}

@@ -780,27 +780,30 @@
 %0A%09%09try %7B%0A%09%09%09
-out
+writer
 .write(%22Ente
@@ -813,36 +813,28 @@
 and 1: %22
-.getBytes());%0A%09%09%09out
+);%0A%09%09%09writer
 .flush()
@@ -864,32 +864,76 @@
 der.readLine();%0A
+%09%09%09if(null == line1) %7B%0A%09%09%09%09line1 = %22%22;%0A%09%09%09%7D%0A
 %09%09%09Hand hand1 = 
@@ -997,27 +997,30 @@
 d1);%0A%09%09%09%0A%09%09%09
-out
+writer
 .write(%22Ente
@@ -1034,28 +1034,20 @@
 2: %22
-.getBytes());%0A%09%09%09out
+);%0A%09%09%09writer
 .flu
@@ -1089,16 +1089,60 @@
 Line();%0A
+%09%09%09if(null == line2) %7B%0A%09%09%09%09line2 = %22%22;%0A%09%09%09%7D%0A
 %09%09%09Hand 
@@ -1269,35 +1269,38 @@
 sult %3E 0) %7B%0A%09%09%09%09
-out
+writer
 .write(%22Winning 
@@ -1306,37 +1306,29 @@
  hand: %22
-.getBytes()
 );%0A%09%09%09%09
-out
+writer
 .write(h
@@ -1346,37 +1346,28 @@
 ng()
-.getBytes()
 );%0A%09%09%09%09
-out
+writer
 .write(
-(
 %22 (%22
@@ -1395,36 +1395,24 @@
 ing() + %22)%22)
-.getBytes())
 ;%0A%09%09%09%7D else 
@@ -1428,27 +1428,30 @@
  %3C 0) %7B%0A%09%09%09%09
-out
+writer
 .write(%22Winn
@@ -1465,29 +1465,21 @@
 d: %22
-.getBytes()
 );%0A%09%09%09%09
-out
+writer
 .wri
@@ -1501,39 +1501,28 @@
 ng()
-.getBytes());%09%09%0A%09%09%09%09out
+);%0A%09%09%09%09writer
 .write(
-(
 %22 (%22
@@ -1554,28 +1554,16 @@
 ) + %22)%22)
-.getBytes())
 ;%0A%09%09%09%7D e
@@ -1572,19 +1572,22 @@
 e %7B%0A%09%09%09%09
-out
+writer
 .write(%22
@@ -1602,27 +1602,16 @@
 ng hand%22
-.getBytes()
 );%0A%09%09%09%7D%0A
@@ -1613,19 +1613,22 @@
 %09%09%09%7D%0A%09%09%09
-out
+writer
 .write(%22
@@ -1636,25 +1636,10 @@
 n%5Cn%22
-.getBytes());%0A%09%09%09
+);
 %0A%09%09%09
@@ -1725,16 +1725,36 @@
 %22 + e);%0A
+%09%09%09%09writer.flush();%0A
 %09%09%09%7D cat

Use Buffered output in comparer