Java Code Examples for java.util.Random#nextBytes()

The following examples show how to use java.util.Random#nextBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Benchmark.java    From GeoIP2-java with Apache License 2.0 6 votes vote down vote up
private static void bench(DatabaseReader r, int count, int seed) throws GeoIp2Exception, UnknownHostException {
    Random random = new Random(seed);
    long startTime = System.nanoTime();
    byte[] address = new byte[4];
    for (int i = 0; i < count; i++) {
        random.nextBytes(address);
        InetAddress ip = InetAddress.getByAddress(address);
        CityResponse t;
        try {
            t = r.city(ip);
        } catch (AddressNotFoundException | IOException e) {
        }
        if (TRACE) {
            if (i % 50000 == 0) {
                System.out.println(i + " " + ip);
                System.out.println(t);
            }
        }
    }
    long endTime = System.nanoTime();

    long duration = endTime - startTime;
    long qps = count * 1000000000L / duration;
    System.out.println("Requests per second: " + qps);
}
 
Example 2
Source File: Murmur3_x86_32Suite.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void randomizedStressTestBytes() {
  int size = 65536;
  Random rand = new Random();

  // A set used to track collision rate.
  Set<Integer> hashcodes = new HashSet<Integer>();
  for (int i = 0; i < size; i++) {
    int byteArrSize = rand.nextInt(100) * 8;
    byte[] bytes = new byte[byteArrSize];
    rand.nextBytes(bytes);

    Assert.assertEquals(
      hasher.hashUnsafeWords(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize),
      hasher.hashUnsafeWords(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize));

    hashcodes.add(hasher.hashUnsafeWords(
      bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize));
  }

  // A very loose bound.
  Assert.assertTrue(hashcodes.size() > size * 0.95);
}
 
Example 3
Source File: TestDefaultContainerExecutor.java    From big-c with Apache License 2.0 6 votes vote down vote up
byte[] createTmpFile(Path dst, Random r, int len)
    throws IOException {
  // use unmodified local context
  FileContext lfs = FileContext.getLocalFSFileContext();
  dst = lfs.makeQualified(dst);
  lfs.mkdir(dst.getParent(), null, true);
  byte[] bytes = new byte[len];
  FSDataOutputStream out = null;
  try {
    out = lfs.create(dst, EnumSet.of(CREATE, OVERWRITE));
    r.nextBytes(bytes);
    out.write(bytes);
  } finally {
    if (out != null) out.close();
  }
  return bytes;
}
 
Example 4
Source File: ByteStorageConversionCheck.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testRandomConversion() {
	byte[] bin = new byte[102400];
	char[] cin = new char[102400];
	byte[] bout = new byte[102400];
	
	Random r = new Random();
	r.nextBytes(bin);
	
	ByteStorageConversion.toChar(bin, 0, cin, 0, bin.length);
	ByteStorageConversion.toByte(cin, 0, bout, 0, cin.length);
	
	for ( int i = 0; i < bin.length; i++ ) {
		if ( bin[i] !=  bout[i] ) {
			Assert.assertEquals("Internal Byte conversion failed at "+bin[i]+"=>"+(int)cin[i]+"=>"+bout[i],bin[i],bout[i]);
		}
	}
	log.info("Internal Byte (random set) conversion test Passed Ok");
}
 
Example 5
Source File: ZipUtilsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp() throws IOException {
  zipFile = File.createTempFile("test", "zip");
  zipFile.deleteOnExit();

  testData = new byte[2048];
  Random random = new Random();
  random.nextBytes(testData);

  try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
    ZipEntry entry = new ZipEntry(FILE_NAME);
    entry.setSize(testData.length);
    zos.putNextEntry(entry);
    zos.write(testData);
    zos.closeEntry();
    zos.close();
  }
}
 
Example 6
Source File: AddressUtilTest.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddressConversions()
  throws ValidationException
{
  ArrayList<NetworkParams> plist = new ArrayList<NetworkParams>();
  plist.add(new NetworkParamsProd());
  plist.add(new NetworkParamsTestnet());
  plist.add(new NetworkParamsRegtest());

  byte[] buff = new byte[Globals.ADDRESS_SPEC_HASH_LEN];
  Random rnd = new Random();
  HashSet<String> addresses = new HashSet<>();

  for(int i=0; i<1000; i++)
  {
    if (i > 0)
    {
      rnd.nextBytes(buff);
    }

    AddressSpecHash spec = new AddressSpecHash(buff);
    for(NetworkParams p : plist)
    {
      String addr = spec.toAddressString(p);
      Assert.assertFalse(addresses.contains(addr));
      addresses.add(addr);
      System.out.println("Address: "  + addr);

      AddressSpecHash dec = new AddressSpecHash(addr, p);
      Assert.assertEquals(spec, dec);


      int colon = addr.indexOf(":");
      String without = addr.substring(colon+1);

      AddressSpecHash dec2 = new AddressSpecHash(without, p);
      Assert.assertEquals(spec, dec2);
    }
  }
}
 
