-
Notifications
You must be signed in to change notification settings - Fork 156
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
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0bc2966
add getInputStream to ByteString
pjfanning 98c962f
scala 2 updates
pjfanning 27b883d
rename as asInputStream
pjfanning c720b01
make methods final
pjfanning 2d1b5ce
use different asInputStream implementations on different subclasses
pjfanning 356c46c
Update ByteBufferBackedInputStream.scala
pjfanning 2b0b077
remove default asInputStream impl
pjfanning 5cfa759
add some tests - more needed
pjfanning c7afd7a
Create bytestring-inputstream.excludes
pjfanning e28a96f
scalafmt
pjfanning 6f48a03
Update bytestring-inputstream.excludes
pjfanning e5aac86
Update ByteStringInputStreamSpec.scala
pjfanning 81b0d9f
Update ByteStringInputStreamSpec.scala
pjfanning 62a79ea
Update ByteStringInputStreamSpec.scala
pjfanning c5b28f4
add benchmark
pjfanning 9f3a505
Update ByteString_asInputStream_Benchmark.scala
pjfanning ad2a43e
change to iterator earlier in chain
pjfanning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
actor-tests/src/test/scala/org/apache/pekko/util/ByteStringInputStreamSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
actor/src/main/mima-filters/1.0.x.backwards.excludes/bytestring-inputstream.excludes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 } | ||||||
|
@@ -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 { | ||||||
|
||||||
/** | ||||||
|
@@ -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 */ | ||||||
|
@@ -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 { | ||||||
|
@@ -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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
def decodeString(charset: String): String = compact.decodeString(charset) | ||||||
|
||||||
def decodeString(charset: Charset): String = compact.decodeString(charset) | ||||||
|
@@ -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 | ||||||
|
@@ -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 | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.