Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add asInputStream to ByteString #1085

Merged
merged 17 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/

package org.apache.pekko.util

import java.io.{ ByteArrayOutputStream, InputStream, OutputStream }
import java.nio.charset.StandardCharsets

import org.apache.pekko
import pekko.util.ByteString.{ ByteString1, ByteString1C, ByteStrings }

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class ByteStringInputStreamSpec extends AnyWordSpec with Matchers {
"ByteString1" must {
"support asInputStream" in {
ByteString1.empty.asInputStream.read() shouldEqual -1
ByteString1.empty.asInputStream.read(Array.empty) shouldEqual -1
toUtf8String(ByteString1.empty.asInputStream) shouldEqual ""
toUtf8String(ByteString1.fromString("abc").asInputStream) shouldEqual "abc"
}
}
"ByteString1C" must {
"support asInputStream" in {
toUtf8String(ByteString1C.fromString("").asInputStream) shouldEqual ""
toUtf8String(ByteString1C.fromString("abc").asInputStream) shouldEqual "abc"
val bytes = "abcdef".getBytes(StandardCharsets.US_ASCII)
toUtf8String(ByteString.fromArray(bytes, 1, 3).asInputStream) shouldEqual "bcd"
}
}
"ByteStrings" must {
"support asInputStream" in {
val empty = ByteStrings(ByteString1.fromString(""), ByteString1.fromString(""))
empty.asInputStream.read() shouldEqual -1
empty.asInputStream.read(Array.empty) shouldEqual -1
toUtf8String(empty.asInputStream) shouldEqual ""
val abc = ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("bc"))
toUtf8String(abc.asInputStream) shouldEqual "abc"
}
}

private def toUtf8String(input: InputStream): String =
new String(toByteArray(input), StandardCharsets.UTF_8)

private def toByteArray(input: InputStream): Array[Byte] = {
val output = new ByteArrayOutputStream
try {
copy(input, output)
output.toByteArray
} finally {
output.close()
}
}

private def copy(input: InputStream, output: OutputStream): Int = {
val buffer = new Array[Byte](4096)
var count = 0
var n = input.read(buffer)
while (n != -1) {
output.write(buffer, 0, n)
count += n
n = input.read(buffer)
}
count
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

# Add ByteString.asInputStream
ProblemFilters.exclude[ReversedMissingMethodProblem]("org.apache.pekko.util.ByteString.asInputStream")
21 changes: 19 additions & 2 deletions actor/src/main/scala-2.12/org/apache/pekko/util/ByteString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

package org.apache.pekko.util

import java.io.{ ObjectInputStream, ObjectOutputStream }
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
import java.lang.{ Iterable => JIterable }
import java.nio.{ ByteBuffer, ByteOrder }
import java.nio.charset.{ Charset, StandardCharsets }
import java.util.Base64

import scala.annotation.{ tailrec, varargs }
import scala.collection.IndexedSeqOptimized
import scala.collection.JavaConverters._
import scala.collection.generic.CanBuildFrom
import scala.collection.immutable
import scala.collection.immutable.{ IndexedSeq, VectorBuilder }
Expand Down Expand Up @@ -271,6 +272,8 @@ object ByteString {
}

override def toArrayUnsafe(): Array[Byte] = bytes

override def asInputStream: InputStream = new ByteArrayInputStream(bytes)
}

/** INTERNAL API: ByteString backed by exactly one array, with start / end markers */
Expand Down Expand Up @@ -436,6 +439,9 @@ object ByteString {
if (startIndex == 0 && length == bytes.length) bytes
else toArray
}

override def asInputStream: InputStream =
new ByteArrayInputStream(bytes, startIndex, length)
}