Example 7
Source File: PESectionTest.java    From PortEx with Apache License 2.0 5 votes vote down vote up
@Test
public void getInfo() {
	Random rand = new Random();
	byte[] randomBytes = new byte[20];
	rand.nextBytes(randomBytes);
	PESection section = new PESection(randomBytes, 0, new DummyHeader(), new File(""));
	String info = section.toString();
	assertNotNull(info);
	assertTrue(info.length() > 0);
}
 
Example 8
Source File: RandomAdaptorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkConstant(Random random) {
    byte[] bytes = new byte[] {0};
    random.nextBytes(bytes);
    Assert.assertEquals(0, bytes[0]);
    Assert.assertEquals(false, random.nextBoolean());
    Assert.assertEquals(0, random.nextDouble(), 0);
    Assert.assertEquals(0, random.nextFloat(), 0);
    Assert.assertEquals(0, random.nextGaussian(), 0);
    Assert.assertEquals(0, random.nextInt());
    Assert.assertEquals(0, random.nextInt(1));
    Assert.assertEquals(0, random.nextLong());
    random.setSeed(100);
    Assert.assertEquals(0, random.nextDouble(), 0);
}
 
Example 9
Source File: RandomAdaptorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkConstant(Random random) {
    byte[] bytes = new byte[] {0};
    random.nextBytes(bytes);
    assertEquals(0, bytes[0]);  
    assertEquals(false, random.nextBoolean());
    assertEquals(0, random.nextDouble(), 0);
    assertEquals(0, random.nextFloat(), 0);
    assertEquals(0, random.nextGaussian(), 0);
    assertEquals(0, random.nextInt());
    assertEquals(0, random.nextInt(1));
    assertEquals(0, random.nextLong());
    random.setSeed(100);
    assertEquals(0, random.nextDouble(), 0);
}
 
Example 10
Source File: TailAppendingInputStreamTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  Random random = new Random();
  random.setSeed(RANDOM_SEED);
  mBytes = new byte[BYTES_LENGTH];
  mTail = new byte[TAIL_LENGTH];
  mOutputBuffer = new byte[OUTPUT_LENGTH];
  random.nextBytes(mBytes);
  random.nextBytes(mTail);
  InputStream stream = new ByteArrayInputStream(mBytes);
  mTailAppendingInputStream = new TailAppendingInputStream(stream, mTail);
}
 
Example 11
Source File: BufferedContentTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests buffered content.
 * @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);
}
 
Example 12
Source File: AES256Cryptor.java    From Insights with Apache License 2.0 5 votes vote down vote up
/**
 * @return a new pseudorandom salt of the specified length
 */
private static byte[] generateSalt(int length) {
	Random r = new SecureRandom();
	byte[] salt = new byte[length];
	r.nextBytes(salt);
	return salt;
}
 
Example 13
Source File: BitsTest.java    From jenetics with Apache License 2.0 5 votes vote down vote up
@DataProvider(name = "toLargeIntegerData")
public Iterator<Object[]> toLargeIntegerData() {
	final long seed = System.currentTimeMillis();
	final Random random = new Random(seed);
	final int length = 20;

	return new Iterator<>() {
		private int _pos = 0;

		@Override
		public boolean hasNext() {
			return _pos < length;
		}

		@Override
		public Object[] next() {
			final int size = random.nextInt(1000) + 1;
			final byte[] data = new byte[size];
			random.nextBytes(data);
			_pos += 1;

			return new Object[]{data};
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
	};
}
 
Example 14
Source File: TestCheckpoint.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
private void 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);
  stm.close();
}
 
Example 15
Source File: KvClient.java    From kvstore with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an exponentially sized byte string with a mean size.
 */
