|
16 | 16 |
|
17 | 17 | package org.apache.pdfbox.io; |
18 | 18 |
|
| 19 | +import java.io.EOFException; |
19 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertFalse; |
21 | 22 | import static org.junit.jupiter.api.Assertions.assertTrue; |
22 | 23 |
|
23 | 24 | import java.io.File; |
| 25 | +import java.io.FileInputStream; |
24 | 26 | import java.io.IOException; |
| 27 | +import java.io.InputStream; |
25 | 28 | import java.net.URISyntaxException; |
| 29 | +import java.nio.charset.StandardCharsets; |
26 | 30 | import java.nio.file.Paths; |
27 | 31 |
|
28 | 32 | import org.junit.jupiter.api.Assertions; |
@@ -191,4 +195,105 @@ void testView() throws IOException, URISyntaxException |
191 | 195 | assertEquals(3, view.getPosition()); |
192 | 196 | } |
193 | 197 | } |
| 198 | + |
| 199 | + @Test |
| 200 | + void testReadFully1() throws IOException, URISyntaxException |
| 201 | + { |
| 202 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile( |
| 203 | + new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()))) |
| 204 | + { |
| 205 | + byte[] b = new byte[10]; |
| 206 | + randomAccessSource.seek(1); |
| 207 | + randomAccessSource.readFully(b); |
| 208 | + String s = new String(b, StandardCharsets.US_ASCII); |
| 209 | + assertEquals("1234567890", s); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + void testReadFully2() throws IOException, URISyntaxException |
| 215 | + { |
| 216 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile( |
| 217 | + new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()))) |
| 218 | + { |
| 219 | + byte[] b = new byte[10]; |
| 220 | + randomAccessSource.readFully(b, 2, 8); |
| 221 | + String s = new String(b, 2, 8, StandardCharsets.US_ASCII); |
| 222 | + assertEquals("01234567", s); |
| 223 | + assertEquals(0, b[0]); |
| 224 | + assertEquals(0, b[1]); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + void testReadFully3() throws IOException, URISyntaxException |
| 230 | + { |
| 231 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile( |
| 232 | + new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()))) |
| 233 | + { |
| 234 | + byte[] b = new byte[10]; |
| 235 | + randomAccessSource.seek(randomAccessSource.length() - b.length); |
| 236 | + randomAccessSource.readFully(b); |
| 237 | + String s = new String(b, StandardCharsets.US_ASCII); |
| 238 | + assertEquals("0123456789", s); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + void testReadFullyEOF() throws IOException, URISyntaxException |
| 244 | + { |
| 245 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile( |
| 246 | + new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()))) |
| 247 | + { |
| 248 | + byte[] b = new byte[10]; |
| 249 | + randomAccessSource.seek(randomAccessSource.length() - b.length + 1); |
| 250 | + Assertions.assertThrows(EOFException.class, () -> randomAccessSource.readFully(b)); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + void testReadFullyExact() throws IOException, URISyntaxException |
| 256 | + { |
| 257 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile( |
| 258 | + new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()))) |
| 259 | + { |
| 260 | + int length = (int) randomAccessSource.length(); |
| 261 | + byte[] b = new byte[length]; |
| 262 | + randomAccessSource.readFully(b); |
| 263 | + byte[] allBytes = getClass().getResourceAsStream("RandomAccessReadFile1.txt").readAllBytes(); |
| 264 | + Assertions.assertArrayEquals(allBytes, b); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + void testReadFullyAcrossBuffers() throws IOException, URISyntaxException |
| 270 | + { |
| 271 | + int bufferLen; |
| 272 | + File file = new File("src/test/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStreamTest.java"); |
| 273 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(file)) |
| 274 | + { |
| 275 | + int length = (int) randomAccessSource.length(); |
| 276 | + byte[] b = new byte[length]; |
| 277 | + bufferLen = randomAccessSource.read(b); |
| 278 | + // make sure that the double buffer size is smaller than the length |
| 279 | + // if this test fails, we'll need a larger file |
| 280 | + assertTrue(bufferLen * 2 < length); |
| 281 | + } |
| 282 | + byte[] expectedBytes; |
| 283 | + try (InputStream is = new FileInputStream(file)) |
| 284 | + { |
| 285 | + long skipped = is.skip(bufferLen / 2); |
| 286 | + assertEquals(skipped, bufferLen / 2); |
| 287 | + expectedBytes = new byte[bufferLen * 2]; |
| 288 | + int actualRead = is.read(expectedBytes, 1, expectedBytes.length - 1); |
| 289 | + assertEquals(expectedBytes.length - 1, actualRead); |
| 290 | + } |
| 291 | + try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(file)) |
| 292 | + { |
| 293 | + randomAccessSource.seek(bufferLen / 2); |
| 294 | + byte[] b = new byte[bufferLen * 2]; |
| 295 | + randomAccessSource.readFully(b, 1, b.length - 1); |
| 296 | + Assertions.assertArrayEquals(expectedBytes, b); |
| 297 | + } |
| 298 | + } |
194 | 299 | } |
0 commit comments