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
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.

# Ignore bytesMatch - a package private internal method
ProblemFilters.exclude[ReversedMissingMethodProblem]("org.apache.pekko.util.ByteString.bytesMatch")
227 changes: 202 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,85 @@ 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#L242-L325
override def indexOfSlice(slice: Array[Byte], from: Int): Int = {
val fromIndex = math.max(0, from)
val n = length - fromIndex
val m = slice.length
if (m == 0) return 0
// When the needle has only one byte that can be read,
// the indexOf() can be used
if (m == 1) return indexOf(slice.head, fromIndex)
var i = 0
var j = 0
val aStartIndex = 0
val bStartIndex = fromIndex
val suffixes = SWARUtil.maxSuf(slice, m, aStartIndex, true)
val prefixes = SWARUtil.maxSuf(slice, m, aStartIndex, false)
val ell = Math.max((suffixes >> 32).toInt, (prefixes >> 32).toInt)
var per = Math.max(suffixes.toInt, prefixes.toInt)
var memory = 0
val checkLen = Math.min(m - per, ell + 1)
if (SWARUtil.arrayBytesMatch(slice, aStartIndex, slice, aStartIndex + per, checkLen)) {
memory = -1
while (j <= n - m) {
i = Math.max(ell, memory) + 1
while (i < m && slice(i + aStartIndex) == bytes(i + j + bStartIndex)) i += 1
if (i > n) return -1
if (i >= m) {
i = ell
while (i > memory && slice(i + aStartIndex) == bytes(i + j + bStartIndex)) i -= 1
if (i <= memory) return j + bStartIndex
j += per
memory = m - per - 1
} else {
j += i - ell
memory = -1
}
}
} else {
per = Math.max(ell + 1, m - ell - 1) + 1
while (j <= n - m) {
i = ell + 1
while (i < m && slice(i + aStartIndex) == bytes(i + j + bStartIndex)) i += 1
if (i > n) return -1
if (i >= m) {
i = ell
while (i >= 0 && slice(i + aStartIndex) == bytes(i + j + bStartIndex)) i -= 1
if (i < 0) return j + bStartIndex
j += per
} else j += i - ell
}
}
-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 final 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 +669,87 @@ 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#L242-L325
override def indexOfSlice(slice: Array[Byte], from: Int): Int = {
val fromIndex = math.max(0, from)
val n = length - fromIndex
val m = slice.length
if (m == 0) return 0
// When the needle has only one byte that can be read,
// the indexOf() can be used
if (m == 1) return indexOf(slice.head, fromIndex)
var i = 0
var j = 0
val aStartIndex = 0
val bStartIndex = fromIndex + startIndex
val suffixes = SWARUtil.maxSuf(slice, m, aStartIndex, true)
Comment thread
pjfanning marked this conversation as resolved.
val prefixes = SWARUtil.maxSuf(slice, m, aStartIndex, false)
val ell = Math.max((suffixes >> 32).toInt, (prefixes >> 32).toInt)
var per = Math.max(suffixes.toInt, prefixes.toInt)
var memory = 0
val checkLen = Math.min(m - per, ell + 1)
if (SWARUtil.arrayBytesMatch(slice, aStartIndex, slice, aStartIndex + per, checkLen)) {
memory = -1
while (j <= n - m) {
i = Math.max(ell, memory) + 1
while (i < m && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i += 1
if (i > n) return -1
if (i >= m) {
i = ell
while (i > memory && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i -= 1
if (i <= memory) return j + bStartIndex - startIndex
j += per
memory = m - per - 1
} else {
j += i - ell
memory = -1
}
}
} else {
per = Math.max(ell + 1, m - ell - 1) + 1
while (j <= n - m) {
i = ell + 1
while (i < m && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i += 1
if (i > n) return -1
if (i >= m) {
i = ell
while (i >= 0 && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i -= 1
if (i < 0) return j + bStartIndex - startIndex
j += per
} else j += i - ell
}
}
-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 final 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 +1087,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) {
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 +1284,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 +1326,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 +1338,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
Loading
Loading