+ private byte[] getAppDataAsByteArray(AppData appdata, VamsasArchiveReader docreader) {
+ if (appdata.getData()==null) {
+ if (docreader==null) {
+ log.warn("Silently failing getAppDataAsByteArray with null docreader.",new Exception());
+ return null;
+ }
+ // resolve and load data
+ JarInputStream entry = getAppDataStream(appdata, docreader);
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ try {
+ byte buff[] = new byte[_TRANSFER_BUFFER];
+ int olen=0;
+ while (entry.available()>0) {
+ int len = entry.read(buff, olen, _TRANSFER_BUFFER);
+ bytes.write(buff, 0, len);
+ olen+=len;
+ }
+ buff=null;
+ } catch (Exception e) {
+ log.warn("Unexpected exception - probable truncation when accessing VamsasDocument entry "+appdata.getDataReference(), e);
+ }
+ if (bytes.size()>0) {
+ // LATER: deal with probable OutOfMemoryErrors here
+ log.debug("Got "+bytes.size()+" bytes from AppDataReference "+appdata.getDataReference());
+ byte data[] = bytes.toByteArray();
+ bytes = null;
+ return data;
+ }
+ return null;
+ } else {
+ log.debug("Returning inline AppData block for "+appdata.getVorbaId());
+ return appdata.getData();
+ }
+ }
+ /**
+ * internal method for getting a DataInputStream from an AppData object.
+ * @param appdata
+ * @param docreader
+ * @return data in object or null if no data is accessible
+ */
+ private DataInput getAppDataAsDataInputStream(AppData appdata, VamsasArchiveReader docreader) {
+ if (appdata!=null && docreader!=null) {
+ String entryRef = appdata.getDataReference();
+ if (entryRef!=null) {
+ log.debug("Resolving AppData reference for "+entryRef);
+ InputStream jstrm = docreader.getAppdataStream(entryRef);
+ if (jstrm!=null)
+ return new AppDataInputStream(jstrm);
+ else {
+ log.debug("Returning null input stream for unresolved reference ("+entryRef+") id="+appdata.getVorbaId());
+ return null;
+ }
+ } else {
+ // return a byteArray input stream
+ byte[] data=appdata.getData();
+ if (data.length>0) {
+ ByteArrayInputStream stream = new ByteArrayInputStream(data);
+ return new DataInputStream(stream);
+ } else {
+ log.debug("Returning null input stream for empty Appdata data block in id="+appdata.getVorbaId());
+ return null;
+ }
+ }
+ } else {
+ log.debug("Returning null DataInputStream for appdata entry:"+appdata.getVorbaId());
+ }