- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个静态辅助方法,负责从我们的 Rails 应用程序中获取压缩的 JSON 字符串,并在返回 String
表示之前解压缩数据。
我编写了两个 JUnit 测试,一个测试 JSON 是否正确解析,另一个更基本的测试确定是否从服务器返回长度大于零的字符串。
问题:当我运行测试套件时,第一个测试方法正确地成功,而另一个失败并出现 IOException
和消息“Corrupt GZIP trailer”(见下面的代码)。我已经确定失败的不是测试本身,因为当我使测试以相反的顺序运行时,“成功”的测试被逆转了(换句话说,无论如何,失败的总是第二个测试,无论两个测试中的哪一个先运行)。
这是辅助方法:
public static String doHTTPGet(String urlString) throws IOException{
URL weatherAPI = new URL(urlString);
HttpURLConnection apiConnection = (HttpURLConnection) weatherAPI.openConnection();
apiConnection.setRequestMethod("GET");
apiConnection.setRequestProperty("Accept-Encoding", "gzip");
apiConnection.connect();
BufferedInputStream bufferedInputStream = new BufferedInputStream(apiConnection.getInputStream());
byte[] inputByteBuffer = new byte[10 * 1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(10 * 1024); // initialize the output stream with at least one buffer's worth of bytes
while(bufferedInputStream.read(inputByteBuffer) > -1){
outputStream.write(inputByteBuffer);
}
outputStream.close();
bufferedInputStream.close();
apiConnection.disconnect();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
byteArrayInputStream.close();
GZIPInputStream gis = new GZIPInputStream(byteArrayInputStream);
InputStreamReader inputStreamReader = new InputStreamReader(gis, "UTF-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
String decompressedResponse = "";
String line;
// readLine() is generating the IOException on the second pass.
while((line = reader.readLine()) != null){
decompressedResponse += line;
}
reader.close();
inputStreamReader.close();
gis.close();
return decompressedResponse;
}
错误发生在辅助方法的底部,在行 while((line = reader.readLine()) != null)...
上。具体来说,错误发生在 reader.readLine()
上。
以及两种测试方法:
@Test
public void testHttpGet(){
try {
// FILTERED_API_URL_WITH_TOKEN is merely the URL with an auth token
String apiResponse = HTTPHelper.doHTTPGet(GlobalConstants.FILTERED_API_URL_WITH_TOKEN);
assertNotNull(apiResponse);
assertTrue("The size of the API response should be greater than zero. It is an empty string.", apiResponse.length() > 0);
} catch (IOException e) {
e.printStackTrace();
assertTrue("An exception occured while trying to perform the HTTP Get to the api at URL " + GlobalConstants.FILTERED_API_URL_WITH_TOKEN, false);
}
}
@Test
public void testAPIContent(){
try {
// the getAPIJson() method basically does the same as the testHttpGet
// method, but converts the string to a json
JSONObject jsonObject = XMLProducerFromAPI.getAPIJson();
System.out.println(jsonObject);
assertNotNull(jsonObject);
} catch (IOException e) {
e.printStackTrace();
assertTrue("An IOException occured. See stack trace", false);
} catch (JSONException e) {
e.printStackTrace();
assertTrue("A JSONException occured. See stack trace", false);
}
}
我已通读 this question和 the answer ,但我认为它不适用,(或者可能是但我误解了,如果是这种情况请告诉我),我尝试了他们的方法,但只收到了相同的消息。
由于 doHTTPGet
方法是静态的,并且创建的对象是在方法体内完成的,因此不应重用任何内容(流、连接对象等)。坦率地说,我很难过。
问题:我是否在我的辅助代码中做错了什么,或者我是否误解了某些对象的某些用法,这会产生“Corrupt GZIP Trailer”消息?简而言之,在我的场景中会导致此错误的原因是什么?
一如既往,如果我遗漏了这个问题的任何内容,请告诉我。
编辑
这是堆栈跟踪:
java.io.IOException: Corrupt GZIP trailer
at java.util.zip.GZIPInputStream.readTrailer(GZIPInputStream.java:200)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:92)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at com.weathertx.xmlserver.support.HTTPHelper.doHTTPGet(HTTPHelper.java:60)
at com.weathertx.xmlserver.tests.HttpHelperTest.getAPIResponse(HttpHelperTest.java:47)
at com.weathertx.xmlserver.tests.HttpHelperTest.testHttpGet(HttpHelperTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
最佳答案
问题已经解决。坦率地说,我不太明白为什么它最初不起作用,或者它有什么问题(除了明显过于复杂和不必要的复杂之外)。感谢this solution ,我不知何故错过了我的第一次搜索,我能够通过基本上完全或多或少地实现他们所做的来解决问题。这是我的最终代码:
public static String doHTTPGet(String urlString) throws IOException{
URL weatherAPI = new URL(urlString);
HttpURLConnection apiConnection = (HttpURLConnection) weatherAPI.openConnection();
apiConnection.setRequestMethod("GET");
apiConnection.setRequestProperty("Accept-Encoding", "gzip");
apiConnection.connect();
InputStream gzippedResponse = apiConnection.getInputStream();
InputStream decompressedResponse = new GZIPInputStream(gzippedResponse);
Reader reader = new InputStreamReader(decompressedResponse, "UTF-8");
StringWriter writer = new StringWriter();
char[] buffer = new char[10240];
for(int length = 0; (length = reader.read(buffer)) > 0;){
writer.write(buffer, 0, length);
}
writer.close();
reader.close();
decompressedResponse.close();
gzippedResponse.close();
apiConnection.disconnect();
return writer.toString();
}
所以最终,我不需要通过字节数组流和到处传递数据。除了我原来的方法很复杂之外,如果有人知道为什么我的原始算法在第一次调用此方法后 产生“Corrupted GZIP trailer”错误消息,请务必告诉我。
关于java - GZIPInputStream - 损坏的 GZIP 预告片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329517/
GZIP 的大小限制为 4GB,从 http://www.gzip.org/#faq10 获取上面的链接中提到了一些补丁,可以读取超过 4GB 的文件。 我正在使用 GZIPInputStream 读
我需要清楚地了解 GZIPInputstream 的工作原理。我创建了 FileInputStream 对象并将其传递给 GZIPInputstream 以读取我的 config.tar.gz 文件。
简而言之,问题是每当我添加 GZipInputStream 和 GZipOutputStream 时,我的数据就会损坏。没有它们,一切都可以正常工作。 我有以下代码: public boolean l
我在解压缩文件时遇到了一个奇怪的问题,我正在考虑为此使用字符集 UTF-8。我正在使用 Guava 库。 public static byte[] gzip(final CharSequence cs
我有一个包含拉丁文、西里尔文和中文字符的文本。我尝试使用 GZIPOutputStream 压缩字符串(通过 bytes[])并使用 GZIPInputStream 解压缩它。但我无法将所有角色转换回
我正在尝试将 HTTP 响应的 gzip 压缩正文转换为纯文本。我已获取此响应的字节数组并将其转换为 ByteArrayInputStream。然后我将其转换为 GZIPInputStream。我现在
我有一个 .gz 格式的文件。用于读取此文件的 java 类是 GZIPInputStream。但是,这个类并没有扩展 java 的 BufferedReader 类。结果,我无法逐行读取文件。我需要
我正在尝试使用 GZIPOutputStream 在客户端中对字符串进行编码,然后使用 GZIPOutputStream 在服务器中对字符串进行解码。 客户端代码(在初始套接字连接建立之后)是: //
我正在通过 GZIPInputStream 读取 gzip 压缩文件。我想一次读取大量数据,但无论我要求 GZIPInputStream 读取多少字节,它总是读取少得多的字节数。例如, val bAr
在下面的行中,当instream是GZIPInputStream时,我发现c的值是完全随机的,要么大于或小于1024。但是当instream是FileInputStream时,返回值始终是1024。
我在使用 SharpZipLib 的 GZipInputStream 编写未压缩的 GZIP 流时遇到问题。我似乎只能获得 256 字节的数据,其余数据未写入并归零。已检查压缩流 (compresse
我有一个奇怪的程序,其中缓冲区的 GzipInputStream 零填充部分。我有幸知道字节在流中应该是什么样子,我可以看到缓冲区中填充了 8 个正确的字节和 12 个零(不应为零) 字节应该像这样-
我正在尝试在 Java 中使用 GZIPInputStream 来解压缩来自 .NET 应用程序的图像数据(以 GZIP 格式压缩)。图像数据作为 base64 字符串传输,因为它作为 XML 文本同
作为引用,这是我收到的完整错误: java.io.EOFException: Unexpected end of ZLIB input stream at java.util.zip.Infl
场景是读取一个gzip文件(扩展名为.gz) 知道有 GZIPInputStream 类来处理这个。 这里是将文件对象转换为 GZIPStream 的代码。 FileInputStream fin =
我有一个从另一个 ByteArrayInputStream 构建的 GZIPInputStream。我想知道 gzip 数据的原始(未压缩)长度。虽然我可以读取到GZIPInputStream的末尾,
是否有任何适用于 J2ME 的 GZIPInputStream 实现。我更喜欢 Apache 样式许可证 最佳答案 IIRC phoneME project包括 GZIPInputStream 的实现
我正在尝试将一系列 Long 写入 GZIPOutputStream,希望稍后能解压缩这些数字。 当我尝试使用少量 Long 时,以下程序运行良好,但使用许多 Long 时会抛出异常,例如 (1024
我有一个静态辅助方法,负责从我们的 Rails 应用程序中获取压缩的 JSON 字符串,并在返回 String 表示之前解压缩数据。 我编写了两个 JUnit 测试,一个测试 JSON 是否正确解析,
我使用 Java BufferedReader 对象逐行读取指向有效 GZIP 存档的 GZIPInputStream,该存档包含 1,000 行 ASCII 文本,采用典型的 CSV 格式。代码如下
我是一名优秀的程序员,十分优秀!