Example usage for java.util Random nextBytes

List of usage examples for java.util Random nextBytes

Introduction

In this page you can find the example usage for java.util Random nextBytes.

Prototype

public void nextBytes(byte[] bytes) 

Source Link

Document

Generates random bytes and places them into a user-supplied byte array.

Usage

From source file:org.unitedinternet.cosmo.util.BufferedContentTest.java

/**
 * Tests buffered content.// w  ww .ja v  a  2s.co  m
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testBufferedContent() throws Exception {
    Random random = new Random();

    // 100K test
    byte[] bytes = new byte[1024 * 100];
    random.nextBytes(bytes);

    BufferedContent content = new BufferedContent(new ByteArrayInputStream(bytes));

    Assert.assertTrue(content.getLength() == (1024 * 100));

    // verify streams are the same
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));
    // verify we can re-consume the same stream
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));

    // should fit into memory
    Assert.assertTrue(content.getInputStream() instanceof ByteArrayInputStream);

    // should be buffered into file
    content = new BufferedContent(new ByteArrayInputStream(bytes), 1024 * 50);
    Assert.assertTrue(content.getLength() == (1024 * 100));
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));

    // should be in a file
    Assert.assertTrue(content.getInputStream() instanceof FileInputStream);
}

From source file:alluxio.client.rest.TestCaseOptionsTest.java

/**
 * Tests getting and setting fields.//from w  ww  .j  a v  a  2s . c o m
 */
@Test
public void fields() {
    Random random = new Random();
    Object body = new Object();
    byte[] bytes = new byte[5];
    random.nextBytes(bytes);
    InputStream inputStream = new ByteArrayInputStream(bytes);
    boolean prettyPrint = random.nextBoolean();
    TestCaseOptions options = TestCaseOptions.defaults();
    String md5 = RandomStringUtils.random(64);

    options.setBody(body);
    options.setInputStream(inputStream);
    options.setPrettyPrint(prettyPrint);
    options.setMD5(md5);

    Assert.assertEquals(body, options.getBody());
    Assert.assertEquals(inputStream, options.getInputStream());
    Assert.assertEquals(prettyPrint, options.isPrettyPrint());
    Assert.assertEquals(md5, options.getMD5());
}

From source file:com.moesol.keys.EncodeUidsTest.java

public void testSha1Compression() throws NoSuchAlgorithmException, IOException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream out = makeCompressStream(baos);
    try {/*from  ww w. j  a  v  a  2s  .co m*/
        for (int i = 0; i < 128; i++) {
            Random r = new Random();
            byte[] bytes = new byte[20];
            r.nextBytes(bytes);
            byte[] sha1 = digest.digest(bytes);
            out.write(sha1);
        }
    } finally {
        out.close();
    }
    System.out.println("sha1 deflate: " + baos.toByteArray().length);
}

From source file:com.bigdata.dastor.dht.CollatingOrderPreservingPartitioner.java

public BytesToken getRandomToken() {
    Random r = new Random();
    byte[] buffer = new byte[16];
    r.nextBytes(buffer);
    return new BytesToken(buffer);
}

From source file:com.github.aelstad.keccakj.fips202.KeccackDigestTestUtils.java

public void testPerformance(AbstractKeccackMessageDigest messageDigest) {
    int rounds = 128;
    byte[] buf = new byte[2 * 1024 * 1024];
    Random random = new Random();
    random.nextBytes(buf);
    long startTs;
    long stopTs;//from  www  .j  a  va 2 s .c  o  m
    long digestTime = 0;
    byte[] digest = messageDigest.digest(buf);
    for (int i = 0; i < rounds; ++i) {
        System.arraycopy(digest, 0, buf, 0, digest.length);
        startTs = System.currentTimeMillis();
        digest = messageDigest.digest(buf);
        stopTs = System.currentTimeMillis();
        digestTime += (stopTs - startTs);
    }
    System.out.println("Performance of " + messageDigest.getAlgorithm() + ": "
            + ((double) ((buf.length * ((long) rounds)) * 1000)) / ((double) digestTime * 1024 * 1024)
            + " MB/s");
}

From source file:com.github.aelstad.keccakj.fips202.KeccackDigestTestUtils.java

