1
0
Fork 0
mirror of https://github.com/IRS-Public/direct-file.git synced 2025-06-28 04:25:52 +00:00
This commit is contained in:
Elijah Wright 2025-06-05 22:23:45 -07:00 committed by GitHub
commit cd0412f56e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,8 +3,8 @@ package gov.irs.directfile.api.io.memory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -18,36 +18,18 @@ public class MemoryIOLocation implements IIOLocation {
// don't use it as a cache, because the stream copy isn't performant. // don't use it as a cache, because the stream copy isn't performant.
// it is still better than getting files off of disk. // it is still better than getting files off of disk.
// Locations are meant for heavy IO operations // Locations are meant for heavy IO operations
Map<String, InputStream> writtenData; Map<String, byte[]> writtenData;
public MemoryIOLocation() { public MemoryIOLocation() {
writtenData = new HashMap<>(); writtenData = new ConcurrentHashMap<>();
} }
@Override @Override
public InputStream read(String location) throws IOLocationException { public InputStream read(String location) throws IOLocationException {
if (writtenData.containsKey(location)) { byte[] bytes = writtenData.get(location);
var stream = writtenData.get(location); if (bytes != null) {
// we might hit a case where multiple threads try to read from the same stream
// at the same time. We don't want a partial read. There are better ways of solving
// this, but this is a quick way. If this performs anything like dotnet, this will show
// itself as a bottleneck by increasing the background thread count beyond normal levels.
// The read should be fast enough that it doesn't really matter, but I'd like to be certain.
// P.S. I don't care about writing as much in this context, but if write becomes important
// then you will need to sync that too.
synchronized (stream) {
try {
// We cannot hand out the original stream, as we don't know the stateCode
// they will return it in. We want to have it fresh and ready for each call
// so we pass out a copy and reset ours for the next call
log.info("In memory information found at {}! Copying bytes to a new buffer", location); log.info("In memory information found at {}! Copying bytes to a new buffer", location);
var bytes = stream.readAllBytes();
stream.reset();
return new ByteArrayInputStream(bytes); return new ByteArrayInputStream(bytes);
} catch (IOException e) {
throw new IOLocationException("In memory location couldn't copy bytes to allow reading", e);
}
}
} }
throw new IOLocationException( throw new IOLocationException(
String.format("Did not write data to in memory location %s before reading", location)); String.format("Did not write data to in memory location %s before reading", location));
@ -64,9 +46,8 @@ public class MemoryIOLocation implements IIOLocation {
} catch (IOException e) { } catch (IOException e) {
throw new IOLocationException("Could not write bytes to in memory stream", e); throw new IOLocationException("Could not write bytes to in memory stream", e);
} }
var stream = new ByteArrayInputStream(bytes);
log.info("Adding in memory byte stream at {}", location); log.info("Adding in memory byte stream at {}", location);
writtenData.put(location, stream); writtenData.put(location, bytes);
} }
@Override @Override