From 3e8b089a7838da522348f8601ef261deeb2ec5fc Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 22 Mar 2006 14:12:12 +0000 Subject: [PATCH] added new method for writing an AppData jar entry directly from an InputStream. Also reprioritised some TODOs and made comments git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@197 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/simpleclient/VamsasArchive.java | 75 ++++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/src/org/vamsas/client/simpleclient/VamsasArchive.java b/src/org/vamsas/client/simpleclient/VamsasArchive.java index 93c775c..d510d51 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchive.java @@ -149,7 +149,7 @@ public class VamsasArchive { /** * name of backup of existing archive that has been updated/overwritten. - * onlu one backup will be made - and this is it. + * only one backup will be made - and this is it. */ File originalBackup = null; @@ -157,9 +157,10 @@ public class VamsasArchive { private final int _TRANSFER_BUFFER=4096*4; protected SimpleDocument vorba = null; /** + * LATER: ? CUT'n'Paste error ? * Access and return current vamsas Document, if it exists, or create a new one * (without affecting VamsasArchive object state - so is NOT THREAD SAFE) - * TODO: possibly modify internal state to lock low-level files + * _TODO: possibly modify internal state to lock low-level files * (like the IClientDocument interface instance constructer would do) * @see org.vamsas.simpleclient.VamsasArchive.getOriginalVamsasDocument for additional caveats * @@ -167,6 +168,7 @@ public class VamsasArchive { * @throws IOException * @throws org.exolab.castor.xml.MarshalException * @throws org.exolab.castor.xml.ValidationException + * ????? where does this live JBPNote ? */ private VamsasDocument _doc=null; @@ -502,7 +504,7 @@ public class VamsasArchive { * opens the new archive ready for writing. If the new archive is replacing an existing one, * then the existing archive will be locked, and the new archive written to a temporary file. * The new archive will be put in place once close() is called. - * @param doclock TODO + * @param doclock LATER - pass existing lock on document, if it exists.... no need yet? * @throws IOException */ private void openArchive() throws IOException { @@ -598,7 +600,23 @@ public class VamsasArchive { public boolean transferAppDataEntry(String AppDataReference) throws IOException { return transferAppDataEntry(AppDataReference, AppDataReference); } - + /** + * Validates the AppDataReference: not null and not already written to archive. + * @param AppDataReference + * @return true if valid. false if not + * @throws IOException for really broken references! + */ + protected boolean _validNewAppDataReference(String newAppDataReference) throws IOException { + // LATER: Specify valid AppDataReference form in all VamsasArchive handlers + if (newAppDataReference==null) + throw new IOException("null newAppDataReference!"); + if (entries.containsKey(newAppDataReference)) { + log.warn("Attempt to write '"+newAppDataReference+"' twice! - IGNORED"); + // LATER: fix me? warning message should raise an exception here. + return false; + } + return true; + } /** * Transfers an AppDataReference from old to new vamsas archive, with a name change. * @see transferAppDataEntry(String AppDataReference) @@ -608,17 +626,15 @@ public class VamsasArchive { * @throws IOException */ public boolean transferAppDataEntry(String AppDataReference, String NewAppDataReference) throws IOException { - // TODO: Specify valid AppDataReference form in all VamsasArchive handlers - if (AppDataReference==null) - throw new IOException("null AppDataReference!"); if (original==null || !original.exists()) { log.warn("No backup archive exists."); return false; } - if (entries.containsKey(NewAppDataReference)) { - log.warn("Attempt to write '"+NewAppDataReference+"' twice! - IGNORED"); - return true; - } + if (AppDataReference==null) + throw new IOException("null AppDataReference!"); + + if (!_validNewAppDataReference(NewAppDataReference)) + return false; accessOriginal(); @@ -645,11 +661,42 @@ public class VamsasArchive { +AppDataReference+"' as '"+NewAppDataReference+"' ("+count+" bytes)"); return true; } - + /** + * write data from a stream into an appData reference. + * @param AppDataReference - New AppDataReference not already written to archive + * @param adstream Source of data for appData reference - read until .read(buffer) returns -1 + * @return true on success. + * @throws IOException for file IO or invalid AppDataReference string + */ + public boolean writeAppdataFromStream(String AppDataReference, java.io.InputStream adstream) throws IOException { + if (!_validNewAppDataReference(AppDataReference)) { + log.warn("Invalid AppDataReference passed to writeAppdataFromStream"); + throw new IOException("Invalid AppDataReference! (null, or maybe non-unique)!"); + } + + if (AppDataReference==null) { + log.warn("null appdata passed."); + throw new IOException("Null AppDataReference"); + } + + java.io.OutputStream adout = getAppDataStream(AppDataReference); + // copy over the bytes + int written=-1; + long count=0; + byte[] buffer = new byte[_TRANSFER_BUFFER]; // conservative estimate of a sensible buffer + do { + if ((written = adstream.read(buffer))>-1) { + adout.write(buffer, 0, written); + log.debug("Transferring "+written+"."); + count+=written; + } + } while (written>-1); + return true; + } /** * transfers any AppDataReferences existing in the old document * that haven't already been transferred to the new one - * TODO: LATER: do the same for transfers requiring a namechange - more document dependent. + * LATER: do the same for transfers requiring a namechange - more document dependent. * @return true if data was transferred. */ public boolean transferRemainingAppDatas() throws IOException { @@ -698,7 +745,7 @@ public class VamsasArchive { odoclock.updateFrom(null, rchive); } catch (IOException e) { - // TODO: LATER: decide if leaving nastily named backup files around is necessary. + // LATER: decide if leaving nastily named backup files around is necessary. log.error("Problem updating archive from temporary file! - backup left in '" +backupFile().getAbsolutePath()+"'",e); } -- 1.7.10.2