private[pekko] object ByteStrings extends Companion {
Expand Down Expand Up @@ -566,6 +572,9 @@ object ByteString {

def asByteBuffers: scala.collection.immutable.Iterable[ByteBuffer] = bytestrings.map { _.asByteBuffer }

override def asInputStream: InputStream =
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytestrings.iterator.map(_.asInputString).asJavaEnumeration

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@pjfanning I will send a suggestion tomorrow about this line, currently on my phone.

pjfanning marked this conversation as resolved.
Show resolved Hide resolved

def decodeString(charset: String): String = compact.decodeString(charset)

def decodeString(charset: Charset): String = compact.decodeString(charset)
Expand Down Expand Up @@ -827,6 +836,15 @@ sealed abstract class ByteString extends IndexedSeq[Byte] with IndexedSeqOptimiz
*/
def toArrayUnsafe(): Array[Byte] = toArray

/**
* Return the bytes in this ByteString as an InputStream.
*
* @return the bytes in this ByteString accessible as an InputStream
* @see [[asByteBuffer]]
* @since 1.1.0
*/
def asInputStream: InputStream

override def foreach[@specialized U](f: Byte => U): Unit = iterator.foreach(f)

private[pekko] def writeToOutputStream(os: ObjectOutputStream): Unit
Expand Down Expand Up @@ -886,7 +904,6 @@ sealed abstract class ByteString extends IndexedSeq[Byte] with IndexedSeqOptimiz
* all fragments. Will always have at least one entry.
*/
def getByteBuffers(): JIterable[ByteBuffer] = {
import scala.collection.JavaConverters.asJavaIterableConverter
asByteBuffers.asJava
}

Expand Down
24 changes: 19 additions & 5 deletions actor/src/main/scala-2.13/org/apache/pekko/util/ByteString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@

package org.apache.pekko.util

import java.io.{ ObjectInputStream, ObjectOutputStream }
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
import java.lang.{ Iterable => JIterable }
import java.nio.{ ByteBuffer, ByteOrder }
import java.nio.charset.{ Charset, StandardCharsets }
import java.util.Base64
import scala.annotation.{ tailrec, varargs }
import scala.annotation.{ nowarn, tailrec, varargs }
import scala.collection.{ immutable, mutable }
import scala.collection.immutable.{ IndexedSeq, IndexedSeqOps, StrictOptimizedSeqOps, VectorBuilder }
import scala.collection.mutable.{ Builder, WrappedArray }
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag
import scala.annotation.nowarn

object ByteString {

Expand Down Expand Up @@ -278,6 +278,7 @@ object ByteString {

override def toArrayUnsafe(): Array[Byte] = bytes

override def asInputStream: InputStream = new ByteArrayInputStream(bytes)
}

/** INTERNAL API: ByteString backed by exactly one array, with start / end markers */
Expand Down Expand Up @@ -448,6 +449,9 @@ object ByteString {
if (startIndex == 0 && length == bytes.length) bytes
else toArray
}

override def asInputStream: InputStream =
new ByteArrayInputStream(bytes, startIndex, length)
}

private[pekko] object ByteStrings extends Companion {
Expand Down Expand Up @@ -578,6 +582,9 @@ object ByteString {

def asByteBuffers: scala.collection.immutable.Iterable[ByteBuffer] = bytestrings.map { _.asByteBuffer }

override def asInputStream: InputStream =
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
pjfanning marked this conversation as resolved.
Show resolved Hide resolved

def decodeString(charset: String): String = compact.decodeString(charset)

def decodeString(charset: Charset): String = compact.decodeString(charset)
Expand Down Expand Up @@ -876,6 +883,15 @@ sealed abstract class ByteString
*/
def toArrayUnsafe(): Array[Byte] = toArray

/**
* Return the bytes in this ByteString as an InputStream.
*
* @return the bytes in this ByteString accessible as an InputStream
* @see [[asByteBuffer]]
* @since 1.1.0
*/
def asInputStream: InputStream

override def foreach[@specialized U](f: Byte => U): Unit = iterator.foreach(f)

private[pekko] def writeToOutputStream(os: ObjectOutputStream): Unit
Expand Down Expand Up @@ -931,9 +947,7 @@ sealed abstract class ByteString
* Java API: Returns an Iterable of read-only ByteBuffers that directly wraps this ByteStrings
* all fragments. Will always have at least one entry.
*/
@nowarn
def getByteBuffers(): JIterable[ByteBuffer] = {
import scala.collection.JavaConverters.asJavaIterableConverter
asByteBuffers.asJava
}

Expand Down
24 changes: 19 additions & 5 deletions actor/src/main/scala-3/org/apache/pekko/util/ByteString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package org.apache.pekko.util

import java.io.{ ObjectInputStream, ObjectOutputStream }
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
import java.lang.{ Iterable => JIterable }
import java.nio.{ ByteBuffer, ByteOrder }
import java.nio.charset.{ Charset, StandardCharsets }
Expand All @@ -23,10 +23,9 @@ import scala.annotation.{ tailrec, varargs }
import scala.collection.{ immutable, mutable }
import scala.collection.immutable.{ IndexedSeq, IndexedSeqOps, StrictOptimizedSeqOps, VectorBuilder }
import scala.collection.mutable.{ Builder, WrappedArray }
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag

import scala.annotation.nowarn

object ByteString {

/**
Expand Down Expand Up @@ -279,6 +278,8 @@ object ByteString {
}

override def toArrayUnsafe(): Array[Byte] = bytes

override def asInputStream: InputStream = new ByteArrayInputStream(bytes)
}

/** INTERNAL API: ByteString backed by exactly one array, with start / end markers */
Expand Down Expand Up @@ -449,6 +450,9 @@ object ByteString {
if (startIndex == 0 && length == bytes.length) bytes
else toArray
}

override def asInputStream: InputStream =
new ByteArrayInputStream(bytes, startIndex, length)
}

private[pekko] object ByteStrings extends Companion {
Expand Down Expand Up @@ -579,6 +583,9 @@ object ByteString {

def asByteBuffers: scala.collection.immutable.Iterable[ByteBuffer] = bytestrings.map { _.asByteBuffer }

override def asInputStream: InputStream =
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
new SequenceInputStream(bytestrings.iterator.map(_.asInputStream).asJavaEnumeration)


def decodeString(charset: String): String = compact.decodeString(charset)

def decodeString(charset: Charset): String = compact.decodeString(charset)
Expand Down Expand Up @@ -876,6 +883,15 @@ sealed abstract class ByteString
*/
def toArrayUnsafe(): Array[Byte] = toArray

/**
* Return the bytes in this ByteString as an InputStream.
*
* @return the bytes in this ByteString accessible as an InputStream
* @see [[asByteBuffer]]
* @since 1.1.0
*/
def asInputStream: InputStream

override def foreach[@specialized U](f: Byte => U): Unit = iterator.foreach(f)

private[pekko] def writeToOutputStream(os: ObjectOutputStream): Unit
Expand Down Expand Up @@ -931,9 +947,7 @@ sealed abstract class ByteString
* Java API: Returns an Iterable of read-only ByteBuffers that directly wraps this ByteStrings
* all fragments. Will always have at least one entry.
*/
@nowarn
def getByteBuffers(): JIterable[ByteBuffer] = {
import scala.collection.JavaConverters.asJavaIterableConverter
asByteBuffers.asJava
}

Expand Down
2 changes: 1 addition & 1 deletion bench-jmh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Pekko uses [sbt-jmh](https://github.com/sbt/sbt-jmh) to integrate [Java Microben
```shell
sbt shell
pekko > project bench-jmh
sbt:bench-jmh> Jmh/run -i 3 -wi 3 -f 1 .*ActorCreationBenchmark
sbt:pekko-bench-jmh> Jmh/run -i 3 -wi 3 -f 1 .*ActorCreationBenchmark
```

or execute in one-line command
Expand Down
Loading
Loading