public void testPerformance(KeccackSponge sponge) {
    int rounds = 128;
    byte[] buf = new byte[2 * 1024 * 1024];
    Random random = new Random();
    random.nextBytes(buf);
    long startTs;
    long stopTs;// w ww  .  j a v  a 2 s  . c  o m
    long digestTime = 0;
    byte[] digest = new byte[16];
    for (int i = 0; i < rounds; ++i) {
        System.arraycopy(digest, 0, buf, 0, digest.length);
        startTs = System.currentTimeMillis();
        sponge.getAbsorbStream().write(buf, 0, buf.length);
        sponge.getSqueezeStream().read(digest);
        stopTs = System.currentTimeMillis();
        digestTime += (stopTs - startTs);
    }
    System.out.println("Performance of sponge with capacity " + (1600 - sponge.getRateBits()) + ": "
            + ((double) ((buf.length * ((long) rounds)) * 1000)) / ((double) digestTime * 1024 * 1024)
            + " MB/s");
}

From source file:svnserver.TemporaryOutputStreamTest.java

@Test
public void checkLifeTime() throws IOException {
    final byte[] expectedData = new byte[MAX_MEMORY_SIZE * 2];
    final Random random = new Random(0);
    random.nextBytes(expectedData);

    final TemporaryOutputStream outputStream = new TemporaryOutputStream(MAX_MEMORY_SIZE);
    Assert.assertNull(outputStream.tempFile());
    outputStream.write(expectedData);//  w  w w  .  j a  v  a 2 s. c  om
    checkFileExists(outputStream, true);

    final InputStream inputStream1 = outputStream.toInputStream();
    final InputStream inputStream2 = outputStream.toInputStream();

    final byte[] actualData1 = IOUtils.toByteArray(inputStream1, expectedData.length);
    inputStream1.close();
    inputStream1.close();

    outputStream.close();
    outputStream.close();

    final byte[] actualData2 = IOUtils.toByteArray(inputStream2, expectedData.length);
    checkFileExists(outputStream, true);
    inputStream2.close();
    checkFileExists(outputStream, false);
    inputStream2.close();
    checkFileExists(outputStream, false);

    ArrayAsserts.assertArrayEquals(actualData1, expectedData);
    ArrayAsserts.assertArrayEquals(actualData2, expectedData);
}

From source file:com.github.aelstad.keccakj.fips202.KeccakDigestTestUtils.java

public void testPerformance(AbstractKeccakMessageDigest messageDigest) {
    int rounds = 128;
    byte[] buf = new byte[2 * 1024 * 1024];
    Random random = new Random();
    random.nextBytes(buf);
    long startTs;
    long stopTs;//  w w  w. jav  a2  s .c  o  m
    long digestTime = 0;
    byte[] digest = messageDigest.digest(buf);
    for (int i = 0; i < rounds; ++i) {
        System.arraycopy(digest, 0, buf, 0, digest.length);
        startTs = System.currentTimeMillis();
        digest = messageDigest.digest(buf);
        stopTs = System.currentTimeMillis();
        digestTime += (stopTs - startTs);
    }
    System.out.println("Performance of " + messageDigest.getAlgorithm() + ": "
            + ((double) ((buf.length * ((long) rounds)) * 1000)) / ((double) digestTime * 1024 * 1024)
            + " MB/s");
}

From source file:com.github.aelstad.keccakj.fips202.KeccakDigestTestUtils.java

public void testPerformance(KeccakSponge sponge) {
    int rounds = 128;
    byte[] buf = new byte[2 * 1024 * 1024];
    Random random = new Random();
    random.nextBytes(buf);
    long startTs;
    long stopTs;/*  w w w.j ava  2s.c o  m*/
    long digestTime = 0;
    byte[] digest = new byte[16];
    for (int i = 0; i < rounds; ++i) {
        System.arraycopy(digest, 0, buf, 0, digest.length);
        startTs = System.currentTimeMillis();
        sponge.getAbsorbStream().write(buf, 0, buf.length);
        sponge.getSqueezeStream().read(digest);
        stopTs = System.currentTimeMillis();
        digestTime += (stopTs - startTs);
    }
    System.out.println("Performance of sponge with capacity " + (1600 - sponge.getRateBits()) + ": "
            + ((double) ((buf.length * ((long) rounds)) * 1000)) / ((double) digestTime * 1024 * 1024)
            + " MB/s");
}

From source file:org.apache.wink.itest.standard.JAXRSFileTest.java

/**
 * Tests posting to a File entity parameter.
 * /*from www.ja v a 2  s.c om*/
 * @throws HttpException
 * @throws IOException
 */
public void testPostFile() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/file");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "text/plain"));
    postMethod.addRequestHeader("Accept", "text/plain");
    try {
        client.executeMethod(postMethod);

        assertEquals(200, postMethod.getStatusCode());
        InputStream is = postMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(1000,
                Integer.valueOf(postMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        postMethod.releaseConnection();
    }

    /* TODO : need to test that any temporary files created are deleted */
}