You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IOTest.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.nio.charset.StandardCharsets;
  17. import java.util.Arrays;
  18. import org.junit.Test;
  19. public class IOTest {
  20. private static final byte[] DATA = "abcdefghijklmnopqrstuvwxyz"
  21. .getBytes(StandardCharsets.US_ASCII);
  22. private byte[] initBuffer(int size) {
  23. byte[] buffer = new byte[size];
  24. for (int i = 0; i < size; i++) {
  25. buffer[i] = (byte) ('0' + (i % 10));
  26. }
  27. return buffer;
  28. }
  29. private int read(byte[] buffer, int from) throws IOException {
  30. try (InputStream in = new ByteArrayInputStream(DATA)) {
  31. return IO.readFully(in, buffer, from);
  32. }
  33. }
  34. @Test
  35. public void readFullyBufferShorter() throws Exception {
  36. byte[] buffer = initBuffer(9);
  37. int length = read(buffer, 0);
  38. assertEquals(buffer.length, length);
  39. assertArrayEquals(buffer, Arrays.copyOfRange(DATA, 0, length));
  40. }
  41. @Test
  42. public void readFullyBufferLonger() throws Exception {
  43. byte[] buffer = initBuffer(50);
  44. byte[] initial = Arrays.copyOf(buffer, buffer.length);
  45. int length = read(buffer, 0);
  46. assertEquals(DATA.length, length);
  47. assertArrayEquals(Arrays.copyOfRange(buffer, 0, length), DATA);
  48. assertArrayEquals(Arrays.copyOfRange(buffer, length, buffer.length),
  49. Arrays.copyOfRange(initial, length, initial.length));
  50. }
  51. @Test
  52. public void readFullyBufferShorterOffset() throws Exception {
  53. byte[] buffer = initBuffer(9);
  54. byte[] initial = Arrays.copyOf(buffer, buffer.length);
  55. int length = read(buffer, 6);
  56. assertEquals(3, length);
  57. assertArrayEquals(Arrays.copyOfRange(buffer, 0, 6),
  58. Arrays.copyOfRange(initial, 0, 6));
  59. assertArrayEquals(Arrays.copyOfRange(buffer, 6, buffer.length),
  60. Arrays.copyOfRange(DATA, 0, 3));
  61. }
  62. @Test
  63. public void readFullyBufferLongerOffset() throws Exception {
  64. byte[] buffer = initBuffer(50);
  65. byte[] initial = Arrays.copyOf(buffer, buffer.length);
  66. int length = read(buffer, 40);
  67. assertEquals(10, length);
  68. assertArrayEquals(Arrays.copyOfRange(buffer, 0, 40),
  69. Arrays.copyOfRange(initial, 0, 40));
  70. assertArrayEquals(Arrays.copyOfRange(buffer, 40, buffer.length),
  71. Arrays.copyOfRange(DATA, 0, 10));
  72. }
  73. }