X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fattachment%2FFileAttachmentsStorage.java;h=0e70805c061c602f10e678d9063ef176f503d060;hb=5ae9dc81ad1c3ddfa99a8456b0c5263dd483642d;hp=fad4db19eeaf5e373ffa45389ad472a8640057da;hpb=44dd5750650b46d9979b4e06e4cc76c0b0003f4f;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java index fad4db19e..0e70805c0 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java @@ -11,9 +11,23 @@ import org.apache.commons.io.IOUtils; import org.argeo.slc.SlcException; public class FileAttachmentsStorage implements AttachmentsStorage { - private File attachmentsDirectory = new File(System - .getProperty("java.io.tmpdir") - + File.separator + "slcAttachments"); + private File attachmentsDirectory; + + public FileAttachmentsStorage() { + String osgiInstanceArea = System.getProperty("osgi.instance.area"); + if (osgiInstanceArea != null) { + if (osgiInstanceArea.startsWith("file:")) + osgiInstanceArea = osgiInstanceArea.substring("file:".length()); + attachmentsDirectory = new File(osgiInstanceArea + File.separator + + "slcAttachments"); + } + + if (attachmentsDirectory == null) { + String tempDir = System.getProperty("java.io.tmpdir"); + attachmentsDirectory = new File(tempDir + File.separator + + "slcAttachments"); + } + } public void retrieveAttachment(Attachment attachment, OutputStream outputStream) { @@ -22,8 +36,9 @@ public class FileAttachmentsStorage implements AttachmentsStorage { try { byte[] buffer = new byte[1024 * 1024]; in = new FileInputStream(file); - while (in.read(buffer) >= 0) { - outputStream.write(buffer); + int read = -1; + while ((read = in.read(buffer)) >= 0) { + outputStream.write(buffer, 0, read); } } catch (IOException e) { throw new SlcException("Cannot write attachment " + attachment @@ -39,8 +54,9 @@ public class FileAttachmentsStorage implements AttachmentsStorage { try { byte[] buffer = new byte[1024 * 1024]; out = new FileOutputStream(file); - while (inputStream.read(buffer) >= 0) { - out.write(buffer); + int read = -1; + while ((read = inputStream.read(buffer)) >= 0) { + out.write(buffer, 0, read); } } catch (IOException e) { throw new SlcException("Cannot write attachment " + attachment