|
16 | 16 |
|
17 | 17 | package com.google.cloud.storage; |
18 | 18 |
|
| 19 | +import static com.google.cloud.storage.TestUtils.assertAll; |
| 20 | +import static com.google.cloud.storage.TestUtils.xxd; |
19 | 21 | import static com.google.common.truth.Truth.assertThat; |
20 | 22 |
|
| 23 | +import java.io.IOException; |
21 | 24 | import java.nio.ByteBuffer; |
| 25 | +import java.nio.channels.ReadableByteChannel; |
22 | 26 | import java.security.SecureRandom; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
23 | 28 | import org.junit.Test; |
24 | 29 |
|
25 | 30 | public final class BuffersTest { |
@@ -72,4 +77,87 @@ public void allocateAligned_evenlyDivisible_capacityGtAlignment() { |
72 | 77 | ByteBuffer b1 = Buffers.allocateAligned(8, 4); |
73 | 78 | assertThat(b1.capacity()).isEqualTo(8); |
74 | 79 | } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void fillFrom_handles_0SizeRead_someBytesRead() throws Exception { |
| 83 | + byte[] bytes = new byte[14]; |
| 84 | + ByteBuffer buf = ByteBuffer.wrap(bytes); |
| 85 | + |
| 86 | + byte[] expected = |
| 87 | + new byte[] { |
| 88 | + (byte) 'A', |
| 89 | + (byte) 'B', |
| 90 | + (byte) 'C', |
| 91 | + (byte) 'A', |
| 92 | + (byte) 'B', |
| 93 | + (byte) 'A', |
| 94 | + (byte) 'A', |
| 95 | + (byte) 'A', |
| 96 | + (byte) 'B', |
| 97 | + (byte) 'A', |
| 98 | + (byte) 'B', |
| 99 | + (byte) 'C', |
| 100 | + (byte) 0, |
| 101 | + (byte) 0 |
| 102 | + }; |
| 103 | + |
| 104 | + int[] acceptSequence = new int[] {3, 2, 1, 0, 0, 1, 2, 3}; |
| 105 | + AtomicInteger readCount = new AtomicInteger(0); |
| 106 | + |
| 107 | + ReadableByteChannel c = |
| 108 | + new ReadableByteChannel() { |
| 109 | + @Override |
| 110 | + public int read(ByteBuffer dst) throws IOException { |
| 111 | + int i = readCount.getAndIncrement(); |
| 112 | + if (i == acceptSequence.length) { |
| 113 | + return -1; |
| 114 | + } |
| 115 | + int bytesToRead = acceptSequence[i]; |
| 116 | + if (bytesToRead > 0) { |
| 117 | + long copy = |
| 118 | + Buffers.copy(DataGenerator.base64Characters().genByteBuffer(bytesToRead), dst); |
| 119 | + assertThat(copy).isEqualTo(bytesToRead); |
| 120 | + } |
| 121 | + |
| 122 | + return bytesToRead; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean isOpen() { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void close() throws IOException {} |
| 132 | + }; |
| 133 | + int filled = Buffers.fillFrom(buf, c); |
| 134 | + |
| 135 | + assertAll( |
| 136 | + () -> assertThat(filled).isEqualTo(12), |
| 137 | + () -> assertThat(xxd(bytes)).isEqualTo(xxd(expected))); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void fillFrom_handles_0SizeRead_noBytesRead() throws Exception { |
| 142 | + ByteBuffer buf = ByteBuffer.allocate(3); |
| 143 | + |
| 144 | + ReadableByteChannel c = |
| 145 | + new ReadableByteChannel() { |
| 146 | + @Override |
| 147 | + public int read(ByteBuffer dst) throws IOException { |
| 148 | + return -1; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean isOpen() { |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void close() throws IOException {} |
| 158 | + }; |
| 159 | + int filled = Buffers.fillFrom(buf, c); |
| 160 | + |
| 161 | + assertThat(filled).isEqualTo(-1); |
| 162 | + } |
75 | 163 | } |
0 commit comments