move sep char to final const
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
index 50d9ffcf61abdc8991094769ab8b6fd3cf3d68aa..0f2f1b4319fa799950119815c31dd9bf81043ee8 100644 (file)
@@ -9,6 +9,7 @@ import javax.net.ssl.SSLException;
 
 import be.nikiroo.utils.CryptUtils;
 import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.serial.Exporter;
 import be.nikiroo.utils.serial.Importer;
 import be.nikiroo.utils.streams.BufferedOutputStream;
@@ -27,6 +28,12 @@ import be.nikiroo.utils.streams.ReplaceOutputStream;
  * @author niki
  */
 abstract class ConnectAction {
+       // We separate each "packet" we send with this character and make sure it
+       // does not occurs in the message itself.
+       static private char STREAM_SEP = '\b';
+       static private String[] STREAM_RAW = new String[] { "\\", "\b" };
+       static private String[] STREAM_CODED = new String[] { "\\\\", "\\b" };
+
        private Socket s;
        private boolean server;
 
@@ -98,13 +105,10 @@ abstract class ConnectAction {
         */
        public void connect() {
                try {
-                       // TODO: assure that \b is never used, make sure \n usage is OK
                        in = new NextableInputStream(s.getInputStream(),
-                                       new NextableInputStreamStep('\b'));
-
+                                       new NextableInputStreamStep(STREAM_SEP));
                        try {
                                out = new BufferedOutputStream(s.getOutputStream());
-
                                try {
                                        action();
                                } finally {
@@ -293,15 +297,10 @@ abstract class ConnectAction {
                                sub = out.open();
                        }
 
-                       // TODO: could be possible to check for non-crypt and only
-                       // do it for crypt
-                       sub = new ReplaceOutputStream(sub, //
-                                       new String[] { "\\", "\b" }, //
-                                       new String[] { "\\\\", "\\b" });
-
+                       sub = new ReplaceOutputStream(sub, STREAM_RAW, STREAM_CODED);
                        try {
                                if (asString) {
-                                       sub.write(data.toString().getBytes("UTF-8"));
+                                       sub.write(StringUtils.getBytes(data.toString()));
                                } else {
                                        new Exporter(sub).append(data);
                                }
@@ -309,7 +308,7 @@ abstract class ConnectAction {
                                sub.close();
                        }
 
-                       out.write('\b');
+                       out.write(STREAM_SEP);
 
                        if (server) {
                                out.flush();
@@ -363,6 +362,7 @@ abstract class ConnectAction {
         * @throws java.lang.NullPointerException
         *             for Objects only: if the counter part has no data to send
         */
+       @SuppressWarnings("resource")
        private Object rec(boolean asString) throws IOException,
                        NoSuchFieldException, NoSuchMethodException,
                        ClassNotFoundException, java.lang.NullPointerException {
@@ -374,13 +374,9 @@ abstract class ConnectAction {
                                        contentToSend = false;
                                }
 
-                               if (in.next()) {
-                                       // TODO: could be possible to check for non-crypt and only
-                                       // do it for crypt
-                                       InputStream read = new ReplaceInputStream(in.open(), //
-                                                       new String[] { "\\\\", "\\b" }, //
-                                                       new String[] { "\\", "\b" });
-
+                               if (in.next() && !in.eof()) {
+                                       InputStream read = new ReplaceInputStream(in.open(),
+                                                       STREAM_CODED, STREAM_RAW);
                                        try {
                                                if (crypt != null) {
                                                        read = crypt.decrypt64(read);