Skip to content

Commit

Permalink
use ByteString unsafe methods where it is ok (#4021)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Sep 6, 2024
1 parent 88505c0 commit f383d41
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ private[akkagrpc] class AkkaGrpcRequestBody(serverOptions: AkkaHttpServerOptions

private def toExpectedBodyType[R](byteString: ByteString, bodyType: RawBodyType[R]): RawValue[R] = {
bodyType match {
case RawBodyType.ByteArrayBody => RawValue(byteString.toArray)
case RawBodyType.ByteArrayBody => RawValue(byteString.toArrayUnsafe())
case RawBodyType.ByteBufferBody => RawValue(byteString.asByteBuffer)
case RawBodyType.InputStreamBody => RawValue(new ByteArrayInputStream(byteString.toArray))
case RawBodyType.InputStreamRangeBody => RawValue(InputStreamRange(() => new ByteArrayInputStream(byteString.toArray)))
case RawBodyType.InputStreamBody => RawValue(new ByteArrayInputStream(byteString.toArrayUnsafe()))
case RawBodyType.InputStreamRangeBody => RawValue(InputStreamRange(() => new ByteArrayInputStream(byteString.toArrayUnsafe())))
case RawBodyType.FileBody => ???
case m: RawBodyType.MultipartBody => ???
case _ => ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private[akkagrpc] class AkkaGrpcToResponseBody(implicit m: Materializer, ec: Exe
): ResponseEntity = {
bodyType match {
case RawBodyType.StringBody(charset) => ???
case RawBodyType.ByteArrayBody => HttpEntity(ct, encodeDataToFrameBytes(ByteString(r)))
case RawBodyType.ByteArrayBody => HttpEntity(ct, encodeDataToFrameBytes(ByteString.fromArrayUnsafe(r)))
case RawBodyType.ByteBufferBody => HttpEntity(ct, encodeDataToFrameBytes(ByteString(r)))
case RawBodyType.InputStreamBody => ???
case RawBodyType.InputStreamRangeBody => ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ private[akkahttp] object AkkaWebSockets {
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 @@ -11,7 +11,6 @@ import sttp.tapir.model.ServerRequest
import sttp.tapir.server.pekkohttp.PekkoHttpServerOptions
import sttp.tapir.server.interpreter.{RawValue, RequestBody}

import java.io.ByteArrayInputStream
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

Expand Down Expand Up @@ -39,10 +38,10 @@ private[pekkogrpc] class PekkoGrpcRequestBody(serverOptions: PekkoHttpServerOpti

private def toExpectedBodyType[R](byteString: ByteString, bodyType: RawBodyType[R]): RawValue[R] = {
bodyType match {
case RawBodyType.ByteArrayBody => RawValue(byteString.toArray)
case RawBodyType.ByteArrayBody => RawValue(byteString.toArrayUnsafe())
case RawBodyType.ByteBufferBody => RawValue(byteString.asByteBuffer)
case RawBodyType.InputStreamBody => RawValue(new ByteArrayInputStream(byteString.toArray))
case RawBodyType.InputStreamRangeBody => RawValue(InputStreamRange(() => new ByteArrayInputStream(byteString.toArray)))
case RawBodyType.InputStreamBody => RawValue(byteString.asInputStream)
case RawBodyType.InputStreamRangeBody => RawValue(InputStreamRange(() => byteString.asInputStream))
case RawBodyType.FileBody => ???
case m: RawBodyType.MultipartBody => ???
case _ => ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private[pekkogrpc] class PekkoGrpcToResponseBody(implicit m: Materializer, ec: E
): ResponseEntity = {
bodyType match {
case RawBodyType.StringBody(charset) => ???
case RawBodyType.ByteArrayBody => HttpEntity(ct, encodeDataToFrameBytes(ByteString(r)))
case RawBodyType.ByteArrayBody => HttpEntity(ct, encodeDataToFrameBytes(ByteString.fromArrayUnsafe(r)))
case RawBodyType.ByteBufferBody => HttpEntity(ct, encodeDataToFrameBytes(ByteString(r)))
case RawBodyType.InputStreamBody => ???
case RawBodyType.InputStreamRangeBody => ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ private[pekkohttp] object PekkoWebSockets {
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 @@ -63,11 +63,11 @@ private[play] class PlayRequestBody(serverOptions: PlayServerOptions)(implicit
bodyType match {
case RawBodyType.StringBody(defaultCharset) =>
bodyAsByteString().map(b => RawValue(b.decodeString(charset.getOrElse(defaultCharset))))
case RawBodyType.ByteArrayBody => bodyAsByteString().map(b => RawValue(b.toArray))
case RawBodyType.ByteArrayBody => bodyAsByteString().map(b => RawValue(b.toArrayUnsafe()))
case RawBodyType.ByteBufferBody => bodyAsByteString().map(b => RawValue(b.toByteBuffer))
case RawBodyType.InputStreamBody => bodyAsByteString().map(b => RawValue(new ByteArrayInputStream(b.toArray)))
case RawBodyType.InputStreamBody => bodyAsByteString().map(b => RawValue(new ByteArrayInputStream(b.toArrayUnsafe())))
case RawBodyType.InputStreamRangeBody =>
bodyAsByteString().map(b => RawValue(new InputStreamRange(() => new ByteArrayInputStream(b.toArray))))
bodyAsByteString().map(b => RawValue(new InputStreamRange(() => new ByteArrayInputStream(b.toArrayUnsafe()))))
case RawBodyType.FileBody =>
bodyAsFile match {
case Some(file) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ private[play] class PlayRequestBody(serverOptions: PlayServerOptions)(implicit
bodyType match {
case RawBodyType.StringBody(defaultCharset) =>
bodyAsByteString().map(b => RawValue(b.decodeString(charset.getOrElse(defaultCharset))))
case RawBodyType.ByteArrayBody => bodyAsByteString().map(b => RawValue(b.toArray))
case RawBodyType.ByteArrayBody => bodyAsByteString().map(b => RawValue(b.toArrayUnsafe()))
case RawBodyType.ByteBufferBody => bodyAsByteString().map(b => RawValue(b.toByteBuffer))
case RawBodyType.InputStreamBody => bodyAsByteString().map(b => RawValue(new ByteArrayInputStream(b.toArray)))
case RawBodyType.InputStreamBody => bodyAsByteString().map(b => RawValue(new ByteArrayInputStream(b.toArrayUnsafe())))
case RawBodyType.InputStreamRangeBody =>
bodyAsByteString().map(b => RawValue(new InputStreamRange(() => new ByteArrayInputStream(b.toArray))))
bodyAsByteString().map(b => RawValue(new InputStreamRange(() => new ByteArrayInputStream(b.toArrayUnsafe()))))
case RawBodyType.FileBody =>
bodyAsFile match {
case Some(file) =>
Expand Down

0 comments on commit f383d41

Please sign in to comment.