Skip to content

Commit

Permalink
catch EOFException for InputStream.read
Browse files Browse the repository at this point in the history
  • Loading branch information
benibela committed Feb 18, 2024
1 parent 8889d6a commit 3cbffec
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions system/bbjniutils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ TJavaEnv = record

function arrayToJArray(a: array of string): jobject;

private
function getClassNameOfObject(obj: jobject): string;
public
procedure RethrowJavaExceptionIfThereIsOne(aExceptionClass: JNIExceptionClass);
procedure RethrowJavaExceptionIfThereIsOne();
function ExceptionDescribeAndClear: string;
Expand Down Expand Up @@ -1139,6 +1142,15 @@ function TJavaEnv.arrayToJArray(a: array of string): jobject;
setStringArrayElement(result, i, a[i]);
end;

function TJavaEnv.getClassNameOfObject(obj: jobject): string;
var
temp: jobject;
begin
temp:= env^^.CallObjectMethod(env, obj, jCommonClasses.&Object.getClass);
result := jStringToStringAndDelete(env^^.CallObjectMethod(env, temp, jCommonClasses.&Class.getName));
DeleteLocalRef(temp);
end;


procedure TJavaEnv.RethrowJavaExceptionIfThereIsOne(aExceptionClass: JNIExceptionClass);
var cause: jobject;
Expand All @@ -1157,16 +1169,13 @@ procedure TJavaEnv.RethrowJavaExceptionIfThereIsOne();

function TJavaEnv.ExceptionDescribeAndClear: string;
var je: jthrowable;
temp: jobject;
begin
je := env^^.ExceptionOccurred(env);
if je = nil then exit('');
env^^.ExceptionDescribe(env);
env^^.ExceptionClear(env); //carefully, we need to clear before calling anything else
temp:= env^^.CallObjectMethod(env, je, jCommonClasses.&Object.getClass);
result := jStringToStringAndDelete(env^^.CallObjectMethod(env, temp, jCommonClasses.&Class.getName)) + ': '
result := getClassNameOfObject(je) + ': '
+ jStringToStringAndDelete(env^^.CallObjectMethod(env, je, jCommonClasses.Throwable.getMessage));
DeleteLocalRef(temp);
deleteLocalRef(je);
end;

Expand Down Expand Up @@ -1231,6 +1240,24 @@ function TJavaEnv.inputStreamToStringAndDelete(stream: jobject): string;

procedure TJavaEnv.inputStreamReadAllAndDelete(stream: jobject; readCallback: TStreamLikeWriteNativeInt; jmInputStreamRead,
jmInputStreamClose: jmethodID);

function RethrowNonEOFException: boolean;
var je: jthrowable;
className, fullError: String;
begin
result := false;
je := env^^.ExceptionOccurred(env);
if je = nil then exit();
className := getClassNameOfObject(je);
fullError := ExceptionDescribeAndClear;
if className = 'java.io.EOFException' then
exit(true)
else begin
fullError := 'InputStream.read error: '+fullError;
raise EAndroidInterfaceException.create(fullError, je);
end;
end;

const BUFFERLEN = 8192;
var wrappedBuffer: jvalue;
temp: JBoolean;
Expand All @@ -1242,7 +1269,8 @@ procedure TJavaEnv.inputStreamReadAllAndDelete(stream: jobject; readCallback: TS

wrappedBuffer.l := env^^.NewByteArray(env, BUFFERLEN);
len := env^^.CallIntMethodA(env, stream, jmInputStreamRead, @wrappedBuffer);;
RethrowJavaExceptionIfThereIsOne;
if ExceptionCheck and RethrowNonEOFException then
len := 0; //ignore java.io.EOFException. InputStream.read cannot throw java.io.EOFException, but it does with okhttp
while len >= 0 do begin
if len > 0 then begin
buffer := env^^.GetPrimitiveArrayCritical(env, wrappedBuffer.l, temp);
Expand Down

0 comments on commit 3cbffec

Please sign in to comment.