Skip to content

Commit 05d43d1

Browse files
Merge branch 'mrschmidt/bundle/bytebufferoutputstream' into mrschmidt/bundle/master
2 parents 01a8945 + 01c9c1a commit 05d43d1

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore.util;
16+
17+
import androidx.annotation.NonNull;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.nio.ByteBuffer;
21+
22+
/** Wraps a ByteBuffer in an InputStream. */
23+
public class ByteBufferInputStream extends InputStream {
24+
ByteBuffer buffer;
25+
26+
public ByteBufferInputStream(ByteBuffer buf) {
27+
this.buffer = buf;
28+
}
29+
30+
public int read() {
31+
if (!buffer.hasRemaining()) {
32+
return -1;
33+
}
34+
// `& 0xFF` converts the signed byte value to an integer and then strips the first 3 bytes.
35+
// This keeps the last eight bits of the value and thereby translates the original byte value to
36+
// the [0, 255] range.
37+
return buffer.get() & 0xFF;
38+
}
39+
40+
public int read(@NonNull byte[] b, int off, int len) throws IOException {
41+
if (!buffer.hasRemaining()) {
42+
return -1;
43+
}
44+
45+
len = Math.min(len, buffer.remaining());
46+
buffer.get(b, off, len);
47+
return len;
48+
}
49+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore.util;
16+
17+
import static org.junit.Assert.assertArrayEquals;
18+
import static org.junit.Assert.assertEquals;
19+
20+
import java.io.IOException;
21+
import java.nio.ByteBuffer;
22+
import org.junit.Test;
23+
import org.robolectric.annotation.Config;
24+
25+
@org.junit.runner.RunWith(org.robolectric.RobolectricTestRunner.class)
26+
@Config(manifest = Config.NONE)
27+
public class ByteBufferInputStreamTest {
28+
29+
@Test
30+
public void canReadSingleByte() {
31+
ByteBuffer source = ByteBuffer.wrap(new byte[] {1, 2, 3});
32+
ByteBufferInputStream inputStream = new ByteBufferInputStream(source);
33+
34+
assertEquals(1, inputStream.read());
35+
assertEquals(2, inputStream.read());
36+
assertEquals(3, inputStream.read());
37+
}
38+
39+
@Test
40+
public void canReadMultipleBytes() throws IOException {
41+
ByteBuffer source = ByteBuffer.wrap(new byte[] {1, 2, 3});
42+
ByteBufferInputStream inputStream = new ByteBufferInputStream(source);
43+
44+
byte[] destination = new byte[3];
45+
int bytesRead = inputStream.read(destination);
46+
47+
assertEquals(3, bytesRead);
48+
assertArrayEquals(new byte[] {1, 2, 3}, destination);
49+
}
50+
51+
@Test
52+
public void testHandlesEndOfStream() throws IOException {
53+
ByteBuffer source = ByteBuffer.wrap(new byte[] {1});
54+
ByteBufferInputStream inputStream = new ByteBufferInputStream(source);
55+
56+
byte[] destination = new byte[2];
57+
int bytesRead = inputStream.read(destination);
58+
59+
assertEquals(1, bytesRead);
60+
assertArrayEquals(new byte[] {1, 0}, destination);
61+
62+
assertEquals(-1, inputStream.read());
63+
}
64+
65+
@Test
66+
public void testHandlesNegativeBytes() throws IOException {
67+
ByteBuffer source = ByteBuffer.wrap(new byte[] {-1});
68+
ByteBufferInputStream inputStream = new ByteBufferInputStream(source);
69+
70+
int read = inputStream.read();
71+
// The integer value for byte -1 is 255, as the last three bits are set for -1.
72+
assertEquals(0xFF, read);
73+
assertEquals(-1, (byte) read);
74+
}
75+
}

0 commit comments

Comments
 (0)