List of usage examples for java.util Random nextBytes
public void nextBytes(byte[] bytes)
From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java
/** * Tests receiving a StreamingOutput with a non-standard content-type. * //from w w w .j ava 2s . c o m * @throws HttpException * @throws IOException */ public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput"); byte[] barr = new byte[100000]; 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/streamingoutput"); getMethod.addRequestHeader("Accept", "mytype/subtype"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.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("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue()); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:syndeticlogic.catena.utility.FixedLengthArrayGenerator.java
public void generateFileArray() throws Exception { file = new File(filename); FileOutputStream out = new FileOutputStream(file); out.getChannel().truncate(0);//w w w .j ava 2s.co m out.flush(); Random localRandom = new Random(seed); for (int i = 0; i < elementLength; i++) { byte[] buffer = new byte[elementSize]; localRandom.nextBytes(buffer); out.write(buffer); out.flush(); if (log.isTraceEnabled()) { System.out.println("generate file " + i + " ==================================================================="); for (int k = 0; k < buffer.length; k++) { System.out.print((Integer.toHexString((int) buffer[k] & 0xff)) + ", "); } System.out.println(); } } out.close(); }
From source file:com.microsoft.azure.servicebus.samples.prefetch.Prefetch.java
long sendAndReceiveMessages(IMessageSender sender, IMessageReceiver receiver, int messageCount) throws Exception { // Now we can start sending messages. Random rnd = new Random(); byte[] mockPayload = new byte[100]; // 100 random-byte payload rnd.nextBytes(mockPayload); System.out.printf("\nSending %d messages to the queue\n", messageCount); ArrayList<CompletableFuture<Void>> sendOps = new ArrayList<>(); for (int i = 0; i < messageCount; i++) { IMessage message = new Message(mockPayload); message.setTimeToLive(Duration.ofMinutes(5)); sendOps.add(sender.sendAsync(message)); }/*from w ww . j a va2s. co m*/ CompletableFuture.allOf(sendOps.toArray(new CompletableFuture<?>[sendOps.size()])).join(); System.out.printf("Send completed\n"); // Receive the messages System.out.printf("Receiving messages...\n"); // Start stopwatch Stopwatch stopWatch = Stopwatch.createStarted(); IMessage receivedMessage = receiver.receive(Duration.ofSeconds(5)); while (receivedMessage != null) { // here's where you'd do any work // complete (round trips) receiver.complete(receivedMessage.getLockToken()); if (--messageCount <= 0) break; // now get the next message receivedMessage = receiver.receive(Duration.ofSeconds(5)); } // Stop the stopwatch stopWatch.stop(); System.out.printf("Receive completed\n"); long timeTaken = stopWatch.elapsed(TimeUnit.MILLISECONDS); System.out.printf("Time to receive and complete all messages = %d milliseconds\n", timeTaken); return timeTaken; }
From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java
/** * Tests putting and then getting a StreamingOutput. * //from w ww . j av a2s . co m * @throws HttpException * @throws IOException */ public void testPutStreamngOutput() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput"); byte[] barr = new byte[100000]; 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/streamingoutput"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.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]); } String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod.getResponseHeader("Content-Type").getValue(); assertNotNull(contentType, contentType); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java
/** * Tests posting a byte array.//from www .ja v a 2 s . com * * @throws HttpException * @throws IOException */ public void testPostByteArray() throws HttpException, IOException { HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/bytesarray"); 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(barr.length, Integer.valueOf(postMethod.getResponseHeader("Content-Length").getValue()).intValue()); } finally { postMethod.releaseConnection(); } }
From source file:com.github.jinahya.codec.PercentBinaryDecoderProxyTest.java
@Test(invocationCount = 128) public void testDecode() throws Exception { final BinaryDecoder decoder = (BinaryDecoder) PercentBinaryDecoderProxy.newInstance(); try {/*from w w w . j av a 2 s . com*/ decoder.decode((Object) null); Assert.fail("passed: decode((Object) null)"); } catch (NullPointerException npe) { // ok } try { decoder.decode((byte[]) null); Assert.fail("passed: decode((byte[]) null)"); } catch (NullPointerException npe) { // ok } final Random random = ThreadLocalRandom.current(); final byte[] expected = new byte[random.nextInt(128)]; random.nextBytes(expected); System.out.println("original ----------------------------------------"); System.out.println(Base64.encodeBase64String(expected)); final byte[] encoded = PercentEncoder.encodeMultiple(expected); System.out.println("encoded -----------------------------------------"); System.out.println(new String(encoded, "US-ASCII")); final byte[] actual = decoder.decode(encoded); System.out.println("decoded -----------------------------------------"); System.out.println(Base64.encodeBase64String(actual)); Assert.assertEquals(actual, expected); }
From source file:com.github.lukaszbudnik.dqueue.QueueClientPerformanceTest.java
@Test public void doIt5Filters() throws ExecutionException, InterruptedException { byte[] data = new byte[2045]; Random r = new Random(); r.nextBytes(data); ByteBuffer buffer = ByteBuffer.wrap(data); Map<String, String> filters = ImmutableMap.of( // f1 "f1", Long.toHexString(r.nextLong()), // f2 "f2", Long.toHexString(r.nextLong()), // f3 "f3", Long.toHexString(r.nextLong()), // f4 "f4", Long.toHexString(r.nextLong()), // f5 "f5", Long.toHexString(r.nextLong())); IntStream.range(0, NUMBER_OF_ITERATIONS).forEach((i) -> { UUID startTime = UUIDs.timeBased(); Future<UUID> id = queueClient.publish(new Item(startTime, buffer, filters)); try {// w ww.j a va2 s.c o m Assert.assertEquals(startTime, id.get()); } catch (Exception e) { fail(e.getMessage()); } }); IntStream.range(0, NUMBER_OF_ITERATIONS).forEach((i) -> { Future<Optional<Item>> itemFuture = queueClient.consume(filters); Optional<Item> item = null; try { item = itemFuture.get(); } catch (Exception e) { fail(e.getMessage()); } assertTrue(item.isPresent()); }); }
From source file:com.ethercamp.harmony.keystore.KeystoreFormat.java
private byte[] generateRandomBytes(int size) { final byte[] bytes = new byte[size]; Random random = new Random(); random.nextBytes(bytes); return bytes; }
From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java
/** * Tests receiving an empty byte array./*from w w w.j a v a 2 s .c om*/ * * @throws HttpException * @throws IOException */ public void testWithRequestAcceptHeaderWillReturnRequestedContentType() 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), "any/type")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/bytesarray"); 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.atricore.idbus.kernel.main.util.AbstractIdGenerator.java
/** * Generate a byte array containing a assertion identifier *///from w w w . j a v a2 s. c om protected void getRandomBytes(byte[] bytes) { // Performance may be improved by using O.S. Specific devices like /dev/urandom (see tomcat 5.0) Random random = getRandom(); random.nextBytes(bytes); }