Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getStreamEntryRecord hanging with java21 migration #549

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,40 +208,42 @@ public static Object getByteStream(BObject entityObj) {
}
}

public static Object getStreamEntryRecord(BObject entityObj, long inputArraySize) {
Channel byteChannel = EntityBodyHandler.getByteChannel(entityObj);
if (byteChannel == null) {
return null;
}
byte[] bytes;
int arraySize = (int) inputArraySize;
try {
InputStream inputStream = byteChannel.getInputStream();
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
do {
byte[] buffer = new byte[arraySize];
int readCount = inputStream.read(buffer, 0, arraySize);
arraySize -= readCount;
if (readCount == -1 && output.size() == 0) {
EntityBodyHandler.closeByteChannel(byteChannel);
entityObj.addNativeData(ENTITY_BYTE_CHANNEL, null);
return null;
}
if (readCount == -1) {
break;
}
output.write(buffer, 0, readCount);
} while (arraySize > 0);
bytes = output.toByteArray();
public static Object getStreamEntryRecord(Environment env, BObject entityObj, long inputArraySize) {
return env.yieldAndRun(() -> {
Channel byteChannel = EntityBodyHandler.getByteChannel(entityObj);
if (byteChannel == null) {
return null;
}
byte[] bytes;
int arraySize = (int) inputArraySize;
try {
InputStream inputStream = byteChannel.getInputStream();
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
do {
byte[] buffer = new byte[arraySize];
int readCount = inputStream.read(buffer, 0, arraySize);
arraySize -= readCount;
if (readCount == -1 && output.size() == 0) {
EntityBodyHandler.closeByteChannel(byteChannel);
entityObj.addNativeData(ENTITY_BYTE_CHANNEL, null);
return null;
}
if (readCount == -1) {
break;
}
output.write(buffer, 0, readCount);
} while (arraySize > 0);
bytes = output.toByteArray();
}
} catch (RuntimeException | IOException ex) {
return IOUtils.createError(IOConstants.ErrorCode.GenericError,
"Error occurred while reading stream:" + ex.getMessage());
}
} catch (RuntimeException | IOException ex) {
return IOUtils.createError(IOConstants.ErrorCode.GenericError,
"Error occurred while reading stream:" + ex.getMessage());
}

BMap<BString, Object> streamEntry = ValueCreator.createRecordValue(getMimePackage(), STREAM_ENTRY_RECORD);
streamEntry.put(MimeConstants.FIELD_VALUE, ValueCreator.createArrayValue(bytes));
return streamEntry;
BMap<BString, Object> streamEntry = ValueCreator.createRecordValue(getMimePackage(), STREAM_ENTRY_RECORD);
streamEntry.put(MimeConstants.FIELD_VALUE, ValueCreator.createArrayValue(bytes));
return streamEntry;
});
}

public static Object closeInputByteStream(BObject entityObj) {
Expand Down
Loading