Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 0ca2eb1

Browse files
pasinbmeike
authored andcommitted
Fix reading from Blob stream not returning -1 when end of stream (#1902)
* c4stream_read return zero not -1 when end of stream. * Fixed BlobInputStream to return -1 when the number of bytes is zero. #1900
1 parent 74ec9d5 commit 0ca2eb1

File tree

2 files changed

+36
-2
lines changed
  • android/CouchbaseLite/src/androidTest/java/com/couchbase/lite
  • shared/src/main/java/com/couchbase/lite

2 files changed

+36
-2
lines changed

android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/BlobTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
//
1818
package com.couchbase.lite;
1919

20+
import android.os.Debug;
21+
import android.util.Log;
22+
2023
import com.couchbase.lite.utils.IOUtils;
2124

2225
import org.junit.Rule;
2326
import org.junit.Test;
2427
import org.junit.rules.TemporaryFolder;
2528

29+
import java.io.ByteArrayOutputStream;
2630
import java.io.File;
2731
import java.io.FileOutputStream;
2832
import java.io.IOException;
@@ -261,6 +265,36 @@ public void testBlobReadFunctions() throws Exception {
261265
}
262266
}
263267

268+
@Test
269+
public void testReadBlobStream() throws Exception {
270+
byte[] bytes;
271+
InputStream is = getAsset("attachment.png");
272+
try {
273+
bytes = IOUtils.toByteArray(is);
274+
} finally {
275+
is.close();
276+
}
277+
278+
Blob blob = new Blob("image/png", bytes);
279+
MutableDocument mDoc = new MutableDocument("doc1");
280+
mDoc.setBlob("blob", blob);
281+
Document doc = save(mDoc);
282+
283+
Blob savedBlob = doc.getBlob("blob");
284+
assertNotNull(savedBlob);
285+
assertEquals("image/png", savedBlob.getContentType());
286+
287+
int len = 0;
288+
final byte[] buffer = new byte[1024];
289+
ByteArrayOutputStream out = new ByteArrayOutputStream();
290+
InputStream inputStream = savedBlob.getContentStream();
291+
while ((len = inputStream.read(buffer)) != -1) {
292+
out.write(buffer, 0, len);
293+
}
294+
byte[] readBytes = out.toByteArray();
295+
assertTrue(Arrays.equals(bytes, readBytes));
296+
}
297+
264298
@Test
265299
public void testBlobConstructorsWithEmptyArgs() throws Exception {
266300
byte[] bytes;

shared/src/main/java/com/couchbase/lite/Blob.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public int read(byte[] b) throws IOException {
535535
try {
536536
byte[] bytes = readStream.read(b.length);
537537
System.arraycopy(bytes, 0, b, 0, bytes.length);
538-
if (bytes.length >= 0)
538+
if (bytes.length > 0)
539539
return bytes.length;
540540
else
541541
return -1;
@@ -549,7 +549,7 @@ public int read(byte[] b, int off, int len) throws IOException {
549549
try {
550550
byte[] bytes = readStream.read(len);
551551
System.arraycopy(bytes, 0, b, off, bytes.length);
552-
if (bytes.length >= 0)
552+
if (bytes.length > 0)
553553
return bytes.length;
554554
else
555555
return -1;

0 commit comments

Comments
 (0)