private static ByteString randomBytes(int mean) {
  Random random = new Random();
  // An exponentially distributed random number.
  int size = (int) Math.round(mean * -Math.log(1 - random.nextDouble()));
  byte[] bytes = new byte[1 + size];
  random.nextBytes(bytes);
  return ByteString.copyFrom(bytes);
}
 
Example 16
Source File: TestRegionSizeUse.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Writes at least {@code sizeInBytes} bytes of data to HBase and returns the TableName used.
 *
 * @param sizeInBytes The amount of data to write in bytes.
 * @return The table the data was written to
 */
private TableName writeData(long sizeInBytes) throws IOException {
  final Connection conn = TEST_UTIL.getConnection();
  final Admin admin = TEST_UTIL.getAdmin();
  final TableName tn = TableName.valueOf(testName.getMethodName());

  // Delete the old table
  if (admin.tableExists(tn)) {
    admin.disableTable(tn);
    admin.deleteTable(tn);
  }

  // Create the table
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(tn);
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(F1)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);

  admin.createTable(tableDescriptorBuilder.build(), Bytes.toBytes("1"),
    Bytes.toBytes("9"), NUM_SPLITS);

  final Table table = conn.getTable(tn);
  try {
    List<Put> updates = new ArrayList<>();
    long bytesToWrite = sizeInBytes;
    long rowKeyId = 0L;
    final StringBuilder sb = new StringBuilder();
    final Random r = new Random();
    while (bytesToWrite > 0L) {
      sb.setLength(0);
      sb.append(Long.toString(rowKeyId));
      // Use the reverse counter as the rowKey to get even spread across all regions
      Put p = new Put(Bytes.toBytes(sb.reverse().toString()));
      byte[] value = new byte[SIZE_PER_VALUE];
      r.nextBytes(value);
      p.addColumn(Bytes.toBytes(F1), Bytes.toBytes("q1"), value);
      updates.add(p);

      // Batch 50K worth of updates
      if (updates.size() > 50) {
        table.put(updates);
        updates.clear();
      }

      // Just count the value size, ignore the size of rowkey + column
      bytesToWrite -= SIZE_PER_VALUE;
      rowKeyId++;
    }

    // Write the final batch
    if (!updates.isEmpty()) {
      table.put(updates);
    }

    return tn;
  } finally {
    table.close();
  }
}
 
Example 17
Source File: TestCopyStream.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Stress test writing small streams of random length (from zero bytes to a
 * full block in length).
 * 
 * @throws IOException 
 */
public void test_copyStream_smallRandomStreams() throws IOException {
    
    final int LIMIT = 20;
    
    final Random r = new Random();

    int nzero = 0;
    int nfull = 0;
    
    for(int i=0; i<LIMIT; i++) {
    
        /*
         * Note: {id + version} are always unique for this test.
         */
        final String id = "test#" + r.nextInt(1000);

        final int version = i;

        /*
         * Note: size in [0:block_size] bytes.
         * 
         * Note: the distribution is adjusted to make near zero and near
         * block_size operations at least 10% of all operations.
         */
        final int len;
        {
            final int x = r.nextInt(100);
            if (x < 10) {
                // short block length.
                len = r.nextInt(5);
            } else if (x >= 90) {
                // long block length (up to block_size).
                len = r.nextInt(5) + BLOCK_SIZE - 4;
            } else {
                // uniform random distribution.
                len = r.nextInt(BLOCK_SIZE + 1);
            }
        }
        
        if(len==0) nzero++;
        if(len==BLOCK_SIZE) nfull++;
        
        final byte[] expected = new byte[len];
        
        // random data.
        r.nextBytes(expected);

        assertEquals("nbytes", expected.length, repo.copyStream(id,
                version, new ByteArrayInputStream(expected)));

        assertEquals("blockCount", 1, repo.getBlockCount(id, version));

        assertSameIterator("block identifiers", new Long[] { 0L }, repo
                .blocks(id, version));

        assertEquals("data", expected, read(repo.inputStream(id, version)));

    }
    
    log.warn("There were " + nzero + " zero length blocks and " + nfull
            + " full length blocks out of " + LIMIT + " trials");
            
}
 
