Java 如何确定ByteArrayOutputStream的编码?

Java 如何确定ByteArrayOutputStream的编码?,java,Java,我需要将ByteArrayOutputStream转换为String,但我无法确定编码。请帮忙?我尝试使用ICUJ库,但它只适用于输入流。从字节数组到输入流的转换也很好 下面是我使用默认编码得到的示例。显然,新线不应该出现在那里 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n <html> \n <head

我需要将
ByteArrayOutputStream
转换为
String
,但我无法确定编码。请帮忙?我尝试使用
ICUJ
库,但它只适用于输入流。从字节数组到输入流的转换也很好

下面是我使用默认编码得到的示例。显然,新线不应该出现在那里

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n
<html>
   \n   
   <head>
      \n        
      <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
      \n            
      <style type=\"text/css\">\n                   .style_0 { font-family: sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; color: black; text-indent: 0em; letter-spacing: normal; word-spacing: normal; text-transform: none; white-space: normal; line-height: normal;}\n                    .style_1 { height: 5.062in; width: 8.01in;}\n           </style>
      \n            <script type=\"text/javascript\">\n          //<![CDATA[\n             function redirect(target, url){\n                   if (target =='_blank'){\n                       open(url);\n                }\n                 else if (target == '_top'){\n                       window.top.location.href=url;\n                 }\n                 else if (target == '_parent'){\n                    location.href=url;\n                }\n                 else if (target == '_self'){\n                      location.href =url;\n                   }\n                 else{\n                     open(url);\n                }\n                }\n            //]]>\n            </script>\n     
   </head>
   \n       <body class=\"style_0\" style=\" margin:0px;\">\n           <table cellpadding=\"0\" style=\"empty-cells: show; border-collapse:collapse; width:8in; overflow: hidden; table-layout:fixed;\">\n             
   <col>
   </col>\n             
   <tr>
      \n                    
      <td></td>
      \n                
   </tr>
   \n               
   <tr>
      \n                    
      <td valign=\"top\"></td>
      \n                
   </tr>
   \n               
   <tr>
      \n                    
      <td>
         \n                     
         <div style=\"overflow:hidden; height:0.5in\">\n                            <div style=\" overflow:hidden;\">Dec 23, 2013, 7:11 PM</div>
         \n                     </div>\n                    
      </td>
      \n                
   </tr>
   \n           </table>\n              
   <hr style=\"color:red\"/>
   \n               
   <div style=\"color:red\">
   \n                   
   <div>The following items have errors:\n          </div>
   \n           <br>\n                      
   <div>
   \n                           
   <div  id=\"error_title\" style=\"text-decoration:underline\">
   Chart (id = 12):
\n
\n
\n
\n
\n.style_0{font family:sans serif;font style:normal;font variance:normal;font weight:normal;font size:10pt;颜色:黑色;文本缩进:0em;字母间距:normal;单词间距:normal;文本转换:none;空格:normal;行高:normal;}\n.style_1{height:5.062in;width:8.01in;}\n
\n\n//\n\n
\n\n\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\2013年12月23日下午7:11
\n\n
\n
\n\n

\n \n 下列项目有错误:\n \n
\n \n 图表(id=12):
\n

我尝试使用ICUJ库,但它只适用于输入流

您可以从
ByteArrayInputStream
获取字节数组,然后将其包装在
ByteArrayInputStream
中。。。并将其传递给ICUJ方法


(请记住,ICUJ很有可能会插入错误的编码。或者字节可能不代表任何已知编码中的文本。)

我尝试使用ICUJ库,但它只适用于输入流

您可以从
ByteArrayInputStream
获取字节数组,然后将其包装在
ByteArrayInputStream
中。。。并将其传递给ICUJ方法



(请记住,ICUJ很可能会通过直觉输入错误的编码。或者字节可能不代表任何已知编码中的文本。)

这没有多大帮助,但java.nio.charset.CharsetDecoder有一个
detectedCharset()
方法来自动识别字符编码字节的字符集。但不幸的是,Java SE7中当前的CharSetDecoder impl(通过调用方法
Charset.newDecoder()
)不是一个自动检测Charset解码器,因此调用
detectedCharSet()
method抛出
UnsupportedOperationException

这没有多大帮助,但是java.nio.charset.CharsetDecoder有一个
detectedCharset()
方法来自动识别字符编码字节的字符集。但不幸的是,Java SE7中当前的CharSetDecoder impl(通过调用方法
Charset.newDecoder()
)并不是一个自动检测Charset解码器,因此调用
detectedCharSet()
方法会抛出
不支持操作异常

,我不是要你神奇地猜测编码。我知道库的存在是为了找出
InputStream
的编码,所以肯定有一种编程的方法来解决这个问题。如果你不能帮忙,请不要发表无用的评论。如果ICU4J可以做你想做的事情,那么只需将
BAOS
转换成
BAIS
。顺便说一句,没有自动的方法来计算文本编码。你所能期望的最好的是一个脆弱的启发式方法。不想显得尖刻,但如果编码来自有文档记录的API(例如,如果是JSON,则定义为UTF8),则应该在某处记录编码,如果是您自己的代码,您应该能够看到流的生成位置并找出它。我试图找出的编码是BIRT报告输出流,它基本上是输出HTML标记。你知道这通常使用哪种编码吗?我试过Utf-8我不是要你神奇地猜编码。我知道库的存在是为了找出
InputStream
的编码,所以肯定有一种编程的方法来解决这个问题。如果你不能帮忙,请不要发表无用的评论。如果ICU4J可以做你想做的事情,那么只需将
BAOS
转换成
BAIS
。顺便说一句,没有自动的方法来计算文本编码。你所能期望的最好的是一个脆弱的启发式方法。不想显得尖刻,但如果编码来自有文档记录的API(例如,如果是JSON,则定义为UTF8),则应该在某处记录编码,如果是您自己的代码,您应该能够看到流的生成位置并找出它。我试图找出的编码是BIRT报告输出流,它基本上是输出HTML标记。你知道这通常使用哪种编码吗?我试过Utf-8