Lucio Lelii 2019-04-18 16:21:30 +00:00
parent cefc28fda5
commit 7de18e7745
2 changed files with 66 additions and 40 deletions

View File

@ -21,36 +21,60 @@ public class FileDownload implements SHFile{
InputStream stream; InputStream stream;
AbstractFileItem fileItem; AbstractFileItem fileItem;
Object monitor = new Object();
long offset = 0;
public FileDownload(FileContainer fileContainer) throws Exception { public FileDownload(FileContainer fileContainer) throws Exception {
stream = fileContainer.download().getStream(); stream = fileContainer.download().getStream();
fileItem = fileContainer.get(); fileItem = fileContainer.get();
logger.trace("FILE-DOWNLOAD initialized with {} , {}", fileItem.getName(), fileItem.getContent().getSize()); logger.trace("FILE-DOWNLOAD initialized with {} , {}", fileItem.getName(), fileItem.getContent().getSize());
} }
public synchronized int read(Pointer buf, @size_t long size, @off_t long offset) { public int read(Pointer buf, @size_t long size, @off_t long offset) {
logger.trace("read called with size {} and offset {} ", size, offset); logger.trace("read called with size {} and offset {} ", size, offset);
while (this.offset!=offset) {
logger.trace("going in wait ({},{})",this.offset, offset);
synchronized (monitor) {
try {
monitor.wait();
logger.trace("waking up!!!");
} catch (InterruptedException e2) {
logger.warn("interrupt exception",e2);
}
}
}
int bytesToRead = (int) (size); int bytesToRead = (int) (size);
byte[] mybuf = new byte[bytesToRead]; byte[] mybuf = new byte[bytesToRead];
int readTotal= 0;; int readTotal= 0;
try { try {
int read =0; int read =0;
logger.trace("BEFORE: bytes to read {} and read total {} and last read {}", bytesToRead, readTotal, read);
while ((read= stream.read(mybuf, 0 , bytesToRead-readTotal))!=-1 && bytesToRead>readTotal) { while ((read= stream.read(mybuf, 0 , bytesToRead-readTotal))!=-1 && bytesToRead>readTotal) {
buf.put(0, mybuf, 0, read); buf.put(readTotal, mybuf, 0, read);
readTotal+= read; readTotal+= read;
logger.trace("INSIDE: bytes to read {} and read total {} and last read {}", bytesToRead, readTotal, read);
} }
logger.trace("AFTER: bytes to read {} and read total {} and last read {}", bytesToRead, readTotal, read);
logger.trace("bytes to read {} and read total {} and last read {}", bytesToRead, readTotal, read);
}catch (Exception e) { }catch (Exception e) {
logger.error("error in read",e); logger.error("error in read",e);
try { try {
stream.close(); stream.close();
} catch (IOException e1) {} } catch (IOException e1) {}
return -ErrorCodes.ENOENT(); return -ErrorCodes.ENOENT();
} finally {
this.offset = readTotal+offset;
logger.trace("setting offset to {}",this.offset);
synchronized (monitor) {
monitor.notifyAll();
} }
}
logger.trace("work finished!!! {}", readTotal);
return readTotal; return readTotal;
} }

View File

@ -242,6 +242,8 @@ public class StorageHubFS extends FuseStubFS {
logger.trace("!!! read called in path {} with size {} and offset {} ",path, size, offset); logger.trace("!!! read called in path {} with size {} and offset {} ",path, size, offset);
SHFile fileDownload; SHFile fileDownload;
synchronized (tempFiles) {
if (tempFiles.containsKey(path)) { if (tempFiles.containsKey(path)) {
fileDownload = tempFiles.get(path); fileDownload = tempFiles.get(path);
} else { } else {
@ -262,7 +264,7 @@ public class StorageHubFS extends FuseStubFS {
tempFiles.put(path, fileDownload); tempFiles.put(path, fileDownload);
} }
}
return fileDownload.read(buf, size, offset); return fileDownload.read(buf, size, offset);
} }