Performance improvement:
[jvcard.git] / src / be / nikiroo / jvcard / remote / Server.java
index 08091f6ea352e59cd96e5ec2a59032650eaea65c..ac29e972a562fe94685358c909221a9ffd38d119 100644 (file)
@@ -11,15 +11,16 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.ResourceBundle;
 
 import be.nikiroo.jvcard.Card;
 import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.parsers.Format;
 import be.nikiroo.jvcard.parsers.Vcard21Parser;
-import be.nikiroo.jvcard.resources.Bundles;
+import be.nikiroo.jvcard.remote.SimpleSocket.BlockAppendable;
 import be.nikiroo.jvcard.resources.StringUtils;
+import be.nikiroo.jvcard.resources.bundles.RemoteBundle;
+import be.nikiroo.jvcard.resources.enums.RemotingOption;
 
 /**
  * This class implements a small server that can listen for requests to
@@ -58,9 +59,9 @@ public class Server implements Runnable {
         */
        public Server(int port) throws IOException {
                this.port = port;
-               ResourceBundle bundle = Bundles.getBundle("remote");
+               RemoteBundle bundle = new RemoteBundle();
                try {
-                       String dir = bundle.getString("SERVER_DATA_PATH");
+                       String dir = bundle.getString(RemotingOption.SERVER_DATA_PATH);
                        dataDir = new File(dir);
                        dataDir.mkdir();
 
@@ -323,7 +324,7 @@ public class Server implements Runnable {
 
                switch (command) {
                case GET_CARD: {
-                       s.sendBlock(doGetCard(name));
+                       sendCardBlock(s, name);
                        break;
                }
                case POST_CARD: {
@@ -405,10 +406,13 @@ public class Server implements Runnable {
                switch (command) {
                case GET_CONTACT: {
                        Contact contact = card.getById(cmd.getParam());
-                       if (contact != null)
-                               s.sendBlock(Vcard21Parser.toStrings(contact, -1));
-                       else
+                       if (contact != null) {
+                               BlockAppendable app = s.createBlockAppendable();
+                               Vcard21Parser.write(app, contact, -1);
+                               app.close();
+                       } else {
                                s.sendBlock();
+                       }
                        break;
                }
                case POST_CONTACT: {
@@ -453,7 +457,7 @@ public class Server implements Runnable {
                        if (contact == null) {
                                s.sendBlock();
                        } else {
-                               s.sendLine(contact.getContentState());
+                               s.sendLine(contact.getContentState(true));
                        }
                        break;
                }
@@ -464,7 +468,8 @@ public class Server implements Runnable {
                                                || (contact.getPreferredDataValue("FN") + contact
                                                                .getPreferredDataValue("N")).toLowerCase()
                                                                .contains(cmd.getParam().toLowerCase())) {
-                                       s.send(contact.getContentState() + " " + contact.getId());
+                                       s.send(contact.getContentState(true) + " "
+                                                       + contact.getId());
                                }
                        }
                        s.sendBlock();
@@ -515,9 +520,9 @@ public class Server implements Runnable {
                case GET_DATA: {
                        for (Data data : contact) {
                                if (data.getName().equals(cmd.getParam())) {
-                                       for (String line : Vcard21Parser.toStrings(data)) {
-                                               s.send(line);
-                                       }
+                                       BlockAppendable app = s.createBlockAppendable();
+                                       Vcard21Parser.write(app, data);
+                                       // note: we do NOT close 'app', since it would send an EOB
                                }
                        }
                        s.sendBlock();
@@ -527,7 +532,7 @@ public class Server implements Runnable {
                        String cstate = cmd.getParam();
                        Data data = null;
                        for (Data d : contact) {
-                               if (cstate.equals(d.getContentState()))
+                               if (cstate.equals(d.getContentState(true)))
                                        data = d;
                        }
 
@@ -543,7 +548,7 @@ public class Server implements Runnable {
                        String cstate = cmd.getParam();
                        Data data = null;
                        for (Data d : contact) {
-                               if (cstate.equals(d.getContentState()))
+                               if (cstate.equals(d.getContentState(true)))
                                        data = d;
                        }
 
@@ -559,7 +564,7 @@ public class Server implements Runnable {
                case HASH_DATA: {
                        for (Data data : contact) {
                                if (data.getId().equals(cmd.getParam())) {
-                                       s.send(data.getContentState());
+                                       s.send(data.getContentState(true));
                                }
                        }
                        s.sendBlock();
@@ -571,7 +576,7 @@ public class Server implements Runnable {
                                                || cmd.getParam().length() == 0
                                                || data.getName().toLowerCase()
                                                                .contains(cmd.getParam().toLowerCase())) {
-                                       s.send(data.getContentState() + " " + data.getName());
+                                       s.send(data.getContentState(true) + " " + data.getName());
                                }
                        }
                        s.sendBlock();
@@ -601,20 +606,19 @@ public class Server implements Runnable {
         * @throws IOException
         *             in case of error
         */
-       private List<String> doGetCard(String name) throws IOException {
-               List<String> lines = new LinkedList<String>();
-
+       private void sendCardBlock(SimpleSocket s, String name) throws IOException {
                File vcf = getFile(name);
+               BlockAppendable app = s.createBlockAppendable();
 
                if (vcf != null && vcf.exists()) {
                        Card card = new Card(vcf, Format.VCard21);
 
                        // timestamp + data
-                       lines.add(StringUtils.fromTime(card.getLastModified()));
-                       lines.addAll(Vcard21Parser.toStrings(card));
+                       app.append(StringUtils.fromTime(card.getLastModified()) + "\r\n");
+                       Vcard21Parser.write(app, card);
                }
 
-               return lines;
+               app.close();
        }
 
        /**