Using the Runtime SDK there appears to only be one way to ascertain the size of a portal item for download (in this case I am downloading a TPK or GeoTIFF file for use in our application). So essentially this forces you to cycle through the InputStream twice. Once to get the size of the file and then a second time to download. Obviously this is a waste of time and resources. Anyone know of a way to access portal items and get content-length as you might do with an httpconnection? I guess the complication is that if you go outside of the runtime sdk it is using Rest and you would have to provide credentials a second time, even though you are already doing this via the SDK. It would be a lot nicer if you could just use the "item" to ascertain the file size. Anybody out there have any ideas?
PortalItem item = PortalItem.fetchItem(portal, itemID);
InputStream inputStream = item.fetchData();
long fileLength;
byte[] buf = new byte[16384];
int len, size = 0;
while ((len = inputStream.read(buf)) != -1) {
size += len;
}
inputStream.close();
fileLength = size;
I am not entirely sure as I have not had an opportunity to test this myself, but I believe this will change in the Quartz release with PortalItem.getSize()
Currently, the Quartz release is in Beta. You can get started with the Quartz Beta release here:
This post from stack overflow might be helpful as well since rather than just calculating the length, you can write to the output byte array at the same time:
public byte[] readBytes(InputStream inputStream) throws IOException {
// this dynamically extends to take the bytes you read
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// this is storage overwritten on each iteration with bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
// we need to know how may bytes were read to write them to the byteBuffer
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
// and then we can return your byte array.
return byteBuffer.toByteArray();
}
Android - getting from a Uri to an InputStream to a byte array? - Stack Overflow