From 30972d0e99e9c3776dcdca89f08244acf35af73f Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Wed, 30 Jan 2013 12:52:37 +0000 Subject: [PATCH] SP-475, BIS-255: bug in ContentCache fixed SVN: 28236 --- .../generic/shared/content/ContentCache.java | 206 ++++++++++-------- 1 file changed, 118 insertions(+), 88 deletions(-) diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/content/ContentCache.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/content/ContentCache.java index 11d999924c1..df8eb335db2 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/content/ContentCache.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/content/ContentCache.java @@ -92,6 +92,112 @@ public class ContentCache implements IContentCache, InitializingBean static final String DATA_SET_INFOS_FILE = ".dataSetInfos"; + private final class ProxyInputStream extends InputStream + { + private final InputStream inputStream; + + private final OutputStream fileOutputStream; + + private final IDatasetLocation dataSetLocation; + + private final File tempFile; + + private final String pathInWorkspace; + + private boolean closed; + + private boolean eof; + + private ProxyInputStream(InputStream inputStream, OutputStream fileOutputStream, + IDatasetLocation dataSetLocation, File tempFile, String pathInWorkspace) + { + this.inputStream = inputStream; + this.fileOutputStream = fileOutputStream; + this.dataSetLocation = dataSetLocation; + this.tempFile = tempFile; + this.pathInWorkspace = pathInWorkspace; + } + + @Override + public int read() throws IOException + { + if (eof) + { + return -1; + } + int b = inputStream.read(); + if (b < 0) + { + eof = true; + } else + { + fileOutputStream.write(b); + } + closeIfEndOfFile(); + return b; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException + { + if (eof) + { + return -1; + } + int count = inputStream.read(b, off, len); + if (count >= 0) + { + fileOutputStream.write(b, off, count); + } else + { + eof = true; + } + closeIfEndOfFile(); + return count; + } + + private void closeIfEndOfFile() throws IOException + { + if (eof) + { + close(); + } + } + + @Override + public void close() throws IOException + { + if (closed) + { + return; + } + inputStream.close(); + fileOutputStream.close(); + if (eof) + { + moveDownloadedFileToCache(tempFile, pathInWorkspace, + dataSetLocation.getDataSetCode()); + persistenceManager.requestPersistence(); + } else + { + tempFile.delete(); + } + closed = true; + fileLockManager.unlock(pathInWorkspace); + } + + @Override + protected void finalize() throws Throwable + { + if (closed == false) + { + tempFile.delete(); + fileLockManager.unlock(pathInWorkspace); + } + super.finalize(); + } + } + static final class DataSetInfo implements Serializable { private static final long serialVersionUID = 1L; @@ -272,94 +378,18 @@ public class ContentCache implements IContentCache, InitializingBean fileLockManager.unlock(pathInWorkspace); } } - final File tempFile = createTempFile(); - final InputStream inputStream = createInputStream(sessionToken, dataSetLocation, path); - final OutputStream fileOutputStream = createFileOutputStream(tempFile); - return new InputStream() - { - private boolean closed; - - private boolean eof; - - @Override - public int read() throws IOException - { - if (eof) - { - return -1; - } - int b = inputStream.read(); - if (b < 0) - { - eof = true; - } else - { - fileOutputStream.write(b); - } - closeIfEndOfFile(); - return b; - } - - @Override - public int read(byte[] b, int off, int len) throws IOException - { - if (eof) - { - return -1; - } - int count = inputStream.read(b, off, len); - if (count >= 0) - { - fileOutputStream.write(b, off, count); - } else - { - eof = true; - } - closeIfEndOfFile(); - return count; - } - - private void closeIfEndOfFile() throws IOException - { - if (eof) - { - close(); - } - } - - @Override - public void close() throws IOException - { - if (closed) - { - return; - } - inputStream.close(); - fileOutputStream.close(); - if (eof) - { - moveDownloadedFileToCache(tempFile, pathInWorkspace, - dataSetLocation.getDataSetCode()); - persistenceManager.requestPersistence(); - } else - { - tempFile.delete(); - } - closed = true; - fileLockManager.unlock(pathInWorkspace); - } - - @Override - protected void finalize() throws Throwable - { - if (closed == false) - { - tempFile.delete(); - fileLockManager.unlock(pathInWorkspace); - } - super.finalize(); - } - }; + try + { + final File tempFile = createTempFile(); + final InputStream inputStream = createInputStream(sessionToken, dataSetLocation, path); + final OutputStream fileOutputStream = createFileOutputStream(tempFile); + return new ProxyInputStream(inputStream, fileOutputStream, dataSetLocation, tempFile, + pathInWorkspace); + } catch (Throwable t) + { + fileLockManager.unlock(pathInWorkspace); + throw CheckedExceptionTunnel.wrapIfNecessary(t); + } } private void downloadFile(String sessionToken, IDatasetLocation dataSetLocation, -- GitLab