optimise string operations

This commit is contained in:
hrj 2021-04-12 07:06:44 +05:30
parent 89eeb76c46
commit 6fd34c16ac

View File

@ -19,15 +19,15 @@ class Server(port: Int) {
private def getRequestJson(ex: HttpExchange): JValue = { private def getRequestJson(ex: HttpExchange): JValue = {
val requestBody = ex.getRequestBody val requestBody = ex.getRequestBody
val bytes = requestBody.readAllBytes val bytes = requestBody.readAllBytes
val string = bytes.map(_.toChar).mkString val string = new String(bytes)
parse(string) parse(string)
} }
private val eqPattern = java.util.regex.Pattern.compile("=")
private def getPathParameter(ex: HttpExchange): String = { private def getPathParameter(ex: HttpExchange): String = {
try { try {
val uri = ex.getRequestURI.toString val query = ex.getRequestURI.getQuery
val param = uri.split("\\?")(1) eqPattern.split(query)(1)
param.split("=")(1)
} catch { } catch {
case exception: ArrayIndexOutOfBoundsException => { case exception: ArrayIndexOutOfBoundsException => {
println(exception.getStackTrace) println(exception.getStackTrace)