Example 18
Source File: TestPerColumnFamilyFlush.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  int numRegions = Integer.parseInt(args[0]);
  long numRows = Long.parseLong(args[1]);

  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLENAME);
  tableDescriptor.setMaxFileSize(10L * 1024 * 1024 * 1024);
  tableDescriptor.setValue(HTableDescriptor.SPLIT_POLICY,
    ConstantSizeRegionSplitPolicy.class.getName());
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY1));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY2));
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY3));

  Configuration conf = HBaseConfiguration.create();
  Connection conn = ConnectionFactory.createConnection(conf);
  Admin admin = conn.getAdmin();
  if (admin.tableExists(TABLENAME)) {
    admin.disableTable(TABLENAME);
    admin.deleteTable(TABLENAME);
  }
  if (numRegions >= 3) {
    byte[] startKey = new byte[16];
    byte[] endKey = new byte[16];
    Arrays.fill(endKey, (byte) 0xFF);
    admin.createTable(tableDescriptor, startKey, endKey, numRegions);
  } else {
    admin.createTable(tableDescriptor);
  }
  admin.close();

  Table table = conn.getTable(TABLENAME);
  byte[] qf = Bytes.toBytes("qf");
  Random rand = new Random();
  byte[] value1 = new byte[16];
  byte[] value2 = new byte[256];
  byte[] value3 = new byte[4096];
  for (long i = 0; i < numRows; i++) {
    Put put = new Put(Hashing.md5().hashLong(i).asBytes());
    rand.setSeed(i);
    rand.nextBytes(value1);
    rand.nextBytes(value2);
    rand.nextBytes(value3);
    put.addColumn(FAMILY1, qf, value1);
    put.addColumn(FAMILY2, qf, value2);
    put.addColumn(FAMILY3, qf, value3);
    table.put(put);
    if (i % 10000 == 0) {
      LOG.info(i + " rows put");
    }
  }
  table.close();
  conn.close();
}
 
Example 19
Source File: TestOrcFile.java    From hive-dwrf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetaData() throws Exception {
  ObjectInspector inspector;
  synchronized (TestOrcFile.class) {
    inspector = ObjectInspectorFactory.getReflectionObjectInspector
        (BigRow.class, ObjectInspectorFactory.ObjectInspectorOptions.JAVA);
  }
  ReaderWriterProfiler.setProfilerOptions(conf);
  Writer writer = OrcFile.createWriter(fs, testFilePath, conf, inspector,
      1000, CompressionKind.NONE, 100, 10000);
  writer.addUserMetadata("my.meta", byteBuf(1, 2, 3, 4, 5, 6, 7, -1, -2, 127, -128));
  writer.addUserMetadata("clobber", byteBuf(1,2,3));
  writer.addUserMetadata("clobber", byteBuf(4,3,2,1));
  ByteBuffer bigBuf = ByteBuffer.allocate(40000);
  Random random = new Random(0);
  random.nextBytes(bigBuf.array());
  writer.addUserMetadata("big", bigBuf);
  bigBuf.position(0);
  writer.addRow(new BigRow(true, (byte) 127, (short) 1024, 42,
      42L * 1024 * 1024 * 1024, (float) 3.1415, -2.713, null,
      null, null, null, null));
  writer.addUserMetadata("clobber", byteBuf(5,7,11,13,17,19));
  writer.close();
  ReaderWriterProfiler.setProfilerOptions(conf);
  Reader reader = OrcFile.createReader(fs, testFilePath, conf);
  assertEquals(byteBuf(5,7,11,13,17,19), reader.getMetadataValue("clobber"));
  assertEquals(byteBuf(1,2,3,4,5,6,7,-1,-2,127,-128),
      reader.getMetadataValue("my.meta"));
  assertEquals(bigBuf, reader.getMetadataValue("big"));
  try {
    reader.getMetadataValue("unknown");
    assertTrue(false);
  } catch (IllegalArgumentException iae) {
    // PASS
  }
  int i = 0;
  for(String key: reader.getMetadataKeys()) {
    if ("my.meta".equals(key) ||
        "clobber".equals(key) ||
        "big".equals(key)) {
      i += 1;
    } else {
      throw new IllegalArgumentException("unknown key " + key);
    }
  }
  assertEquals(3, i);
}
 
Example 20
Source File: GuidTest.java    From incubator-gobblin with Apache License 2.0 3 votes vote down vote up
@Test
public void testFromHasGuid() throws IOException {
  Random random = new Random();

  byte[] b = new byte[10];
  random.nextBytes(b);

  Guid guid = new Guid(b);

  HasGuid hasGuid = new Guid.SimpleHasGuid(guid);

  Assert.assertEquals(hasGuid.guid(), guid);


}