Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GZIPEncoding |
|
| 0.0;0 | ||||
GZIPEncoding$GZIPDecompressingEntity |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2003-2008 the original author or authors. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | * | |
16 | * You are receiving this code free of charge, which represents many hours of | |
17 | * effort from other individuals and corporations. As a responsible member | |
18 | * of the community, you are asked (but not required) to donate any | |
19 | * enhancements or improvements back to the community under a similar open | |
20 | * source license. Thank you. -TMN | |
21 | */ | |
22 | package groovyx.net.http; | |
23 | ||
24 | import static groovyx.net.http.ContentEncoding.Type.GZIP; | |
25 | ||
26 | import java.io.IOException; | |
27 | import java.io.InputStream; | |
28 | import java.util.zip.GZIPInputStream; | |
29 | ||
30 | import org.apache.http.HttpEntity; | |
31 | import org.apache.http.entity.HttpEntityWrapper; | |
32 | ||
33 | /** | |
34 | * Content encoding used to handle GZIP responses. | |
35 | * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a> | |
36 | */ | |
37 | 36 | public class GZIPEncoding extends ContentEncoding { |
38 | ||
39 | /** | |
40 | * Returns the {@link Type#GZIP} encoding string which is added to the | |
41 | * <code>Accept</code> header by the base class. | |
42 | */ | |
43 | @Override | |
44 | public String getContentEncoding() { | |
45 | 128 | return GZIP.toString(); |
46 | } | |
47 | ||
48 | /** | |
49 | * Wraps the raw entity in a {@link GZIPDecompressingEntity}. | |
50 | */ | |
51 | @Override | |
52 | public HttpEntity wrapResponseEntity( HttpEntity raw ) { | |
53 | 16 | return new GZIPDecompressingEntity( raw ); |
54 | } | |
55 | ||
56 | /** | |
57 | * Entity used to interpret a GZIP-encoded response | |
58 | * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a> | |
59 | */ | |
60 | 36 | protected static class GZIPDecompressingEntity extends HttpEntityWrapper { |
61 | ||
62 | public GZIPDecompressingEntity(final HttpEntity entity) { | |
63 | 16 | super(entity); |
64 | 16 | } |
65 | ||
66 | /** | |
67 | * returns a {@link GZIPInputStream} which wraps the original entity's | |
68 | * content stream | |
69 | * @see HttpEntity#getContent() | |
70 | */ | |
71 | @Override | |
72 | public InputStream getContent() throws IOException, IllegalStateException { | |
73 | 6 | return new GZIPInputStream( wrappedEntity.getContent() ); |
74 | } | |
75 | ||
76 | /** | |
77 | * @return -1 | |
78 | */ | |
79 | @Override | |
80 | public long getContentLength() { | |
81 | // length of un-gzipped content is not known | |
82 | 16 | return -1; |
83 | } | |
84 | } | |
85 | } |