[HTTPCLIENT-1845]: Extract InputStreamFactory classes out of GzipDecompressingEntity and DeflateDecompressingEntity for reuse and to create less garbage.
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1794410 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
53bed5b13e
commit
07aea2cbb9
|
@ -1,3 +1,11 @@
|
||||||
|
Release 5.0-ALPHA3
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
* [HTTPCLIENT-1845]: Extract InputStreamFactory classes out of GzipDecompressingEntity and
|
||||||
|
DeflateDecompressingEntity for reuse and to create less garbage.
|
||||||
|
Contributed by Gary Gregory <ggregory at apache.org>
|
||||||
|
|
||||||
|
|
||||||
Release 5.0-ALPHA2
|
Release 5.0-ALPHA2
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,6 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.hc.client5.http.entity;
|
package org.apache.hc.client5.http.entity;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import org.apache.hc.core5.http.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,14 +54,7 @@ public class DeflateDecompressingEntity extends DecompressingEntity {
|
||||||
* a non-null {@link HttpEntity} to be wrapped
|
* a non-null {@link HttpEntity} to be wrapped
|
||||||
*/
|
*/
|
||||||
public DeflateDecompressingEntity(final HttpEntity entity) {
|
public DeflateDecompressingEntity(final HttpEntity entity) {
|
||||||
super(entity, new InputStreamFactory() {
|
super(entity, DeflateInputStreamFactory.getInstance());
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream create(final InputStream instream) throws IOException {
|
|
||||||
return new DeflateInputStream(instream);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.hc.client5.http.entity;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.apache.hc.core5.annotation.Contract;
|
||||||
|
import org.apache.hc.core5.annotation.ThreadingBehavior;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link InputStreamFactory} for handling Deflate Content Coded responses.
|
||||||
|
*
|
||||||
|
* @since 5.0
|
||||||
|
*/
|
||||||
|
@Contract(threading = ThreadingBehavior.IMMUTABLE)
|
||||||
|
public class DeflateInputStreamFactory implements InputStreamFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton instance.
|
||||||
|
*/
|
||||||
|
private static final DeflateInputStreamFactory INSTANCE = new DeflateInputStreamFactory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the singleton instance.
|
||||||
|
*
|
||||||
|
* @return the singleton instance.
|
||||||
|
*/
|
||||||
|
public static DeflateInputStreamFactory getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream create(final InputStream inputStream) throws IOException {
|
||||||
|
return new DeflateInputStream(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.hc.client5.http.entity;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
|
import org.apache.hc.core5.annotation.Contract;
|
||||||
|
import org.apache.hc.core5.annotation.ThreadingBehavior;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link InputStreamFactory} for handling GZIPContent Coded responses.
|
||||||
|
*
|
||||||
|
* @since 5.0
|
||||||
|
*/
|
||||||
|
@Contract(threading = ThreadingBehavior.IMMUTABLE)
|
||||||
|
public class GZIPInputStreamFactory implements InputStreamFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton instance.
|
||||||
|
*/
|
||||||
|
private static final GZIPInputStreamFactory INSTANCE = new GZIPInputStreamFactory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the singleton instance.
|
||||||
|
*
|
||||||
|
* @return the singleton instance.
|
||||||
|
*/
|
||||||
|
public static GZIPInputStreamFactory getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream create(final InputStream inputStream) throws IOException {
|
||||||
|
return new GZIPInputStream(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -26,36 +26,25 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.hc.client5.http.entity;
|
package org.apache.hc.client5.http.entity;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
|
|
||||||
import org.apache.hc.core5.http.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.apache.hc.core5.http.io.entity.HttpEntityWrapper} for handling gzip
|
* {@link org.apache.hc.core5.http.io.entity.HttpEntityWrapper} for handling
|
||||||
* Content Coded responses.
|
* gzip Content Coded responses.
|
||||||
*
|
*
|
||||||
* @since 4.1
|
* @since 4.1
|
||||||
*/
|
*/
|
||||||
public class GzipDecompressingEntity extends DecompressingEntity {
|
public class GzipDecompressingEntity extends DecompressingEntity {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@link GzipDecompressingEntity} which will wrap the specified
|
* Creates a new {@link GzipDecompressingEntity} which will wrap the
|
||||||
* {@link HttpEntity}.
|
* specified {@link HttpEntity}.
|
||||||
*
|
*
|
||||||
* @param entity
|
* @param entity
|
||||||
* the non-null {@link HttpEntity} to be wrapped
|
* the non-null {@link HttpEntity} to be wrapped
|
||||||
*/
|
*/
|
||||||
public GzipDecompressingEntity(final HttpEntity entity) {
|
public GzipDecompressingEntity(final HttpEntity entity) {
|
||||||
super(entity, new InputStreamFactory() {
|
super(entity, GZIPInputStreamFactory.getInstance());
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream create(final InputStream instream) throws IOException {
|
|
||||||
return new GZIPInputStream(instream);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,15 +28,14 @@
|
||||||
package org.apache.hc.client5.http.impl.sync;
|
package org.apache.hc.client5.http.impl.sync;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
|
|
||||||
import org.apache.hc.client5.http.config.RequestConfig;
|
import org.apache.hc.client5.http.config.RequestConfig;
|
||||||
import org.apache.hc.client5.http.entity.DecompressingEntity;
|
import org.apache.hc.client5.http.entity.DecompressingEntity;
|
||||||
import org.apache.hc.client5.http.entity.DeflateInputStream;
|
import org.apache.hc.client5.http.entity.DeflateInputStreamFactory;
|
||||||
|
import org.apache.hc.client5.http.entity.GZIPInputStreamFactory;
|
||||||
import org.apache.hc.client5.http.entity.InputStreamFactory;
|
import org.apache.hc.client5.http.entity.InputStreamFactory;
|
||||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||||
import org.apache.hc.client5.http.sync.ExecChain;
|
import org.apache.hc.client5.http.sync.ExecChain;
|
||||||
|
@ -68,23 +67,6 @@ import org.apache.hc.core5.util.Args;
|
||||||
@Contract(threading = ThreadingBehavior.IMMUTABLE)
|
@Contract(threading = ThreadingBehavior.IMMUTABLE)
|
||||||
public final class ContentCompressionExec implements ExecChainHandler {
|
public final class ContentCompressionExec implements ExecChainHandler {
|
||||||
|
|
||||||
private final static InputStreamFactory GZIP = new InputStreamFactory() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream create(final InputStream instream) throws IOException {
|
|
||||||
return new GZIPInputStream(instream);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final static InputStreamFactory DEFLATE = new InputStreamFactory() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream create(final InputStream instream) throws IOException {
|
|
||||||
return new DeflateInputStream(instream);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
private final List<String> acceptEncoding;
|
private final List<String> acceptEncoding;
|
||||||
private final Lookup<InputStreamFactory> decoderRegistry;
|
private final Lookup<InputStreamFactory> decoderRegistry;
|
||||||
private final boolean ignoreUnknown;
|
private final boolean ignoreUnknown;
|
||||||
|
@ -96,9 +78,9 @@ public final class ContentCompressionExec implements ExecChainHandler {
|
||||||
this.acceptEncoding = acceptEncoding != null ? acceptEncoding : Arrays.asList("gzip", "x-gzip", "deflate");
|
this.acceptEncoding = acceptEncoding != null ? acceptEncoding : Arrays.asList("gzip", "x-gzip", "deflate");
|
||||||
this.decoderRegistry = decoderRegistry != null ? decoderRegistry :
|
this.decoderRegistry = decoderRegistry != null ? decoderRegistry :
|
||||||
RegistryBuilder.<InputStreamFactory>create()
|
RegistryBuilder.<InputStreamFactory>create()
|
||||||
.register("gzip", GZIP)
|
.register("gzip", GZIPInputStreamFactory.getInstance())
|
||||||
.register("x-gzip", GZIP)
|
.register("x-gzip", GZIPInputStreamFactory.getInstance())
|
||||||
.register("deflate", DEFLATE)
|
.register("deflate", DeflateInputStreamFactory.getInstance())
|
||||||
.build();
|
.build();
|
||||||
this.ignoreUnknown = ignoreUnknown;
|
this.ignoreUnknown = ignoreUnknown;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue