Advertisement
Guest User

discussion

a guest
May 3rd, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1.      // La fonction à avoir pour la lecture des réponses
  2.      /**
  3.      * Fonction trouvée sur stackoverflow qui lit un certain nombre de caractère
  4.      * http://stackoverflow.com/questions/833744/most-efficient-way-to-read-in-a-tcp-stream-in-java
  5.      *
  6.      * @param reader Le reader
  7.      * @param length La longueur de la chaîne à lire
  8.      * @return String - La chaîne de caractère lue
  9.      * @throws IOException
  10.      */
  11.     public static String readFully(Reader reader, int length) throws IOException
  12.     {
  13.         char[] buffer = new char[length];
  14.         int totalRead = 0;
  15.         while (totalRead < length)
  16.         {
  17.             int read = reader.read(buffer, totalRead, length-totalRead);
  18.             if (read == -1)
  19.             {
  20.                 throw new IOException("Insufficient data or User has disconnected");
  21.             }
  22.             totalRead += read;
  23.         }
  24.         return new String(buffer);
  25.     }
  26.  
  27. /////////////////////////////////////////////////////////////////////////
  28.  
  29. // Elle s'utilise comme ceci
  30. // On récupère la taille de ce qu'on va lire envoyé par un nombre à 5 chiffres
  31. String lengthText = readFully(in, 5);
  32. // On le parse en int
  33. int length = Integer.parseInt(lengthText);
  34. // Puis on lit exactement le nombre de caractère récupérer par le nombre plus haut
  35. String ligne = readFully(in, length);
  36.  
  37. ////////////////////////////////////////////////////////////////////////
  38.  
  39. // Les informations que tu envoies doivent avoir devant ta réponse la taille du message
  40.  
  41. // La je génère ma chaine de caractère correspondant a mon XML
  42. XMLOutputter sortie = new XMLOutputter();
  43. String envoi = sortie.outputString(doc);
  44.        
  45. // puis j'ajoute devant mon XML "String.format("%05d", envoi.length())" qui va mettre la taille de la chaine envoi en 5 chiffres
  46. return (String.format("%05d", envoi.length()) + envoi);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement