I have code that creates a PutDataRequest
to sync a Bitmap
with a wearable. My question is, do I need to close the ByteArrayOutputStream
in the code below, or does the DatApi
need it to stay open?
I'm pretty sure it needs to be closed. Just having a brain-fart moment.
PutDataMapRequest putRequest = PutDataMapRequest.create(path);
DataMap map = putRequest.getDataMap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
Asset asset = Asset.createFromBytes(stream.toByteArray());
map.putAsset(mapKey, asset);
Wearable.DataApi.putDataItem(connection.getClient(), putRequest.asPutDataRequest());
By looking at this line:
Asset asset = Asset.createFromBytes(stream.toByteArray());
it's clear that the Asset instance has no reference to the ByteArrayOutputStream
, since it received a reference to the byte[]
, not to the stream itself. So you should be able to close it safely after that point.
For safety, however, you could flush()
the stream before calling toByteArray()
, in case compress()
does not do that already.