aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/http/GzipDecompressingEntity.java
blob: 0407ddeca1b1ca6bad35ff1b5e4c2b5bcdb647c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation.http;

import org.apache.http.HttpEntity;
import org.apache.http.entity.HttpEntityWrapper;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;

/**
 * Used by HTTPSearcher when talking to services returning compressed content.
 *
 * @author Mainak Mandal
 * @deprecated
 */
// TODO: Remove on Vespa 7
@Deprecated // OK
public class GzipDecompressingEntity extends HttpEntityWrapper {

    private static class Resources {

        byte [] buffer;
        int total;

        Resources() {
            total = 0;
            buffer = new byte[65536];
        }
        void drain(InputStream zipStream) throws IOException {
            int numRead = zipStream.read(buffer, total, buffer.length);
            while (numRead != -1) {
                total += numRead;
                if ((total + 65536) > buffer.length) {
                    buffer = Arrays.copyOf(buffer, buffer.length + numRead);
                }
                numRead = zipStream.read(buffer, total, buffer.length - total);
            }
        }

    }

    private final Resources resources = new Resources();

    public GzipDecompressingEntity(final HttpEntity entity) throws IllegalStateException, IOException {
        super(entity);
        GZIPInputStream gz = new GZIPInputStream(entity.getContent());
        InputStream zipStream = new BufferedInputStream(gz);
        try {
            resources.drain(zipStream);
        } catch (IOException e) {
            throw e;
        } finally {
            zipStream.close();
        }
    }

    @Override
    public InputStream getContent() throws IOException, IllegalStateException {

        final ByteBuffer buff = ByteBuffer.wrap(resources.buffer, 0, resources.total);
        return new InputStream() {

            @Override
            public int available() throws IOException {
                return buff.remaining();
            }

            @Override
            public int read() throws IOException {
                if (buff.hasRemaining())
                    return buff.get() & 0xFF;

                return -1;
            }

            @Override
            public int read(byte[] b) throws IOException {
                if (!buff.hasRemaining())
                    return -1;

                int len = b.length;
                if (len > buff.remaining())
                    len = buff.remaining();
                buff.get(b, 0, len);
                return len;
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                if (!buff.hasRemaining())
                    return -1;

                if (len > buff.remaining())
                    len = buff.remaining();
                buff.get(b, off, len);
                return len;
            }

            @Override
            public long skip(long n) throws IOException {
                if (!buff.hasRemaining())
                    return -1;

                if (n > buff.remaining())
                    n = buff.remaining();

                buff.position(buff.position() + (int) n);
                return n;
            }
        };
    }

    @Override
    public long getContentLength() {
        return resources.total;
    }

    @Override
    public void writeTo(OutputStream outstream) throws IOException {
        outstream.write(resources.buffer, 0, resources.total);
    }

}