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

Commit d7f675e

Browse files
committed
Sync Blob Stream implementation with master branch
- Synced Blob Stream implementation with master branch. - Fixed unit test failures.
1 parent 0ca2eb1 commit d7f675e

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed

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

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

20-
import android.os.Debug;
21-
import android.util.Log;
22-
2320
import com.couchbase.lite.utils.IOUtils;
2421

2522
import org.junit.Rule;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ public void testEmptyBlob() throws IOException, CouchbaseLiteException {
20822082
assertNotNull(is);
20832083
byte[] buffer = new byte[10];
20842084
int bytesRead = is.read(buffer);
2085-
assertEquals(0, bytesRead);
2085+
assertEquals(-1, bytesRead);
20862086
} finally {
20872087
try {
20882088
is.close();
@@ -2093,7 +2093,7 @@ public void testEmptyBlob() throws IOException, CouchbaseLiteException {
20932093
}
20942094

20952095
@Test
2096-
public void testBlobWithStream() throws IOException, CouchbaseLiteException {
2096+
public void testBlobWithEmptyStream() throws IOException, CouchbaseLiteException {
20972097
MutableDocument doc = createMutableDocument("doc1");
20982098
byte[] content = "".getBytes();
20992099
InputStream stream = new ByteArrayInputStream(content);
@@ -2115,7 +2115,7 @@ public void testBlobWithStream() throws IOException, CouchbaseLiteException {
21152115
assertNotNull(is);
21162116
byte[] buffer = new byte[10];
21172117
int bytesRead = is.read(buffer);
2118-
assertEquals(0, bytesRead);
2118+
assertEquals(-1, bytesRead);
21192119
} finally {
21202120
try {
21212121
is.close();

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -520,39 +520,25 @@ static final class BlobInputStream extends InputStream {
520520
public int read() throws IOException {
521521
try {
522522
byte[] bytes = readStream.read(1);
523-
if (bytes.length == 1)
524-
return bytes[0];
525-
else
526-
// EOF
527-
return -1;
523+
return (bytes.length <= 0) ? -1 : bytes[0];
528524
} catch (LiteCoreException e) {
529525
throw new IOException(e);
530526
}
531527
}
532528

533529
@Override
534530
public int read(byte[] b) throws IOException {
535-
try {
536-
byte[] bytes = readStream.read(b.length);
537-
System.arraycopy(bytes, 0, b, 0, bytes.length);
538-
if (bytes.length > 0)
539-
return bytes.length;
540-
else
541-
return -1;
542-
} catch (LiteCoreException e) {
543-
throw new IOException(e);
544-
}
531+
return read(b, 0, b.length);
545532
}
546533

547534
@Override
548535
public int read(byte[] b, int off, int len) throws IOException {
536+
if (b.length <= 0) { return 0; }
549537
try {
550-
byte[] bytes = readStream.read(len);
538+
final byte[] bytes = readStream.read(len);
539+
if (bytes.length <= 0) { return -1; }
551540
System.arraycopy(bytes, 0, b, off, bytes.length);
552-
if (bytes.length > 0)
553-
return bytes.length;
554-
else
555-
return -1;
541+
return bytes.length;
556542
} catch (LiteCoreException e) {
557543
throw new IOException(e);
558544
}

0 commit comments

Comments
 (0)