Skip to content

Commit 416369b

Browse files
committed
fix npe
1 parent fb98d51 commit 416369b

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public int getStatusCode() {
4646

4747
@Override
4848
public InputStream getContent() throws IOException {
49-
HttpEntity entity = response.getEntity();
5049
InputStream content = entity == null ? null : entity.getContent();
5150
return new Apache5ResponseContent(content, response);
5251
}

google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ public synchronized void reset() throws IOException {
5959

6060
@Override
6161
public void close() throws IOException {
62-
wrappedStream.close();
63-
response.close();
62+
if (wrappedStream != null) {
63+
wrappedStream.close();
64+
}
65+
if (response != null) {
66+
response.close();
67+
}
6468
}
6569

6670
@Override

google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpResponseTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,4 @@ public void testNullContent() throws Exception {
4444

4545
assertNotNull(content);
4646
}
47-
4847
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http.apache.v5;
16+
17+
import org.apache.hc.client5.http.classic.methods.*;
18+
import org.junit.*;
19+
20+
import java.io.*;
21+
22+
import static org.junit.Assert.*;
23+
24+
public class Apache5ResponseContentTest {
25+
@Test
26+
public void testNullResponseContent_doesNotThrowExceptionOnClose() throws Exception {
27+
Apache5ResponseContent response =
28+
new Apache5ResponseContent(
29+
new InputStream() {
30+
@Override
31+
public int read() throws IOException {
32+
return 0;
33+
}
34+
},
35+
null);
36+
37+
response.close();
38+
}
39+
40+
@Test
41+
public void testNullWrappedContent_doesNotThrowExceptionOnClose() throws Exception {
42+
MockClassicHttpResponse mockResponse = new MockClassicHttpResponse();
43+
Apache5ResponseContent response =
44+
new Apache5ResponseContent(
45+
null,
46+
mockResponse);
47+
48+
response.close();
49+
}
50+
}

0 commit comments

Comments
 (0)