Comment convertir un InputStream en chaîne sur un BlackBerry?Conversion de BlackBerry InputStream en chaîne
3
A
Répondre
4
Je voudrais stocker les données de l'inputStream dans un StringBuffer. Le code ressemblerait à ceci:
byte[] buffer = new byte[1024];
StringBuffer sb = new StringBuffer();
int readIn = 0;
while((readIn = inputStream.read(buffer)) > 0)
{
String temp = new String(buffer, 0, readIn);
sb.append(temp);
}
String result = sb.toString();
2
approche de Jonathan prend vos octets représentent une chaîne en encodage par défaut BlackBerry (ISO-8859-1). Cependant, la plupart des applications modernes sont multy-langaged, donc le meilleur encodage à supporter est UTF-8. Pour respecter un ensemble d'encodage que vous pouvez utiliser smth comme ceci:
/**
* Constructs a String using the data read from the passed InputStream.
* Data is read using a 1024-chars buffer. Each char is created using the passed
* encoding from one or more bytes.
*
* <p>If passed encoding is null, then the default BlackBerry encoding (ISO-8859-1) is used.</p>
*
* BlackBerry platform supports the following character encodings:
* <ul>
* <li>"ISO-8859-1"</li>
* <li>"UTF-8"</li>
* <li>"UTF-16BE"</li>
* <li>"UTF-16LE"</li>
* <li>"US-ASCII"</li>
* </ul>
*
* @param in - InputStream to read data from.
* @param encoding - String representing the desired character encoding, can be null.
* @return String created using the char data read from the passed InputStream.
* @throws IOException if an I/O error occurs.
* @throws UnsupportedEncodingException if encoding is not supported.
*/
public static String getStringFromStream(InputStream in, String encoding) throws IOException {
InputStreamReader reader;
if (encoding == null) {
reader = new InputStreamReader(in);
} else {
reader = new InputStreamReader(in, encoding);
}
StringBuffer sb = new StringBuffer();
final char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) > 0) {
sb.append(buf, 0, len);
}
return sb.toString();
}
13
Que diriez-vous cela pour un minimum de code:
String str = new String(IOUtilities.streamToBytes(is), "UTF-8");
Questions connexes
- 1. Blackberry Java Chaîne de conversion de date
- 2. Chaîne de conversion en décimal
- 3. Conversion de HtmlDocument.DomDocument en chaîne
- 4. Conversion de chaîne en heure
- 5. Conversion de chaîne en DOM
- 6. Conversion de chaîne en temps
- 7. Conversion de XPathDocument en chaîne
- 8. Conversion de GUID en chaîne
- 9. chaîne conversion en uint
- 10. Conversion Digits en chaîne
- 11. Inputstream to BufferedImage La conversion endommage le fichier
- 12. Conversion d'une chaîne AnsiString en chaîne Unicode
- 13. Conversion de chaîne double en chaîne en C++
- 14. Conversion d'une chaîne en CheckBox
- 15. Conversion Unicode en chaîne Java
- 16. Conversion d'ostream en chaîne standard
- 17. Conversion d'une liste en chaîne
- 18. Conversion de valeurs de chaîne en xml
- 19. Conversion hachage chaîne en Ruby
- 20. chaîne de conversion char
- 21. conversion de chaîne en int array
- 22. VB.Net Conversion de chaîne en double
- 23. Conversion de la chaîne en dateformat
- 24. chaîne de conversion à DateTime en C#
- 25. Javascript: Conversion de chaîne en nombre?
- 26. Conversion du contenu de HtmlTextWriter en chaîne
- 27. Problèmes de conversion d'entier en chaîne
- 28. Problèmes de conversion d'un CGPoint en chaîne
- 29. Conversion Python de chaîne binaire en hexadécimal
- 30. Conversion de HTMLDocument en chaîne imprimable
+1, vraiment minimalistes! –
Existe-t-il une solution non-Blackberry? – User1
oui, regardez la réponse acceptée pour cette question de Jonathan - qui n'utilise aucune API spécifique à BlackBerry –