List of usage examples for java.util Random nextBytes
public void nextBytes(byte[] bytes)
From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java
/** * Tests putting and then getting a byte array. * //from w w w. j a v a 2 s.co m * @throws HttpException * @throws IOException */ public void testPutByteArray() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/bytesarray"); 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/bytesarray"); 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); assertEquals(barr.length, Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue()); } finally { getMethod.releaseConnection(); } }
From source file:com.lightboxtechnologies.io.IOUtilsTest.java
@Test(expected = IOException.class) public void testCopyExactTooLittle() throws IOException { final byte[] buf = new byte[1024]; final byte[] expected = new byte[9999]; final long seed = System.currentTimeMillis(); final Random rng = new Random(seed); rng.nextBytes(expected); final ByteArrayInputStream in = new ByteArrayInputStream(expected); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out, buf, 10000);//from w ww.j a v a2 s. co m }
From source file:org.sipfoundry.sipxconfig.domain.Domain.java
/** * Initialize domain secret if it's not initialized yet * * @return true if new secret has been initialized, false if secret was already there *//* w w w . j av a 2 s . c om*/ protected boolean initSecret() { if (StringUtils.isNotBlank(m_sharedSecret)) { return false; } Random random = new Random(System.currentTimeMillis()); byte[] secretBytes = new byte[SECRET_SIZE]; random.nextBytes(secretBytes); m_sharedSecret = new String(new Base64().encode(secretBytes)); return true; }
From source file:pl.p.lodz.ftims.server.logic.ChallengeServiceTest.java
/** * Test of createChallenge method, of class ChallengeService. *///from www. j a va2 s. c om @Test public void testCreateChallenge() throws Exception { System.out.println("createChallenge"); dataModel.Challenge challengeData = new dataModel.Challenge(); challengeData.setName("testowe"); challengeData.setDescription("Opis testowy"); challengeData.setLocation(new Coordinates(45.0, 100.0)); challengeData.setPassword("passwd"); challengeData.setPoints(20); challengeData.setSecretPassword("secPasswd"); challengeData.setStatus(false); byte[] photo = new byte[1500000]; Random r = new Random(); r.nextBytes(photo); challengeData.setPhoto(photo); List<KHint> khints = new ArrayList<>(5); for (int i = 0; i < 5; ++i) { khints.add(new KHint("hint" + i, null, 10 + i)); } challengeData.setHints(khints); challengeService.createChallenge(challengeData); }
From source file:com.lightboxtechnologies.io.IOUtilsTest.java
@Test public void testCopyExactFull() throws IOException { final byte[] buf = new byte[1024]; final byte[] expected = new byte[10000]; final long seed = System.currentTimeMillis(); final Random rng = new Random(seed); rng.nextBytes(expected); final ByteArrayInputStream in = new ByteArrayInputStream(expected); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out, buf, 10000);/*from w ww.j a va2s . c om*/ assertArrayEquals("seed == " + seed, expected, out.toByteArray()); }
From source file:com.lightboxtechnologies.io.IOUtilsTest.java
@Test public void testCopyBuffer() throws IOException { final byte[] buf = new byte[1024]; final byte[] expected = new byte[10000]; final long seed = System.currentTimeMillis(); final Random rng = new Random(seed); rng.nextBytes(expected); final ByteArrayInputStream in = new ByteArrayInputStream(expected); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final int count = IOUtils.copy(in, out, buf); assertEquals("seed == " + seed, expected.length, count); assertArrayEquals("seed == " + seed, expected, out.toByteArray()); }
From source file:com.lightboxtechnologies.io.IOUtilsTest.java
@Test public void testCopyLargeBuffer() throws IOException { final byte[] buf = new byte[1024]; final byte[] expected = new byte[10000]; final long seed = System.currentTimeMillis(); final Random rng = new Random(seed); rng.nextBytes(expected); final ByteArrayInputStream in = new ByteArrayInputStream(expected); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final long count = IOUtils.copyLarge(in, out, buf); assertEquals("seed == " + seed, expected.length, count); assertArrayEquals("seed == " + seed, expected, out.toByteArray()); }
From source file:org.apache.hadoop.mapred.TestFadvisedFileRegion.java
@Test(timeout = 100000) public void testCustomShuffleTransfer() throws IOException { File absLogDir = new File("target", TestFadvisedFileRegion.class.getSimpleName() + "LocDir") .getAbsoluteFile();//from w ww .j a v a 2 s .co m String testDirPath = StringUtils.join(Path.SEPARATOR, new String[] { absLogDir.getAbsolutePath(), "testCustomShuffleTransfer" }); File testDir = new File(testDirPath); testDir.mkdirs(); System.out.println(testDir.getAbsolutePath()); File inFile = new File(testDir, "fileIn.out"); File outFile = new File(testDir, "fileOut.out"); //Initialize input file byte[] initBuff = new byte[FILE_SIZE]; Random rand = new Random(); rand.nextBytes(initBuff); FileOutputStream out = new FileOutputStream(inFile); try { out.write(initBuff); } finally { IOUtils.cleanup(LOG, out); } //define position and count to read from a file region. int position = 2 * 1024 * 1024; int count = 4 * 1024 * 1024 - 1; RandomAccessFile inputFile = null; RandomAccessFile targetFile = null; WritableByteChannel target = null; FadvisedFileRegion fileRegion = null; try { inputFile = new RandomAccessFile(inFile.getAbsolutePath(), "r"); targetFile = new RandomAccessFile(outFile.getAbsolutePath(), "rw"); target = targetFile.getChannel(); Assert.assertEquals(FILE_SIZE, inputFile.length()); //create FadvisedFileRegion fileRegion = new FadvisedFileRegion(inputFile, position, count, false, 0, null, null, 1024, false); //test corner cases customShuffleTransferCornerCases(fileRegion, target, count); long pos = 0; long size; while ((size = fileRegion.customShuffleTransfer(target, pos)) > 0) { pos += size; } //assert size Assert.assertEquals(count, (int) pos); Assert.assertEquals(count, targetFile.length()); } finally { if (fileRegion != null) { fileRegion.releaseExternalResources(); } IOUtils.cleanup(LOG, target); IOUtils.cleanup(LOG, targetFile); IOUtils.cleanup(LOG, inputFile); } //Read the target file and verify that copy is done correctly byte[] buff = new byte[FILE_SIZE]; FileInputStream in = new FileInputStream(outFile); try { int total = in.read(buff, 0, count); Assert.assertEquals(count, total); for (int i = 0; i < count; i++) { Assert.assertEquals(initBuff[position + i], buff[i]); } } finally { IOUtils.cleanup(LOG, in); } //delete files and folders inFile.delete(); outFile.delete(); testDir.delete(); absLogDir.delete(); }
From source file:com.lightboxtechnologies.io.IOUtilsTest.java
@Test public void testCopyExactNotFull() throws IOException { final byte[] buf = new byte[1024]; final byte[] expected = new byte[10000]; final long seed = System.currentTimeMillis(); final Random rng = new Random(seed); rng.nextBytes(expected); final ByteArrayInputStream in = new ByteArrayInputStream(expected); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out, buf, 9999);// w ww . j a va2 s .c om final byte[] actual = out.toByteArray(); assertEquals("seed == " + seed, 9999, actual.length); for (int i = 0; i < actual.length; ++i) { assertEquals("seed == " + seed, expected[i], actual[i]); } }
From source file:gobblin.util.io.StreamUtilsTest.java
@Test public void testRegularByteBufferToStream() throws IOException { final int BUF_LEN = 128000; Random random = new Random(); byte[] srcBytes = new byte[BUF_LEN]; random.nextBytes(srcBytes); // allocate a larger size than we actually use to make sure we don't copy off the end of the used portion of the // buffer/* w w w .j av a 2 s . co m*/ ByteBuffer buffer = ByteBuffer.allocate(BUF_LEN * 2); verifyBuffer(srcBytes, buffer); // now try with direct buffers; they aren't array backed so codepath is different buffer = ByteBuffer.allocateDirect(BUF_LEN * 2); verifyBuffer(srcBytes, buffer); }