Skip to content

Commit

Permalink
Refactor RSIStorage
Browse files Browse the repository at this point in the history
- do not use raw types
- save byte arrays instead of dangling output streams
  • Loading branch information
Enet4 committed Jul 24, 2024
1 parent 3639ea8 commit bdae719
Showing 1 changed file with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import pt.ua.dicoogle.sdk.StorageInputStream;
import pt.ua.dicoogle.sdk.StorageInterface;
import pt.ua.dicoogle.sdk.settings.ConfigurationHolder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -45,7 +44,7 @@
public class RSIStorage implements StorageInterface {
private static final Logger logger = LoggerFactory.getLogger(RSIStorage.class);

private final Map<String, ByteArrayOutputStream> mem = new HashMap<>();
private final Map<String, byte[]> mem = new HashMap<>();
private boolean enabled = true;
private ConfigurationHolder settings;

Expand All @@ -67,7 +66,6 @@ public Iterable<StorageInputStream> at(final URI location, Object... objects) {

@Override
public Iterator<StorageInputStream> iterator() {
Collection c2 = new ArrayList<>();
StorageInputStream s = new StorageInputStream() {

@Override
Expand All @@ -77,18 +75,17 @@ public URI getURI() {

@Override
public InputStream getInputStream() throws IOException {
ByteArrayOutputStream bos = mem.get(location.toString());
ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
byte[] data = mem.get(location.toString());
ByteArrayInputStream bin = new ByteArrayInputStream(data);
return bin;
}

@Override
public long getSize() throws IOException {
return mem.get(location.toString()).size();
return mem.get(location.toString()).length;
}
};
c2.add(s);
return c2.iterator();
return Collections.singletonList(s).iterator();
}
};
return c;
Expand All @@ -97,16 +94,14 @@ public long getSize() throws IOException {
@Override
public URI store(DicomObject dicomObject, Object... objects) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DicomOutputStream dos = new DicomOutputStream(bos);
try {
try (DicomOutputStream dos = new DicomOutputStream(bos)) {
dos.writeDicomFile(dicomObject);
} catch (IOException ex) {
logger.warn("Failed to store object", ex);
}
bos.toByteArray();
URI uri = URI.create("mem://" + UUID.randomUUID().toString());
mem.put(uri.toString(), bos);
mem.put(uri.toString(), bos.toByteArray());

return uri;
}

Expand Down

0 comments on commit bdae719

Please sign in to comment.