Skip to content

Commit

Permalink
Revert "use optimised ByteString.asInputStream (apache#539)" (apache#550
Browse files Browse the repository at this point in the history
)

This reverts commit 03f1289.
  • Loading branch information
pjfanning authored May 10, 2024
1 parent dd62ec7 commit 708287b
Showing 1 changed file with 9 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package org.apache.pekko.http.impl.engine.http2.hpack

import java.io.{ ByteArrayInputStream, InputStream }
import java.lang.invoke.{ MethodHandles, MethodType }

import scala.util.Try

import org.apache.pekko
import pekko.annotation.InternalApi
Expand All @@ -27,32 +24,13 @@ import pekko.util.ByteString.ByteString1C
@InternalApi
private[http2] object ByteStringInputStream {

private val byteStringInputStreamMethodTypeOpt = Try {
val lookup = MethodHandles.publicLookup()
val inputStreamMethodType = MethodType.methodType(classOf[InputStream])
lookup.findVirtual(classOf[ByteString], "asInputStream", inputStreamMethodType)
}.toOption

def apply(bs: ByteString): InputStream = bs match {
case cs: ByteString1C =>
getInputStreamUnsafe(cs)
case _ =>
if (byteStringInputStreamMethodTypeOpt.isDefined) {
byteStringInputStreamMethodTypeOpt.get.invoke(bs).asInstanceOf[InputStream]
} else {
legacyConvert(bs.compact)
}
}

private def legacyConvert(bs: ByteString): InputStream = bs match {
case cs: ByteString1C =>
getInputStreamUnsafe(cs)
case _ =>
// NOTE: We actually measured recently, and compact + use array was pretty good usually
legacyConvert(bs.compact)
}

private def getInputStreamUnsafe(bs: ByteString): InputStream =
new ByteArrayInputStream(bs.toArrayUnsafe())

def apply(bs: ByteString): InputStream =
bs match {
case cs: ByteString1C =>
// TODO optimise, ByteString needs to expose InputStream (esp if array backed, nice!)
new ByteArrayInputStream(cs.toArrayUnsafe())
case _ =>
// NOTE: We actually measured recently, and compact + use array was pretty good usually
apply(bs.compact)
}
}

0 comments on commit 708287b

Please sign in to comment.