黑莓输入流到字符串的转换

发布于 2024-10-05 08:15:09 字数 44 浏览 0 评论 0原文

如何在 BlackBerry 上将 InputStream 转换为字符串?

How do I convert an InputStream to a String on a BlackBerry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念﹏祤嫣 2024-10-12 08:15:16

Jonathan 的方法假设您的字节代表默认 BlackBerry 编码 (ISO-8859-1) 中的字符串。然而,大多数现代应用程序都是多语言的,因此支持的最佳编码是 UTF-8。为了尊重一组编码,你可以使用这样的东西:

/**
 * 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();
}

Jonathan's approach assumes your bytes represent a String in default BlackBerry encoding (ISO-8859-1). However most modern apps are multy-languaged, so the best encoding to support is UTF-8. To respect a set encoding you may use smth like this:

/**
 * 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();
}
2024-10-12 08:15:14

我会将 inputStream 中的数据存储在 StringBuffer 中。代码如下所示:

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();

I would store the data from the inputStream in a StringBuffer. The code would look like this:

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();
乱了心跳 2024-10-12 08:15:13

对于最少的代码来说怎么样:

String str = new String(IOUtilities.streamToBytes(is), "UTF-8");

How about this for minimal code:

String str = new String(IOUtilities.streamToBytes(is), "UTF-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文