< prev index next >

test/jdk/java/io/ByteArrayInputStream/ReadAllReadNTransferTo.java

Print this page




  28 import java.util.Objects;
  29 import java.util.Random;
  30 import jdk.test.lib.RandomFactory;
  31 
  32 /* @test
  33  * @library /test/lib
  34  * @build jdk.test.lib.RandomFactory
  35  * @run main ReadAllReadNTransferTo
  36  * @bug 8180451
  37  * @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo
  38  * @key randomness
  39  */
  40 public class ReadAllReadNTransferTo {
  41     private static final int SIZE = 0x4d4d;
  42 
  43     private static Random random = RandomFactory.getRandom();
  44 
  45     public static void main(String... args) throws IOException {
  46         byte[] buf = new byte[SIZE];
  47         random.nextBytes(buf);
  48         int offset = random.nextInt(SIZE/2);
  49         int length = random.nextInt(SIZE - offset);
  50 
  51         ByteArrayInputStream bais =
  52             new ByteArrayInputStream(buf, offset, length);
  53         int off = random.nextInt(length/2);
  54         int len = random.nextInt(length - off);
  55 
  56         byte[] bN = new byte[off + len];
  57         if (bais.readNBytes(bN, off, len) != len) {
  58             throw new RuntimeException("readNBytes return value");
  59         }
  60         if (!Arrays.equals(bN, off, off + len,
  61             buf, offset, offset + len)) {
  62             throw new RuntimeException("readNBytes content");
  63         }
  64 
  65         byte[] bAll = bais.readAllBytes();
  66         Objects.requireNonNull(bAll, "readAllBytes return value");
  67         if (bAll.length != length - len) {
  68             throw new RuntimeException("readAllBytes return value length");
  69         }
  70         if (!Arrays.equals(bAll, 0, bAll.length,
  71             buf, offset + len, offset + len + bAll.length)) {
  72             throw new RuntimeException("readAllBytes content");
  73         }
  74 
  75         // XXX transferTo()
  76         bais = new ByteArrayInputStream(buf);
  77         ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
  78         if (bais.transferTo(baos) != buf.length) {
  79             throw new RuntimeException("transferTo return value length");
  80         }
  81         if (!Arrays.equals(buf, baos.toByteArray())) {
  82             throw new RuntimeException("transferTo content");
  83         }
  84     }
  85 }


  28 import java.util.Objects;
  29 import java.util.Random;
  30 import jdk.test.lib.RandomFactory;
  31 
  32 /* @test
  33  * @library /test/lib
  34  * @build jdk.test.lib.RandomFactory
  35  * @run main ReadAllReadNTransferTo
  36  * @bug 8180451
  37  * @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo
  38  * @key randomness
  39  */
  40 public class ReadAllReadNTransferTo {
  41     private static final int SIZE = 0x4d4d;
  42 
  43     private static Random random = RandomFactory.getRandom();
  44 
  45     public static void main(String... args) throws IOException {
  46         byte[] buf = new byte[SIZE];
  47         random.nextBytes(buf);
  48         int position = random.nextInt(SIZE/2);
  49         int size = random.nextInt(SIZE - position);
  50 
  51         ByteArrayInputStream bais =
  52             new ByteArrayInputStream(buf, position, size);
  53         int off = random.nextInt(size/2);
  54         int len = random.nextInt(size- off);
  55 
  56         byte[] bN = new byte[off + len];
  57         if (bais.readNBytes(bN, off, len) != len) {
  58             throw new RuntimeException("readNBytes return value");
  59         }
  60         if (!Arrays.equals(bN, off, off + len,
  61             buf, position, position + len)) {
  62             throw new RuntimeException("readNBytes content");
  63         }
  64 
  65         byte[] bAll = bais.readAllBytes();
  66         Objects.requireNonNull(bAll, "readAllBytes return value");
  67         if (bAll.length != size - len) {
  68             throw new RuntimeException("readAllBytes return value length");
  69         }
  70         if (!Arrays.equals(bAll, 0, bAll.length,
  71             buf, position + len, position + len + bAll.length)) {
  72             throw new RuntimeException("readAllBytes content");
  73         }
  74 
  75         // XXX transferTo()
  76         bais = new ByteArrayInputStream(buf);
  77         ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
  78         if (bais.transferTo(baos) != buf.length) {
  79             throw new RuntimeException("transferTo return value length");
  80         }
  81         if (!Arrays.equals(buf, baos.toByteArray())) {
  82             throw new RuntimeException("transferTo content");
  83         }
  84     }
  85 }
< prev index next >