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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading