Skip to content

Commit 48815e4

Browse files
use different asInputStream implementations on different subclasses
Co-Authored-By: João Ferreira <[email protected]>
1 parent 41e2b81 commit 48815e4

File tree

6 files changed

+101
-14
lines changed

6 files changed

+101
-14
lines changed

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ Copyright 2014 The Netty Project
240240
pekko-actor contains code from java-uuid-generator <https://github.com/cowtowncoder/java-uuid-generator>
241241
in `org.apache.pekko.util.UUIDComparator.scala` which was released under an Apache 2.0 license.
242242

243+
Copyright (c) 2002- Tatu Saloranta, [email protected]
244+
245+
---------------
246+
247+
pekko-actor contains code from jackson-databind <https://github.com/FasterXML/jackson-databind>
248+
in `org.apache.pekko.util.ByteBufferBackedInputStream.scala` which was released under an Apache 2.0 license.
249+
250+
Copyright 2007-, Tatu Saloranta ([email protected])
251+
243252
---------------
244253

245254
pekko-actor contains code in `org.apache.pekko.dispatch.AbstractNodeQueue.java` and in

actor/src/main/scala-2.12/org/apache/pekko/util/ByteString.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
package org.apache.pekko.util
1515

16-
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream }
16+
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
1717
import java.lang.{ Iterable => JIterable }
1818
import java.nio.{ ByteBuffer, ByteOrder }
1919
import java.nio.charset.{ Charset, StandardCharsets }
2020
import java.util.Base64
2121

2222
import scala.annotation.{ tailrec, varargs }
2323
import scala.collection.IndexedSeqOptimized
24+
import scala.collection.JavaConverters._
2425
import scala.collection.generic.CanBuildFrom
2526
import scala.collection.immutable
2627
import scala.collection.immutable.{ IndexedSeq, VectorBuilder }
@@ -271,6 +272,8 @@ object ByteString {
271272
}
272273

273274
override def toArrayUnsafe(): Array[Byte] = bytes
275+
276+
override def asInputStream: InputStream = new ByteArrayInputStream(bytes)
274277
}
275278

276279
/** INTERNAL API: ByteString backed by exactly one array, with start / end markers */
@@ -436,6 +439,9 @@ object ByteString {
436439
if (startIndex == 0 && length == bytes.length) bytes
437440
else toArray
438441
}
442+
443+
override def asInputStream: InputStream =
444+
new ByteArrayInputStream(bytes, startIndex, length)
439445
}
440446

