How do I translate the following Java code to Scala using the new Try
API?
public byte[] deflate(byte[] data) {
ByteArrayOutputStream outputStream = null;
GZIPOutputStream gzipOutputStream = null;
try {
outputStream = new ByteArrayOutputStream();
gzipOutputStream = new GZIPOutputStream(outputStream);
gzipOutputStream.write(data);
return outputStream.toByteArray();
catch (Exception e) {
...
} finally {
if (gzipOutputStream != null) gzipOutputStream.close();
}
}
The Scala version should be something like this...
def deflate(data Array[Byte]): Try[Array[Byte]] = Try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
new GZIPOutputStream(outputStream).write(data)
outputStream.toByteArray
}
... but how do I implement Java's finally
equivalent?