Skip to content
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

use unsafe ByteString methods when it's ok #2276

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
use unsafe ByteString methods when it's ok
  • Loading branch information
pjfanning committed Sep 6, 2024
commit 1baff6774384d03a8216d64fee02c1ac7b58e879
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ private[akkahttp] class BodyFromAkka()(implicit ec: ExecutionContext, mat: Mater
override protected def regularAsByteArray(response: HttpResponse): Future[Array[Byte]] =
response.entity.dataBytes
.runFold(ByteString(""))(_ ++ _)
.map(_.toArray[Byte])
.map(_.toArrayUnsafe())

override protected def regularAsFile(response: HttpResponse, file: SttpFile): Future[SttpFile] = {
val f = file.toFile
@@ -203,13 +203,13 @@ private[akkahttp] class BodyFromAkka()(implicit ec: ExecutionContext, mat: Mater
case msg: TextMessage =>
msg.textStream.runFold("")(_ + _).map(t => WebSocketFrame.text(t))
case msg: BinaryMessage =>
msg.dataStream.runFold(ByteString.empty)(_ ++ _).map(b => WebSocketFrame.binary(b.toArray))
msg.dataStream.runFold(ByteString.empty)(_ ++ _).map(b => WebSocketFrame.binary(b.toArrayUnsafe()))
}

private def frameToMessage(w: WebSocketFrame): Option[Message] =
w match {
case WebSocketFrame.Text(p, _, _) => Some(TextMessage(p))
case WebSocketFrame.Binary(p, _, _) => Some(BinaryMessage(ByteString(p)))
case WebSocketFrame.Binary(p, _, _) => Some(BinaryMessage(ByteString.fromArrayUnsafe(p)))
case WebSocketFrame.Ping(_) => None
case WebSocketFrame.Pong(_) => None
case WebSocketFrame.Close(_, _) => throw WebSocketClosed(None)
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ private[pekkohttp] class BodyFromPekko()(implicit ec: ExecutionContext, mat: Mat
override protected def regularAsByteArray(response: HttpResponse): Future[Array[Byte]] =
response.entity.dataBytes
.runFold(ByteString(""))(_ ++ _)
.map(_.toArray[Byte])
.map(_.toArrayUnsafe())

override protected def regularAsFile(response: HttpResponse, file: SttpFile): Future[SttpFile] = {
val f = file.toFile
@@ -204,13 +204,13 @@ private[pekkohttp] class BodyFromPekko()(implicit ec: ExecutionContext, mat: Mat
case msg: TextMessage =>
msg.textStream.runFold("")(_ + _).map(t => WebSocketFrame.text(t))
case msg: BinaryMessage =>
msg.dataStream.runFold(ByteString.empty)(_ ++ _).map(b => WebSocketFrame.binary(b.toArray))
msg.dataStream.runFold(ByteString.empty)(_ ++ _).map(b => WebSocketFrame.binary(b.toArrayUnsafe()))
}

private def frameToMessage(w: WebSocketFrame): Option[Message] =
w match {
case WebSocketFrame.Text(p, _, _) => Some(TextMessage(p))
case WebSocketFrame.Binary(p, _, _) => Some(BinaryMessage(ByteString(p)))
case WebSocketFrame.Binary(p, _, _) => Some(BinaryMessage(ByteString.fromArrayUnsafe(p)))
case WebSocketFrame.Ping(_) => None
case WebSocketFrame.Pong(_) => None
case WebSocketFrame.Close(_, _) => throw WebSocketClosed(None)
Loading