Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // La fonction à avoir pour la lecture des réponses
- /**
- * Fonction trouvée sur stackoverflow qui lit un certain nombre de caractère
- * http://stackoverflow.com/questions/833744/most-efficient-way-to-read-in-a-tcp-stream-in-java
- *
- * @param reader Le reader
- * @param length La longueur de la chaîne à lire
- * @return String - La chaîne de caractère lue
- * @throws IOException
- */
- public static String readFully(Reader reader, int length) throws IOException
- {
- char[] buffer = new char[length];
- int totalRead = 0;
- while (totalRead < length)
- {
- int read = reader.read(buffer, totalRead, length-totalRead);
- if (read == -1)
- {
- throw new IOException("Insufficient data or User has disconnected");
- }
- totalRead += read;
- }
- return new String(buffer);
- }
- /////////////////////////////////////////////////////////////////////////
- // Elle s'utilise comme ceci
- // On récupère la taille de ce qu'on va lire envoyé par un nombre à 5 chiffres
- String lengthText = readFully(in, 5);
- // On le parse en int
- int length = Integer.parseInt(lengthText);
- // Puis on lit exactement le nombre de caractère récupérer par le nombre plus haut
- String ligne = readFully(in, length);
- ////////////////////////////////////////////////////////////////////////
- // Les informations que tu envoies doivent avoir devant ta réponse la taille du message
- // La je génère ma chaine de caractère correspondant a mon XML
- XMLOutputter sortie = new XMLOutputter();
- String envoi = sortie.outputString(doc);
- // puis j'ajoute devant mon XML "String.format("%05d", envoi.length())" qui va mettre la taille de la chaine envoi en 5 chiffres
- return (String.format("%05d", envoi.length()) + envoi);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement