|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import static com.google.cloud.storage.ByteSizeConstants._256KiB; |
| 20 | +import static com.google.cloud.storage.TestUtils.xxd; |
| 21 | +import static com.google.common.truth.Truth.assertThat; |
| 22 | + |
| 23 | +import com.google.protobuf.ByteString; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public final class RewindableContentInputStreamTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void read_empty() throws IOException { |
| 32 | + RewindableContent content = RewindableContent.empty(); |
| 33 | + try (RewindableContentInputStream in = new RewindableContentInputStream(content)) { |
| 34 | + int read = in.read(); |
| 35 | + assertThat(read).isEqualTo(-1); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void readB_emptySrc() throws IOException { |
| 41 | + RewindableContent content = RewindableContent.empty(); |
| 42 | + try (RewindableContentInputStream in = new RewindableContentInputStream(content)) { |
| 43 | + int read = in.read(new byte[1]); |
| 44 | + assertThat(read).isEqualTo(-1); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void readB_emptyDst() throws IOException { |
| 50 | + byte[] bytes = DataGenerator.base64Characters().genBytes(1); |
| 51 | + RewindableContent content = RewindableContent.of(ByteBuffer.wrap(bytes)); |
| 52 | + try (RewindableContentInputStream in = new RewindableContentInputStream(content)) { |
| 53 | + byte[] tmp = new byte[0]; |
| 54 | + int read = in.read(tmp); |
| 55 | + assertThat(read).isEqualTo(0); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void readB_singleByte() throws IOException { |
| 61 | + byte[] bytes = DataGenerator.base64Characters().genBytes(1); |
| 62 | + RewindableContent content = RewindableContent.of(ByteBuffer.wrap(bytes)); |
| 63 | + try (RewindableContentInputStream in = new RewindableContentInputStream(content)) { |
| 64 | + byte[] tmp = new byte[_256KiB]; |
| 65 | + int read = in.read(tmp); |
| 66 | + assertThat(read).isEqualTo(1); |
| 67 | + assertThat(tmp[0]).isEqualTo(bytes[0]); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void read_singleByte() throws IOException { |
| 73 | + byte[] bytes = DataGenerator.base64Characters().genBytes(1); |
| 74 | + RewindableContent content = RewindableContent.of(ByteBuffer.wrap(bytes)); |
| 75 | + try (RewindableContentInputStream in = new RewindableContentInputStream(content)) { |
| 76 | + int read = in.read(); |
| 77 | + assertThat(read).isEqualTo(bytes[0]); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void readB_multiContent() throws IOException { |
| 83 | + byte[] bytes = DataGenerator.base64Characters().genBytes(30); |
| 84 | + RewindableContent content = |
| 85 | + RewindableContent.of( |
| 86 | + ByteBuffer.wrap(bytes, 0, 10), |
| 87 | + ByteBuffer.wrap(bytes, 10, 10), |
| 88 | + ByteBuffer.wrap(bytes, 20, 10)); |
| 89 | + try (RewindableContentInputStream in = new RewindableContentInputStream(content)) { |
| 90 | + byte[] tmp = new byte[_256KiB]; |
| 91 | + int read = in.read(tmp); |
| 92 | + assertThat(read).isEqualTo(30); |
| 93 | + assertThat(xxd(ByteString.copyFrom(tmp, 0, read))).isEqualTo(xxd(bytes)); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments