-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
fallback to imprecise scaling if low memory #1892
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/org/thoughtcrime/securesms/util/FlushedInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Memory-friendly workaround found from https://code.google.com/p/android/issues/detail?id=6066#c23 | ||
* to solve decoding problems in bitmaps from InputStreams that don't skip if no more stream is available. | ||
*/ | ||
class FlushedInputStream extends FilterInputStream { | ||
public FlushedInputStream(InputStream inputStream) { | ||
super(inputStream); | ||
} | ||
|
||
@Override | ||
public long skip(long n) throws IOException { | ||
long totalBytesSkipped = 0L; | ||
while (totalBytesSkipped < n) { | ||
long bytesSkipped = in.skip(n - totalBytesSkipped); | ||
if (bytesSkipped == 0L) { | ||
int inByte = read(); | ||
if (inByte < 0) { | ||
break; | ||
} else { | ||
bytesSkipped = 1; | ||
} | ||
} | ||
totalBytesSkipped += bytesSkipped; | ||
} | ||
return totalBytesSkipped; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this happening? This link is talking about blocking network streams, where as read() from disk should always read the number of bytes requested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, wasn't aware. The Javadoc didn't say that read() always returns the number of bytes requested. Seemed like a good idea to make sure it worked with all InputStream objects anyway. Would you prefer I instead have the method signature only take in FileInputStreams?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contract is not that strong, but in practice read() will only return short of the requested value if less than the requested value is available. That should only happen when reading from the network. I'm mostly curious how this came up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was a while ago, but I think originally I was experiencing those "decoder false" errors when doing Bitmap work a while back, and wrapping it in a BufferedInputStream seemed to resolve the problem. This is supposedly a more memory-efficient way to do the same thing, and it feels safer to include it.