-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add asInputStream to ByteString (#1085)
* add getInputStream to ByteString * scala 2 updates * rename as asInputStream * make methods final * use different asInputStream implementations on different subclasses Co-Authored-By: João Ferreira <[email protected]> * Update ByteBufferBackedInputStream.scala scalafmt * remove default asInputStream impl * add some tests - more needed * Create bytestring-inputstream.excludes * scalafmt * Update bytestring-inputstream.excludes * Update ByteStringInputStreamSpec.scala * Update ByteStringInputStreamSpec.scala * Update ByteStringInputStreamSpec.scala * add benchmark * Update ByteString_asInputStream_Benchmark.scala * change to iterator earlier in chain --------- Co-authored-by: João Ferreira <[email protected]>
- Loading branch information
1 parent
e26c6f6
commit ef628ea
Showing
7 changed files
with
256 additions
and
13 deletions.
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
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.