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.apache.hadoop.hdfs.TestRaidDfs.java

public static long createTestFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize)
        throws IOException {
    CRC32 crc = new CRC32();
    Random rand = new Random();
    FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, blocksize);
    // fill random data into file
    final byte[] b = new byte[(int) blocksize];
    for (int i = 0; i < numBlocks; i++) {
        rand.nextBytes(b);
        stm.write(b);//  www .  j  av a 2 s  .co m
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

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

/**
 * Tests posting to a DataSource entity parameter.
 * //from   w ww.  j av  a  2s.  c  o m
 * @throws HttpException
 * @throws IOException
 */
public void testPostDataSource() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/datasource");
    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());
        assertNull(
                (postMethod.getResponseHeader("Content-Length") == null) ? ""
                        : postMethod.getResponseHeader("Content-Length").getValue(),
                postMethod.getResponseHeader("Content-Length"));
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:com.koda.integ.hbase.test.BlockCacheSimpleTest.java

/**
 * Gets the value./*  w  ww  . j a v  a 2  s .c  om*/
 *
 * @param size the size
 * @return the value
 */
private ByteArrayCacheable getValue(int size) {
    Random r = new Random(size);
    byte[] array = new byte[size];
    r.nextBytes(array);
    return new ByteArrayCacheable(array);
}

From source file:org.lealone.cluster.dht.ByteOrderedPartitioner.java

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

From source file:be.tutul.naheulcraft.launcher.auth.Auth.java

private Cipher getCipher(int paramInt, String key) throws Exception {
    Random random = new Random(43287234L);
    byte[] salt = new byte[8];
    random.nextBytes(salt);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);

    SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
            .generateSecret(new PBEKeySpec(key.toCharArray()));
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(paramInt, pbeKey, pbeParamSpec);

    return cipher;
}

From source file:org.apache.hadoop.dfs.DFSTestUtil.java

/** check if the files have been copied correctly. */
public boolean checkFiles(FileSystem fs, String topdir) throws IOException {

    //Configuration conf = new Configuration();
    Path root = new Path(topdir);

    for (int idx = 0; idx < nFiles; idx++) {
        Path fPath = new Path(root, files[idx].getName());
        FSDataInputStream in = fs.open(fPath);
        byte[] toRead = new byte[files[idx].getSize()];
        byte[] toCompare = new byte[files[idx].getSize()];
        Random rb = new Random(files[idx].getSeed());
        rb.nextBytes(toCompare);
        in.readFully(0, toRead);//from   ww  w  .  ja  v a 2s .co m
        in.close();
        for (int i = 0; i < toRead.length; i++) {
            if (toRead[i] != toCompare[i]) {
                return false;
            }
        }
        toRead = null;
        toCompare = null;
    }

    return true;
}

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

/**
 * Tests receiving a DataSource with any media type.
 * /*  ww  w. ja  va2s.  co m*/
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/datasource");
    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/datasource");
    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());
        assertNull(
                (getMethod.getResponseHeader("Content-Length") == null) ? ""
                        : getMethod.getResponseHeader("Content-Length").getValue(),
                getMethod.getResponseHeader("Content-Length"));
    } finally {
        getMethod.releaseConnection();
    }
}

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

/**
 * Tests putting and then getting a DataSource entity.
 * //  ww  w .  j  a va  2 s.  co  m
 * @throws HttpException
 * @throws IOException
 */
public void testPutDataSource() throws HttpException, IOException {
    HttpClient client = new HttpClient();

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

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/datasource");
    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]);
        }

        String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null
                : getMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
        assertNull(
                (getMethod.getResponseHeader("Content-Length") == null) ? ""
                        : getMethod.getResponseHeader("Content-Length").getValue(),
                getMethod.getResponseHeader("Content-Length"));
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.flink.runtime.state.FileStateBackendTest.java

@Test
public void testStateOutputStream() throws IOException {
    File basePath = tempFolder.newFolder().getAbsoluteFile();

    try {/*from  w ww  .j a  v a  2  s  . com*/
        // the state backend has a very low in-mem state threshold (15 bytes)
        FsStateBackend backend = CommonTestUtils
                .createCopySerializable(new FsStateBackend(basePath.toURI(), 15));
        JobID jobId = new JobID();

        // we know how FsCheckpointStreamFactory is implemented so we know where it
        // will store checkpoints
        File checkpointPath = new File(basePath.getAbsolutePath(), jobId.toString());

        CheckpointStreamFactory streamFactory = backend.createStreamFactory(jobId, "test_op");

        byte[] state1 = new byte[1274673];
        byte[] state2 = new byte[1];
        byte[] state3 = new byte[0];
        byte[] state4 = new byte[177];

        Random rnd = new Random();
        rnd.nextBytes(state1);
        rnd.nextBytes(state2);
        rnd.nextBytes(state3);
        rnd.nextBytes(state4);

        long checkpointId = 97231523452L;

        CheckpointStreamFactory.CheckpointStateOutputStream stream1 = streamFactory
                .createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());

        CheckpointStreamFactory.CheckpointStateOutputStream stream2 = streamFactory
                .createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());

        CheckpointStreamFactory.CheckpointStateOutputStream stream3 = streamFactory
                .createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());

        stream1.write(state1);
        stream2.write(state2);
        stream3.write(state3);

        FileStateHandle handle1 = (FileStateHandle) stream1.closeAndGetHandle();
        ByteStreamStateHandle handle2 = (ByteStreamStateHandle) stream2.closeAndGetHandle();
        ByteStreamStateHandle handle3 = (ByteStreamStateHandle) stream3.closeAndGetHandle();

        // use with try-with-resources
        StreamStateHandle handle4;
        try (CheckpointStreamFactory.CheckpointStateOutputStream stream4 = streamFactory
                .createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis())) {
            stream4.write(state4);
            handle4 = stream4.closeAndGetHandle();
        }

        // close before accessing handle
        CheckpointStreamFactory.CheckpointStateOutputStream stream5 = streamFactory
                .createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());
        stream5.write(state4);
        stream5.close();
        try {
            stream5.closeAndGetHandle();
            fail();
        } catch (IOException e) {
            // uh-huh
        }

        validateBytesInStream(handle1.openInputStream(), state1);
        handle1.discardState();
        assertFalse(isDirectoryEmpty(basePath));
        ensureLocalFileDeleted(handle1.getFilePath());

        validateBytesInStream(handle2.openInputStream(), state2);
        handle2.discardState();

        // nothing was written to the stream, so it will return nothing
        assertNull(handle3);

        validateBytesInStream(handle4.openInputStream(), state4);
        handle4.discardState();
        assertTrue(isDirectoryEmpty(checkpointPath));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.aries.util.UUIDGeneratorFromJBoss.java

public final byte[] generateDummyAddress() {
    Random rnd = getRandomNumberGenerator();
    byte[] dummy = new byte[6];
    rnd.nextBytes(dummy);
    /* Need to set the broadcast bit to indicate it's not a real
     * address./*from   w  w  w.  j  a va 2  s  .  c  o  m*/
     */
    dummy[0] |= (byte) 0x01;

    if (UUIDGeneratorFromJBoss.log.isDebugEnabled()) {
        UUIDGeneratorFromJBoss.log.debug("using dummy address " + UUIDGeneratorFromJBoss.asString(dummy));
    }
    return dummy;
}