jtjeferreira commented on code in PR #309:
URL: https://github.com/apache/pekko-grpc/pull/309#discussion_r1606612594


##########
runtime/src/main/scala/org/apache/pekko/grpc/internal/ByteStringInputStream.scala:
##########
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * license agreements; and to You under the Apache License, version 2.0:
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * This file is part of the Apache Pekko project, which was derived from Akka.
+ */
+
+/*
+ * Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
+ */
+
+package org.apache.pekko.grpc.internal
+
+import java.io.{ ByteArrayInputStream, InputStream }
+import java.lang.invoke.{ MethodHandles, MethodType }
+
+import scala.util.Try
+
+import org.apache.pekko
+import pekko.annotation.InternalApi
+import pekko.util.ByteString
+import pekko.util.ByteString.ByteString1C
+
+/** INTERNAL API */
+@InternalApi
+private[internal] 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)
+  }

Review Comment:
   actually
   
   ```scala
       def legacyConvert(bs: ByteString): InputStream = bs match {
         case bss: ByteStrings =>
           new 
SequenceInputStream(bss.bytestrings.iterator.map(legacyConvert).asJavaEnumeration)
         case _ =>
           new ByteArrayInputStream(bs.toArrayUnsafe())
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to