Merge branch 'master' into streamify
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
index be987083c5e54e3db1228f563b4ba1bf322a8918..c5750bdcf2aa0616c955229a94388cb95195066b 100644 (file)
@@ -2,8 +2,9 @@ package be.nikiroo.utils.serial.server;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
+import java.io.OutputStream;
 import java.net.Socket;
 
 import javax.net.ssl.SSLException;
@@ -31,8 +32,8 @@ abstract class ConnectAction {
        private CryptUtils crypt;
 
        private Object lock = new Object();
-       private BufferedReader in;
-       private OutputStreamWriter out;
+       private InputStream in;
+       private OutputStream out;
        private boolean contentToSend;
 
        private long bytesReceived;
@@ -136,17 +137,16 @@ abstract class ConnectAction {
         */
        public void connect() {
                try {
-                       in = new BufferedReader(new InputStreamReader(s.getInputStream(),
-                                       "UTF-8"));
+                       in = s.getInputStream();
                        try {
-                               out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
+                               out = s.getOutputStream();
                                try {
                                        if (server) {
                                                String line;
                                                try {
                                                        line = readLine(in);
                                                } catch (SSLException e) {
-                                                       out.write("Unauthorized\n");
+                                                       out.write("Unauthorized\n".getBytes());
                                                        throw e;
                                                }
 
@@ -199,8 +199,9 @@ abstract class ConnectAction {
         * @param data
         *            the data to send
         * 
-        * @return the answer (which can be NULL) if this action is a client, always
-        *         NULL if it is a server
+        * @return the answer (which can be NULL if no answer, or NULL for an answer
+        *         which is NULL) if this action is a client, always NULL if it is a
+        *         server
         * 
         * @throws IOException
         *             in case of I/O error
@@ -216,10 +217,19 @@ abstract class ConnectAction {
        protected Object sendObject(Object data) throws IOException,
                        NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
                synchronized (lock) {
-                       String rep = sendString(new Exporter().append(data).toString(true,
-                                       true));
-                       if (rep != null) {
-                               return new Importer().read(rep).getValue();
+
+                       new Exporter(out).append(data);
+
+                       if (server) {
+                               out.flush();
+                               return null;
+                       }
+
+                       contentToSend = true;
+                       try {
+                               return recObject();
+                       } catch (NullPointerException e) {
+                               // We accept no data here
                        }
 
                        return null;
@@ -253,12 +263,18 @@ abstract class ConnectAction {
        protected Object recObject() throws IOException, NoSuchFieldException,
                        NoSuchMethodException, ClassNotFoundException,
                        java.lang.NullPointerException {
-               String str = recString();
-               if (str == null) {
-                       throw new NullPointerException("No more data available");
-               }
+               synchronized (lock) {
+                       if (server || contentToSend) {
+                               if (contentToSend) {
+                                       out.flush();
+                                       contentToSend = false;
+                               }
+                               
+                               return new Importer().read(in).getValue();
+                       }
 
-               return new Importer().read(str).getValue();
+                       return null;
+               }
        }
 
        /**
@@ -334,8 +350,11 @@ abstract class ConnectAction {
         * @throws SSLException
         *             in case of crypt error
         */
-       private String readLine(BufferedReader in) throws IOException {
-               String line = in.readLine();
+       private String readLine(InputStream in) throws IOException {
+               if (inReader == null) {
+                       inReader = new BufferedReader(new InputStreamReader(in));
+               }
+               String line = inReader.readLine();
                if (line != null) {
                        bytesReceived += line.length();
                        if (crypt != null) {
@@ -346,6 +365,8 @@ abstract class ConnectAction {
                return line;
        }
 
+       private BufferedReader inReader;
+
        /**
         * Write a line, possible encrypted.
         * 
@@ -358,18 +379,17 @@ abstract class ConnectAction {
         * @throws SSLException
         *             in case of crypt error
         */
-       private void writeLine(OutputStreamWriter out, String line)
-                       throws IOException {
+       private void writeLine(OutputStream out, String line) throws IOException {
                if (crypt == null) {
-                       out.write(line);
+                       out.write(line.getBytes("UTF-8"));
                        bytesSent += line.length();
                } else {
                        // TODO: how NOT to create so many big Strings?
                        String b64 = crypt.encrypt64(line, false);
-                       out.write(b64);
+                       out.write(b64.getBytes("UTF-8"));
                        bytesSent += b64.length();
                }
-               out.write("\n");
+               out.write("\n".getBytes("UTF-8"));
                bytesSent++;
        }
 }
\ No newline at end of file