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:corner.encrypt.services.impl.CipherKey.java

public void createRandomCipher() {
    SecureRandom secureRandom = new SecureRandom();
    Random random = new Random(secureRandom.nextLong());
    cipher = new byte[cipherLen];
    random.nextBytes(cipher);
    persistCipher();//from w  w w . j a v a 2s .  c  o m
}

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

/**
 * Tests receiving an empty byte array./*w  ww  .  j av a2  s  .c  o  m*/
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/file");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/file");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.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("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.nickelproject.util.IoUtilTest.java

@Test
public void testDeflateMiscData() throws Exception {
    final Random vRandomizer = new Random(1234567);
    final byte[] vData = new byte[testSize];
    vRandomizer.nextBytes(vData);
    testDeflateInflate(vData);//  ww  w. j a va  2  s .  c om
}

From source file:com.nesscomputing.cache.TestBase64Compatible.java

@Test
public void testEqualEncoding() {
    final Random rand = new Random();

    for (int i = 0; i < 100; i++) {
        final byte[] bytes = new byte[i];
        rand.nextBytes(bytes);

        final byte[] base64Bytes = Base64.encode(bytes);
        final byte[] apache64Bytes = new org.apache.commons.codec.binary.Base64().encode(bytes);

        Assert.assertEquals(base64Bytes.length, apache64Bytes.length);
        for (int j = 0; j < base64Bytes.length; j++) {
            Assert.assertEquals(base64Bytes[j], apache64Bytes[j]);
        }/*  w w  w.j a  va 2  s.co  m*/
    }
}

From source file:com.nesscomputing.cache.TestBase64Compatible.java

@Test
public void testEncoding() {
    final Random rand = new Random();

    for (int i = 0; i < 100; i++) {
        final byte[] bytes = new byte[i];
        rand.nextBytes(bytes);

        final byte[] base64Bytes = Base64.encode(bytes);
        final byte[] decoded = new org.apache.commons.codec.binary.Base64().decode(base64Bytes);

        Assert.assertEquals(bytes.length, decoded.length);
        for (int j = 0; j < bytes.length; j++) {
            Assert.assertEquals(bytes[j], decoded[j]);
        }//from w  ww . j a  va2 s  .c o m

    }
}

From source file:com.nesscomputing.cache.TestBase64Compatible.java

@Test
public void testDecode() {
    final Random rand = new Random();

    for (int i = 0; i < 100; i++) {
        final byte[] bytes = new byte[i];
        rand.nextBytes(bytes);

        final byte[] base64Bytes = new org.apache.commons.codec.binary.Base64().encode(bytes);
        final byte[] decoded = Base64.decode(base64Bytes);

        Assert.assertEquals(bytes.length, decoded.length);
        for (int j = 0; j < bytes.length; j++) {
            Assert.assertEquals(bytes[j], decoded[j]);
        }//  w  ww  . jav  a  2 s  . c om

    }
}

From source file:my.adam.smo.EncryptionTest.java

@Test
public void symmetricEncryptionGaveDifferentCryptogramForSamePlainText() {
    ApplicationContext clientContext = new ClassPathXmlApplicationContext("Context.xml");
    SymmetricEncryptionBox box = clientContext.getBean(SymmetricEncryptionBox.class);
    int plainTextLength = 17;
    Random random = new Random();

    byte[] plainText = new byte[plainTextLength];
    random.nextBytes(plainText);

    byte[] cryptogram1 = box.encrypt(plainText);
    byte[] cryptogram2 = box.encrypt(plainText);

    Assert.assertFalse("cryptograms are same", Arrays.equals(cryptogram1, cryptogram2));

    byte[] decrypted1 = box.decrypt(cryptogram1);
    Assert.assertTrue("unable to decrypt", Arrays.equals(plainText, decrypted1));

    byte[] decrypted2 = box.decrypt(cryptogram2);
    Assert.assertTrue("unable to decrypt", Arrays.equals(plainText, decrypted2));
}

From source file:org.apache.hadoop.hdfs.TestSetTimes.java

private FSDataOutputStream writeFile(FileSystem fileSys, Path name, int repl) throws IOException {
    FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, (long) blockSize);
    byte[] buffer = new byte[fileSize];
    Random rand = new Random(seed);
    rand.nextBytes(buffer);
    stm.write(buffer);//from ww  w.ja  v a 2  s  .c o m
    return stm;
}

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

/**
 * Tests posting to a StreamingOutput and then returning StreamingOutput.
 * //from  w w w  .j  a va  2 s  .c  o  m
 * @throws HttpException
 * @throws IOException
 */
public void testPostStreamingOutput() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/streamingoutput");
    byte[] barr = new byte[100000];
    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[barr.length];
        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());
        Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:com.github.lukaszbudnik.dqueue.QueueClientPerformanceTest.java

@Test
public void doItNoFilters() throws ExecutionException, InterruptedException {

    byte[] data = new byte[2045];
    Random r = new Random();
    r.nextBytes(data);
    ByteBuffer buffer = ByteBuffer.wrap(data);

    IntStream.range(0, NUMBER_OF_ITERATIONS).forEach((i) -> {
        UUID startTime = UUIDs.timeBased();
        Future<UUID> id = queueClient.publish(new Item(startTime, buffer));
        try {// w  ww .j  a  v a 2  s  .c  o m
            id.get();
        } catch (Exception e) {
            fail(e.getMessage());
        }
    });

    IntStream.range(0, NUMBER_OF_ITERATIONS).forEach((i) -> {
        Future<Optional<Item>> itemFuture = queueClient.consume();

        Optional<Item> item = null;
        try {
            item = itemFuture.get();
        } catch (Exception e) {
            fail(e.getMessage());
        }

        assertTrue(item.isPresent());
    });

}