Author: glandais Date: 2008-02-14 15:46:29 +0000 (Thu, 14 Feb 2008) New Revision: 973 Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java Log: Few optimizations Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-14 15:44:29 UTC (rev 972) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-14 15:46:29 UTC (rev 973) @@ -45,10 +45,8 @@ protected String baseFolder; /** - * Constructeur utilisant la configuration par defaut. - * <p/> - * TODO on devrait supprimer ce constructeur, car on doit maitriser la - * configuration. + * Constructeur utilisant la configuration par defaut. <p/> TODO on devrait supprimer ce constructeur, car on doit + * maitriser la configuration. */ public FileSystemAttachmentHandler() { baseFolder = Config.getProperties().getProperty("simexplorer.data"); @@ -56,7 +54,7 @@ /** * Constructeur utilisant une configuration spécifique - * + * * @param baseFolder le répertoire de base */ public FileSystemAttachmentHandler(String baseFolder) { @@ -64,36 +62,36 @@ } /** - * Retrieve file associated to content Create directories associated to - * file. + * Retrieve file associated to content Create directories associated to file. * - * @param entity - * DataEntity related to content - * @param attachment - * the attachment + * @param entity DataEntity related to content + * @param attachment the attachment * * @return Instance of file */ - private File getFile(MetaData entity, Attachment attachment) { - String resultPath = baseFolder; + private File getFile(MetaData entity, Attachment attachment, boolean createDir) { + StringBuffer resultPath = new StringBuffer(baseFolder); + String uuid = entity.getUuid(); + // Create file path String separator = File.separator; - if (entity.getUuid().length() > 3) { - resultPath = resultPath + entity.getUuid().substring(0, 1) + separator - + entity.getUuid().substring(1, 2) + separator - + entity.getUuid().substring(2, 3) + separator + entity.getUuid(); + if (uuid.length() > 3) { + resultPath.append(uuid.substring(0, 1)).append(separator).append(uuid.substring(1, 2)).append(separator) + .append(uuid.substring(2, 3)).append(separator).append(uuid); } else { - resultPath = resultPath + entity.getUuid(); + resultPath.append(uuid); } - resultPath = resultPath + separator + entity.getVersion().toString() + separator - + attachment.getUniqueId(); + resultPath.append(separator).append(entity.getVersion().toString()).append(separator).append( + attachment.getUniqueId()); - File result = new File(resultPath); - // Create directories - File resultFolder = new File(result.getParent()); - if (!resultFolder.exists()) { - resultFolder.mkdirs(); + File result = new File(resultPath.toString()); + if (createDir) { + // Create directories + File resultFolder = new File(result.getParent()); + if (!resultFolder.exists()) { + resultFolder.mkdirs(); + } } return result; @@ -103,12 +101,11 @@ * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#retrieveData(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String) */ @Override - public InputStream retrieveData(MetaData entity, Attachment attachment) - throws SimExplorerStorageException { + public InputStream retrieveData(MetaData entity, Attachment attachment) throws SimExplorerStorageException { // Simple stream on file FileInputStream fis; try { - fis = new FileInputStream(getFile(entity, attachment)); + fis = new FileInputStream(getFile(entity, attachment, false)); } catch (FileNotFoundException e) { throw new SimExplorerStorageException(e); } @@ -119,11 +116,10 @@ * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#retrieveMD5Data(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String) */ @Override - public String retrieveMD5Data(MetaData entity, Attachment attachment) - throws SimExplorerStorageException { + public String retrieveMD5Data(MetaData entity, Attachment attachment) throws SimExplorerStorageException { String md5; try { - md5 = MD5.asHex(MD5.getHash(getFile(entity, attachment))); + md5 = MD5.asHex(MD5.getHash(getFile(entity, attachment, false))); } catch (Exception e) { throw new SimExplorerStorageException(e); } @@ -134,14 +130,13 @@ * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#storeData(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String, java.io.InputStream) */ @Override - public void storeData(MetaData entity, Attachment attachment, InputStream is) - throws SimExplorerStorageException { + public void storeData(MetaData entity, Attachment attachment, InputStream is) throws SimExplorerStorageException { BufferedOutputStream bout = null; // Simple stream on file try { - File file = getFile(entity, attachment); + File file = getFile(entity, attachment, true); FileOutputStream fos = new FileOutputStream(file); // Buffer copy stream to stream @@ -154,7 +149,7 @@ break; bout.write(datum); } - + bout.flush(); } catch (FileNotFoundException e) { @@ -181,11 +176,10 @@ * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#deleteData(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String) */ @Override - public void deleteData(MetaData entity, Attachment attachment) - throws SimExplorerStorageException { + public void deleteData(MetaData entity, Attachment attachment) throws SimExplorerStorageException { try { // Simple delete on file - getFile(entity, attachment).delete(); + getFile(entity, attachment, false).delete(); } catch (Exception e) { throw new SimExplorerStorageException(e); }
participants (1)
-
glandais@users.labs.libre-entreprise.org