Skip to content

Instantly share code, notes, and snippets.

@cormacrelf
Last active January 4, 2016 18:59
Show Gist options
  • Save cormacrelf/8664818 to your computer and use it in GitHub Desktop.
Save cormacrelf/8664818 to your computer and use it in GitHub Desktop.
def encodeMapResponse(results: List[Match]): HttpResponse = {
val response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
val stream = new ByteArrayOutputStream(1024)
val w = new ObjectOutputStream(stream)
try {
w.writeObject(results)
} catch {
case e:IOException => Console.err.println(id + ": " + e)
}
w.close
val content = stream.toByteArray.toString
response.setContent(copiedBuffer(content, UTF_8))
response
}
def decodeMapResponse(response: HttpResponse): List[Match] = {
var list = List[Match]()
val cbuf:ChannelBuffer = response.getContent()
Console.err.println(cbuf.toString("UTF-8"))
if (cbuf.readable) {
val stream = new ByteArrayInputStream(cbuf.toString("UTF-8").getBytes())
val w = new ObjectInputStream(stream)
try {
list = w.readObject.asInstanceOf[List[Match]]
} catch {
case e:IOException => Console.err.println(e)
}
w.close
}
list
}
def encodeMapResponse(results: List[Match]): HttpResponse = {
val response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
val stream = new ByteArrayOutputStream(1024)
val w = new ObjectOutputStream(stream)
try {
w.writeObject(results)
} catch {
case e:IOException => Console.err.println(id + ": " + e)
}
w.flush
w.close
val content = stream.toByteArray()
val cbuf = ChannelBuffers.copiedBuffer(content)
response.setContent(cbuf)
response
}
def decodeMapResponse(response: HttpResponse): List[Match] = {
var list = List[Match]()
val cbuf:ChannelBuffer = response.getContent()
var bytes = new Array[Byte](cbuf.readableBytes())
cbuf.getBytes(cbuf.readerIndex(), bytes, 0, cbuf.readableBytes())
if (cbuf.readable) {
val stream = new ByteArrayInputStream(bytes)
val w = new ObjectInputStream(stream)
try {
list = w.readObject.asInstanceOf[List[Match]]
} catch {
case e:IOException => Console.err.println(e)
}
w.close
}
list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment