1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-03 03:30:06 +00:00

Android: Fix issues with input stream special members

Fixes bugs in AndroidInputStreamWrapper introduced in
0d2e34f34c

- Now that AndroidInputStreamWrapper is moveable, its destructor must be
  able to handle the situation where stream is null
- The move assignment operators of AndroidInputStreamWrapper and
  AndroidContentUriInputStream could previously end up calling
  themselves recursively
This commit is contained in:
reuk 2024-08-29 19:40:08 +01:00
parent faf6d9976a
commit add3a5de0d
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C
2 changed files with 24 additions and 7 deletions

View file

@ -560,8 +560,8 @@ struct AndroidStreamHelpers
class AndroidInputStreamWrapper final : public InputStream
{
public:
explicit AndroidInputStreamWrapper (jobject streamIn)
: stream (LocalRef<jobject> { streamIn })
explicit AndroidInputStreamWrapper (LocalRef<jobject> streamIn)
: stream (std::move (streamIn))
{
}
@ -577,7 +577,7 @@ public:
AndroidInputStreamWrapper& operator= (AndroidInputStreamWrapper&& other) noexcept
{
std::swap (*this, other);
AndroidInputStreamWrapper { std::move (other) }.swap (*this);
return *this;
}
@ -585,6 +585,9 @@ public:
~AndroidInputStreamWrapper() override
{
if (stream == nullptr)
return;
getEnv()->CallVoidMethod (stream.get(), AndroidInputStream.close);
jniCheckHasExceptionOccurredAndClear();
}
@ -647,14 +650,22 @@ private:
return skipped == num;
}
void swap (AndroidInputStreamWrapper& other) noexcept
{
std::swap (other.byteArray, byteArray);
std::swap (other.stream, stream);
std::swap (other.pos, pos);
std::swap (other.exhausted, exhausted);
}
CachedByteArray byteArray;
GlobalRef stream;
int64 pos = 0;
bool exhausted = false;
};
std::unique_ptr<InputStream> makeAndroidInputStreamWrapper (jobject stream);
std::unique_ptr<InputStream> makeAndroidInputStreamWrapper (jobject stream)
std::unique_ptr<InputStream> makeAndroidInputStreamWrapper (LocalRef<jobject> stream);
std::unique_ptr<InputStream> makeAndroidInputStreamWrapper (LocalRef<jobject> stream)
{
return std::make_unique<AndroidInputStreamWrapper> (stream);
}
@ -672,7 +683,7 @@ struct AndroidContentUriInputStream final : public InputStream
AndroidContentUriInputStream& operator= (AndroidContentUriInputStream&& other) noexcept
{
std::swap (*this, other);
AndroidContentUriInputStream { std::move (other) }.swap (*this);
return *this;
}
@ -744,6 +755,12 @@ private:
return getPosition() == oldPosition + num;
}
void swap (AndroidContentUriInputStream& other) noexcept
{
std::swap (other.stream, stream);
std::swap (other.uri, uri);
}
AndroidInputStreamWrapper stream;
GlobalRef uri;
};

View file

@ -93,7 +93,7 @@ DECLARE_JNI_CLASS (AndroidAssetManager, "android/content/res/AssetManager")
#undef JNI_CLASS_MEMBERS
// Defined in juce_core
std::unique_ptr<InputStream> makeAndroidInputStreamWrapper (jobject stream);
std::unique_ptr<InputStream> makeAndroidInputStreamWrapper (LocalRef<jobject> stream);
struct AndroidCachedTypeface
{