441447
private[pekko] object ByteStrings extends Companion {
@@ -566,6 +572,9 @@ object ByteString {
566572

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

575+
override def asInputStream: InputStream =
576+
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
577+
569578
def decodeString(charset: String): String = compact.decodeString(charset)
570579

571580
def decodeString(charset: Charset): String = compact.decodeString(charset)
@@ -834,7 +843,8 @@ sealed abstract class ByteString extends IndexedSeq[Byte] with IndexedSeqOptimiz
834843
* @see [[asByteBuffer]]
835844
* @since 1.1.0
836845
*/
837-
final def asInputStream: InputStream = new ByteArrayInputStream(toArrayUnsafe())
846+
def asInputStream: InputStream =
847+
new SequenceInputStream(asByteBuffers.map(bb => new ByteBufferBackedInputStream(bb)).iterator.asJavaEnumeration)
838848

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

@@ -895,7 +905,6 @@ sealed abstract class ByteString extends IndexedSeq[Byte] with IndexedSeqOptimiz
895905
* all fragments. Will always have at least one entry.
896906
*/
897907
def getByteBuffers(): JIterable[ByteBuffer] = {
898-
import scala.collection.JavaConverters.asJavaIterableConverter
899908
asByteBuffers.asJava
900909
}
901910

actor/src/main/scala-2.13/org/apache/pekko/util/ByteString.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313

1414
package org.apache.pekko.util
1515

16-
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream }
16+
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
1717
import java.lang.{ Iterable => JIterable }
1818
import java.nio.{ ByteBuffer, ByteOrder }
1919
import java.nio.charset.{ Charset, StandardCharsets }
2020
import java.util.Base64
21-
import scala.annotation.{ tailrec, varargs }
21+
import scala.annotation.{ nowarn, tailrec, varargs }
2222
import scala.collection.{ immutable, mutable }
2323
import scala.collection.immutable.{ IndexedSeq, IndexedSeqOps, StrictOptimizedSeqOps, VectorBuilder }
2424
import scala.collection.mutable.{ Builder, WrappedArray }
25+
import scala.jdk.CollectionConverters._
2526
import scala.reflect.ClassTag
26-
import scala.annotation.nowarn
2727

2828
object ByteString {
2929

@@ -278,6 +278,7 @@ object ByteString {
278278

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

281+
override def asInputStream: InputStream = new ByteArrayInputStream(bytes)
281282
}
282283

283284
/** INTERNAL API: ByteString backed by exactly one array, with start / end markers */
@@ -448,6 +449,9 @@ object ByteString {
448449
if (startIndex == 0 && length == bytes.length) bytes
449450
else toArray
450451
}
452+
453+
override def asInputStream: InputStream =
454+
new ByteArrayInputStream(bytes, startIndex, length)
451455
}
452456

453457
private[pekko] object ByteStrings extends Companion {
@@ -578,6 +582,9 @@ object ByteString {
578582

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

585+
override def asInputStream: InputStream =
586+
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
587+
581588
def decodeString(charset: String): String = compact.decodeString(charset)
582589

583590
def decodeString(charset: Charset): String = compact.decodeString(charset)
@@ -883,7 +890,8 @@ sealed abstract class ByteString
883890
* @see [[asByteBuffer]]
884891
* @since 1.1.0
885892
*/
886-
final def asInputStream: InputStream = new ByteArrayInputStream(toArrayUnsafe())
893+
def asInputStream: InputStream =
894+
new SequenceInputStream(asByteBuffers.map(bb => new ByteBufferBackedInputStream(bb)).iterator.asJavaEnumeration)
887895

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

@@ -940,9 +948,7 @@ sealed abstract class ByteString
940948
* Java API: Returns an Iterable of read-only ByteBuffers that directly wraps this ByteStrings
941949
* all fragments. Will always have at least one entry.
942950
*/
943-
@nowarn
944951
def getByteBuffers(): JIterable[ByteBuffer] = {
945-
import scala.collection.JavaConverters.asJavaIterableConverter
946952
asByteBuffers.asJava
947953
}
948954

actor/src/main/scala-3/org/apache/pekko/util/ByteString.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313

1414
package org.apache.pekko.util
1515

16-
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream }
16+
import java.io.{ ByteArrayInputStream, InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
1717
import java.lang.{ Iterable => JIterable }
1818
import java.nio.{ ByteBuffer, ByteOrder }
1919
import java.nio.charset.{ Charset, StandardCharsets }
2020
import java.util.Base64
2121

22-
import scala.annotation.{ nowarn, tailrec, varargs }
22+
import scala.annotation.{ tailrec, varargs }
2323
import scala.collection.{ immutable, mutable }
2424
import scala.collection.immutable.{ IndexedSeq, IndexedSeqOps, StrictOptimizedSeqOps, VectorBuilder }
2525
import scala.collection.mutable.{ Builder, WrappedArray }
26+
import scala.jdk.CollectionConverters._
2627
import scala.reflect.ClassTag
2728

2829
object ByteString {
@@ -277,6 +278,8 @@ object ByteString {
277278
}
278279

279280
override def toArrayUnsafe(): Array[Byte] = bytes
281+
282+
override def asInputStream: InputStream = new ByteArrayInputStream(bytes)
280283
}
281284

282285
/** INTERNAL API: ByteString backed by exactly one array, with start / end markers */
@@ -447,6 +450,9 @@ object ByteString {
447450
if (startIndex == 0 && length == bytes.length) bytes
448451
else toArray
449452
}
453+
454+
override def asInputStream: InputStream =
455+
new ByteArrayInputStream(bytes, startIndex, length)
450456
}
451457

452458
private[pekko] object ByteStrings extends Companion {
@@ -577,6 +583,9 @@ object ByteString {
577583

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

586+
override def asInputStream: InputStream =
587+
new SequenceInputStream(bytestrings.map(_.asInputStream).iterator.asJavaEnumeration)
588+
580589
def decodeString(charset: String): String = compact.decodeString(charset)
581590

582591
def decodeString(charset: Charset): String = compact.decodeString(charset)
@@ -881,7 +890,8 @@ sealed abstract class ByteString
881890
* @see [[asByteBuffer]]
882891
* @since 1.1.0
883892
*/
884-
final def asInputStream: InputStream = new ByteArrayInputStream(toArrayUnsafe())
893+
def asInputStream: InputStream =
894+
new SequenceInputStream(asByteBuffers.map(bb => new ByteBufferBackedInputStream(bb)).iterator.asJavaEnumeration)
885895

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

@@ -938,9 +948,7 @@ sealed abstract class ByteString
938948
* Java API: Returns an Iterable of read-only ByteBuffers that directly wraps this ByteStrings
939949
* all fragments. Will always have at least one entry.
940950
*/
941-
@nowarn
942951
def getByteBuffers(): JIterable[ByteBuffer] = {
943-
import scala.collection.JavaConverters.asJavaIterableConverter
944952
asByteBuffers.asJava
945953
}
946954

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.pekko.util
19+
20+
import java.io.{ IOException, InputStream }
21+
import java.nio.ByteBuffer
22+
23+
/**
24+
* Simple {@link InputStream} implementation that exposes currently
25+
* available content of a {@link ByteBuffer}.
26+
*
27+
* Derived from https://github.com/FasterXML/jackson-databind/blob/1e73db1fabd181937c68b49ffc502fb7f614d0c2/src/main/java/com/fasterxml/jackson/databind/util/ByteBufferBackedInputStream.java
28+
*/
29+
private[util] class ByteBufferBackedInputStream(bb: ByteBuffer) extends InputStream {
30+
override def available: Int = bb.remaining
31+
32+
@throws[IOException]
33+
override def read: Int = if (bb.hasRemaining) bb.get & 0xFF
34+
else -1
35+
36+
@throws[IOException]
37+
override def read(bytes: Array[Byte], off: Int, len: Int): Int = {
38+
if (!bb.hasRemaining) {
39+
-1
40+
} else {
41+
val newLen = Math.min(len, bb.remaining)
42+
bb.get(bytes, off, newLen)
43+
newLen
44+
}
45+
}
46+
}

legal/pekko-actor-jar-license.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ Copyright 2014 The Netty Project
240240
pekko-actor contains code from java-uuid-generator <https://github.com/cowtowncoder/java-uuid-generator>
241241
in `org.apache.pekko.util.UUIDComparator.scala` which was released under an Apache 2.0 license.
242242

243+
Copyright (c) 2002- Tatu Saloranta, [email protected]
244+
245+
---------------
246+
247+
pekko-actor contains code from jackson-databind <https://github.com/FasterXML/jackson-databind>
248+
in `org.apache.pekko.util.ByteBufferBackedInputStream.scala` which was released under an Apache 2.0 license.
249+
250+
Copyright 2007-, Tatu Saloranta ([email protected])
251+
243252
---------------
244253

245254
pekko-actor contains code in `org.apache.pekko.dispatch.AbstractNodeQueue.java` and in

0 commit comments

Comments
 (0)