|
51 | 51 | import com.google.api.client.http.HttpResponseException;
|
52 | 52 | import com.google.api.client.http.HttpTransport;
|
53 | 53 | import com.google.api.client.http.InputStreamContent;
|
| 54 | +import com.google.api.client.http.LowLevelHttpResponse; |
54 | 55 | import com.google.api.client.http.json.JsonHttpContent;
|
55 | 56 | import com.google.api.client.json.JsonFactory;
|
56 | 57 | import com.google.api.client.json.jackson.JacksonFactory;
|
| 58 | +import com.google.api.client.util.IOUtils; |
57 | 59 | import com.google.api.services.storage.Storage;
|
58 | 60 | import com.google.api.services.storage.Storage.Objects.Get;
|
59 | 61 | import com.google.api.services.storage.Storage.Objects.Insert;
|
|
65 | 67 | import com.google.api.services.storage.model.ObjectAccessControl;
|
66 | 68 | import com.google.api.services.storage.model.Objects;
|
67 | 69 | import com.google.api.services.storage.model.StorageObject;
|
| 70 | +import com.google.cloud.BaseServiceException; |
68 | 71 | import com.google.cloud.storage.StorageException;
|
69 | 72 | import com.google.cloud.storage.StorageOptions;
|
70 | 73 | import com.google.common.base.Function;
|
|
78 | 81 | import java.io.ByteArrayOutputStream;
|
79 | 82 | import java.io.IOException;
|
80 | 83 | import java.io.InputStream;
|
| 84 | +import java.lang.reflect.Field; |
81 | 85 | import java.math.BigInteger;
|
82 | 86 | import java.util.ArrayList;
|
83 | 87 | import java.util.LinkedList;
|
@@ -500,7 +504,24 @@ public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, lo
|
500 | 504 | requestHeaders.setRange(range.toString());
|
501 | 505 | setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, options);
|
502 | 506 | ByteArrayOutputStream output = new ByteArrayOutputStream(bytes);
|
503 |
| - req.executeMedia().download(output); |
| 507 | + HttpResponse httpResponse = req.executeMedia(); |
| 508 | + // todo(mziccard) remove when |
| 509 | + // https://github.com/GoogleCloudPlatform/google-cloud-java/issues/982 is fixed |
| 510 | + String contentEncoding = httpResponse.getContentEncoding(); |
| 511 | + if (contentEncoding != null && contentEncoding.contains("gzip")) { |
| 512 | + try { |
| 513 | + Field responseField = httpResponse.getClass().getDeclaredField("response"); |
| 514 | + responseField.setAccessible(true); |
| 515 | + LowLevelHttpResponse lowLevelHttpResponse = |
| 516 | + (LowLevelHttpResponse) responseField.get(httpResponse); |
| 517 | + IOUtils.copy(lowLevelHttpResponse.getContent(), output); |
| 518 | + } catch (IllegalAccessException|NoSuchFieldException ex) { |
| 519 | + throw new StorageException( |
| 520 | + BaseServiceException.UNKNOWN_CODE, "Error parsing gzip response", ex); |
| 521 | + } |
| 522 | + } else { |
| 523 | + httpResponse.download(output); |
| 524 | + } |
504 | 525 | String etag = req.getLastResponseHeaders().getETag();
|
505 | 526 | return Tuple.of(etag, output.toByteArray());
|
506 | 527 | } catch (IOException ex) {
|
|
0 commit comments