Here below is my attempt to implement a class that provides functionality for compressing/decompressing strings:
object GZipHelper {
def deflate(txt: String): Try[String] = {
try {
val arrOutputStream = new ByteArrayOutputStream()
val zipOutputStream = new GZIPOutputStream(arrOutputStream)
zipOutputStream.write(txt.getBytes)
new Success(Base64.encodeBase64String(arrOutputStream.toByteArray))
} catch {
case _: e => new Failure(e)
}
}
def inflate(deflatedTxt: String): Try[String] = {
try {
val bytes = Base64.decodedBase64(deflatedTxt)
val zipInputStream = GZIPInputStream(new ByteArrayInputStream(bytes))
new success(IOUtils.toString(zipInputStream))
} catch {
case _: e => new Failure(e)
}
}
}
As you can see, the finally
blocks that close GZIPOutputStream
and GZIPInputStream
are missing... how could I implement this in the ''scala'' way? How could I improve the code?
Since you're using the "old fashioned" try
statement and explicitly turning it into a scala.util.Try
, there really is no reason not to add a finally
block after your try
.
In this specific case though, there is little point in closing, for example, your ByteArrayInputStream
- it's not really an open resource and does not need to be closed. In which case you can simplify your code and make it much more idiomatic this way:
def inflate(deflatedTxt: String): Try[String] = Try {
val bytes = Base64.decodedBase64(deflatedTxt)
val zipInputStream = GZIPInputStream(new ByteArrayInputStream(bytes))
IOUtils.toString(zipInputStream)
}
I personally would not declare bytes
and zipInputStream
since they're only used once, but that's a matter of preference.
The trick here is having a finally
block with a call to scala.util.Try.apply
- I'm not sure that's possible without going through a call to map
that doesn't actually modify anything, which seems like a bit of an oversight to me. I was expecting to see an andThen
or eventually
method in scala.util.Try
, but it doesn't seem to be there (yet?).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With