従来のHttpClient
GZIP 解凍では、すぐに使用できる によって処理されContentCompressionExec
ます。これはどのように行われHttpAsyncClient
ますか?AsyncExecChainHandler
この機能を実装するものは見つかりません。
質問する
120 次
1 に答える
1
AsyncResponseConsumer
私はscalaで以下を自分で実装することになりました。
class SimpleDecompressingResponseConsumer(val entityConsumer: AsyncEntityConsumer[Array[Byte]])
extends AbstractAsyncResponseConsumer[SimpleHttpResponse, Array[Byte]](entityConsumer) {
override def informationResponse(response: HttpResponse, context: HttpContext): Unit = ()
override protected def buildResult(response: HttpResponse, entity: Array[Byte], contentType: ContentType): SimpleHttpResponse = {
val simpleResponse = SimpleHttpResponse.copy(response)
if (entity != null) simpleResponse.setBody(entity, contentType)
simpleResponse
}
}
class SimpleAsyncDecompressingEntityConsumer extends AbstractBinDataConsumer with AsyncEntityConsumer[Array[Byte]] {
@volatile
private var resultCallback: FutureCallback[Array[Byte]] = _
private var encoding: Array[Byte] => Array[Byte] = _
private var content: Array[Byte] = _
private val buffer = new ByteArrayBuffer(1024)
override def streamStart(entityDetails: EntityDetails, resultCallback: FutureCallback[Array[Byte]]): Unit = {
this.resultCallback = resultCallback
this.encoding = entityDetails.getContentEncoding match {
case "gzip" | "x-gzip" =>
bytes => IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(bytes)))
case "deflate" =>
bytes => IOUtils.toByteArray(new DeflateInputStream(new ByteArrayInputStream(bytes)))
case _ =>
identity
}
}
override def failed(cause: Exception): Unit = {
if (resultCallback != null) resultCallback.failed(cause)
releaseResources()
}
override def getContent: Array[Byte] = content
override def capacityIncrement(): Int = Int.MaxValue
override def data(src: ByteBuffer, endOfStream: Boolean): Unit = {
if (src == null) return
if (src.hasArray) buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining())
else while (src.hasRemaining) buffer.append(src.get)
}
override def completed(): Unit = {
this.content = encoding(buffer.toByteArray)
if (resultCallback != null) resultCallback.completed(content)
releaseResources()
}
override def releaseResources(): Unit = buffer.clear()
}
于 2020-04-01T22:13:09.287 に答える