Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Copyright EPFL and Lightbend, Inc.
pekko-actor contains code from Netty which was released under an Apache 2.0 license.
Copyright 2014 The Netty Project
- actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala
- actor/src/main/scala/org/apache/pekko/util/ByteString.scala
- actor/src/main/scala/org/apache/pekko/util/SWARUtil.scala

---------------
Expand Down
119 changes: 94 additions & 25 deletions actor/src/main/scala/org/apache/pekko/util/ByteString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
*/

/*
* Copyright 2012 The Netty Project
*
* The Netty Project 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:
*
* https://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.{ InputStream, ObjectInputStream, ObjectOutputStream, SequenceInputStream }
Expand Down Expand Up @@ -314,6 +329,31 @@ object ByteString {
else -1
}

// Derived from code in Netty
// https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L366-L408
override private[util] def bytesMatch(fromIndex: Int, checkBytes: Array[Byte], bytesFromIndex: Int,
checkLength: Int): Boolean = {
var aIndex = fromIndex
var bIndex = bytesFromIndex
val longCount = checkLength >>> 3
val byteCount = checkLength & 7
var i = 0
while (i < longCount) {
if (SWARUtil.getLong(bytes, aIndex) != SWARUtil.getLong(checkBytes, bIndex)) return false
aIndex += 8
bIndex += 8
i += 1
}
i = 0
while (i < byteCount) {
if (bytes(aIndex) != checkBytes(bIndex)) return false
aIndex += 1
bIndex += 1
i += 1
}
true
}

override def slice(from: Int, until: Int): ByteString =
if (from <= 0 && until >= length) this
else if (from >= length || until <= 0 || from >= until) ByteString.empty
Expand Down Expand Up @@ -575,6 +615,33 @@ object ByteString {
else -1
}

// Derived from code in Netty
// https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L366-L408
override private[util] def bytesMatch(fromIndex: Int,
checkBytes: Array[Byte],
bytesFromIndex: Int,
checkLength: Int): Boolean = {
var aIndex = fromIndex + startIndex
var bIndex = bytesFromIndex
val longCount = checkLength >>> 3
val byteCount = checkLength & 7
var i = 0
while (i < longCount) {
if (SWARUtil.getLong(bytes, aIndex) != SWARUtil.getLong(checkBytes, bIndex)) return false
aIndex += 8
bIndex += 8
i += 1
}
i = 0
while (i < byteCount) {
if (bytes(aIndex) != checkBytes(bIndex)) return false
aIndex += 1
bIndex += 1
i += 1
}
true
}

override def copyToArray[B >: Byte](dest: Array[B], start: Int, len: Int): Int = {
// min of the bytes available to copy, bytes there is room for in dest and the requested number of bytes
val toCopy = math.min(math.min(len, length), dest.length - start)
Expand Down Expand Up @@ -912,6 +979,22 @@ object ByteString {
}
}

private[util] def bytesMatch(fromIndex: Int,
checkBytes: Array[Byte],
checkBytesFromIndex: Int,
checkLength: Int): Boolean = {
if (checkLength > 1 && bytestrings.nonEmpty && bytestrings.head.length >= fromIndex + checkLength - 1) {
bytestrings.head.bytesMatch(fromIndex, checkBytes, checkBytesFromIndex, checkLength)
} else {
var i = 0
while (i < checkLength) {
if (apply(fromIndex + i) != checkBytes(checkBytesFromIndex + i)) return false
i += 1
}
true
}
}

protected def writeReplace(): AnyRef = new SerializationProxy(this)
}

Expand Down Expand Up @@ -1093,22 +1176,10 @@ sealed abstract class ByteString
* @since 2.0.0
*/
def indexOfSlice(slice: Array[Byte], from: Int): Int = {
// this is only called if the first byte matches, so we can skip that check
def check(startPos: Int): Boolean = {
var i = startPos + 1
var j = 1
// let's trust the calling code has ensured that we have enough bytes in this ByteString
while (j < slice.length) {
if (apply(i) != slice(j)) return false
i += 1
j += 1
}
true
}
@tailrec def rec(from: Int): Int = {
val startPos = indexOf(slice.head, from, length - slice.length + 1)
if (startPos == -1) -1
else if (check(startPos)) startPos
else if (bytesMatch(startPos, slice, 0, slice.length)) startPos
else rec(startPos + 1)
}
val sliceLength = slice.length
Expand Down Expand Up @@ -1147,18 +1218,7 @@ sealed abstract class ByteString
*/
def startsWith(bytes: Array[Byte], offset: Int): Boolean = {
if (length - offset < bytes.length) false
else {
var i = offset
var j = 0
while (j < bytes.length) {
// we know that byteString is at least as long as bytes,
// so no need to check i < length
if (apply(i) != bytes(j)) return false
i += 1
j += 1
}
true
}
else bytesMatch(offset, bytes, 0, bytes.length)
}

/**
Expand All @@ -1170,6 +1230,15 @@ sealed abstract class ByteString
*/
def startsWith(bytes: Array[Byte]): Boolean = startsWith(bytes, 0)

/**
* Tests whether the bytes in a segment of this ByteString match the provided bytes.
* Internal use only. ByteString1 and ByteString1C have optimized versions.
*/
private[util] def bytesMatch(fromIndex: Int,
checkBytes: Array[Byte],
checkBytesFromIndex: Int,
checkLength: Int): Boolean

override def grouped(size: Int): Iterator[ByteString] = {
if (size <= 0) {
throw new IllegalArgumentException(s"size=$size must be positive")
Expand Down
1 change: 1 addition & 0 deletions actor/src/main/scala/org/apache/pekko/util/SWARUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private[util] object SWARUtil {
* @param array the byte array to read from
* @param index the index to read from
* @return the long value at the specified index
* @throws IndexOutOfBoundsException if index is out of bounds
*/
def getLong(array: Array[Byte], index: Int): Long = {
if (longBeArrayViewSupported) {
Expand Down
1 change: 1 addition & 0 deletions legal/pekko-actor-jar-license.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Copyright EPFL and Lightbend, Inc.
pekko-actor contains code from Netty which was released under an Apache 2.0 license.
Copyright 2014 The Netty Project
- actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala
- actor/src/main/scala/org/apache/pekko/util/ByteString.scala
- actor/src/main/scala/org/apache/pekko/util/SWARUtil.scala

---------------
Expand Down
Loading