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

Bug: Saving a blob with InputStream #23

Open
benjaminglatzeder opened this issue Sep 9, 2019 · 3 comments

Comments

@benjaminglatzeder
Copy link

commented Sep 9, 2019

CBL: 2.6 CE

Saving a blob with ByteArrayInputStream which extends from InputStream

new Blob("image/jpeg", getPicture(bitmap)
// ...
public static ByteArrayInputStream getPicture(Bitmap bitmap) {
ByteArrayInputStream myByteArrayInputStream = new ByteArrayInputStream(// ...);
// ...
return myByteArrayInputStream;
}

an error is thrown at SG:
[INF] SyncMsg: c:[5b1db747] #39: Type:rev --> 400 Incorrect data sent for attachment with digest: sha1-lcJZ/lBqNbS23v0aIzI+LmSxwxA= Time:66.291689ms

and in Android Studio it's logged as:

E/CouchbaseLite/REPLICATOR: {N8litecore4repl6PusherE#1} Got error response to rev '7JFlD2OEV7U2ZBYiBXCC20aEaIL2::khj8xbs2yw3n::item::b55bea1b804a' #4-23fae3d30ea8398c9750e39637d52618ac7a3068 (seq #545): HTTP 400 'Incorrect data sent for attachment with digest: sha1-lcJZ/lBqNbS23v0aIzI+LmSxwxA='

If I wrap the ByteArrayInputStream with org.apache.commons.io.IOUtils.toByteArray there is no error and the blob is pushed up.

new Blob("image/jpeg", IOUtils.toByteArray(getPicture(bitmap))
@bmeike

This comment has been minimized.

Copy link
Collaborator

commented Sep 9, 2019

Yeah.... Looks to me like the code will read, at most 8K bytes. If your picture is bigger than that, you lose.

(Note that there is no such limit if you do the conversion to a byte array, yourself. The constructor that takes a byte array as its second arg will happily deal with an array of any size.)

    private byte[] getBytesFromInitialContentStream() {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();

//...

            final byte[] buffer = new byte[MAX_CACHED_CONTENT_LENGTH];
            try {
                int bytesRead;
                while ((bytesRead = initialContentStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
                contentResult = out.toByteArray();
                blobLength = contentResult.length;
                if (blobLength <= MAX_CACHED_CONTENT_LENGTH) {
                    content = contentResult;  // cache for later re-use
                }
            }
// ...
        return contentResult;
    }

@bmeike bmeike added bug triaged labels Sep 9, 2019

@bmeike

This comment has been minimized.

Copy link
Collaborator

commented Sep 9, 2019

@bmeike

This comment has been minimized.

Copy link
Collaborator

commented Sep 10, 2019

Note that if that stream is longer than MAX_CACHED_CONTENT_LENGTH, the second call to this function will return different contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.