java.util.Random.nextBytes()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中java.util.Random.nextBytes()方法的一些代码示例,展示了Random.nextBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Random.nextBytes()方法的具体详情如下:
包路径:java.util.Random
类名称:Random
方法名:nextBytes

Random.nextBytes介绍

[英]Fills buf with random bytes.
[中]用随机字节填充buf。

代码示例

代码示例来源:origin: commons-io/commons-io

public static byte[] generateRandomByteStream(final int pSize) {
  final byte[] buffer = new byte[pSize];
  final Random rnd = new Random();
  rnd.nextBytes(buffer);
  return buffer;
}

代码示例来源:origin: google/guava

static void assertHashByteBufferMatchesBytes(HashFunction hashFunction) {
 Random rng = new Random(0L);
 byte[] bytes = new byte[rng.nextInt(256) + 1];
 rng.nextBytes(bytes);
 assertEquals(hashFunction.hashBytes(bytes), hashFunction.hashBytes(ByteBuffer.wrap(bytes)));
}

代码示例来源:origin: google/guava

static void assertHashByteBufferExhaustsBuffer(HashFunction hashFunction) {
 Random rng = new Random(0L);
 byte[] bytes = new byte[rng.nextInt(256) + 1];
 rng.nextBytes(bytes);
 ByteBuffer buffer = ByteBuffer.wrap(bytes);
 HashCode unused = hashFunction.hashBytes(buffer);
 assertFalse(buffer.hasRemaining());
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testInsertLobAsLastValue() {
  doInHibernate( this::sessionFactory, session -> {
    byte[] details = new byte[4000];
    byte[] title = new byte[2000];
    Random random = new Random();
    random.nextBytes( details );
    random.nextBytes( title );
    // This insert will fail on Oracle without the fix to ModelBinder flagging SimpleValue and Property as Lob
    // because the fields will not be placed at the end of the insert, resulting in an Oracle failure.
    final LobAsLastValueEntity entity = new LobAsLastValueEntity( "Test", new String( details ), new String( title ) );
    session.save( entity );
  } );
}

代码示例来源:origin: apache/kylin

@Test
public void testPeekLength() throws IOException {
  HLLCounterOld hllc = new HLLCounterOld(10);
  HLLCounterOld copy = new HLLCounterOld(10);
  byte[] value = new byte[10];
  for (int i = 0; i < 200000; i++) {
    rand1.nextBytes(value);
    hllc.add(value);
    buf.clear();
    hllc.writeRegisters(buf);
    int len = buf.position();
    buf.position(0);
    assertEquals(len, hllc.peekLength(buf));
    copy.readRegisters(buf);
    assertEquals(len, buf.position());
    assertEquals(hllc, copy);
  }
  buf.clear();
}

代码示例来源:origin: google/guava

private static void assertHashBytesEquivalence(HashFunction hashFunction, Random random) {
 int size = random.nextInt(2048);
 byte[] bytes = new byte[size];
 random.nextBytes(bytes);
 assertEquals(
   hashFunction.hashBytes(bytes), hashFunction.newHasher(size).putBytes(bytes).hash());
 int off = random.nextInt(size);
 int len = random.nextInt(size - off);
 assertEquals(
   hashFunction.hashBytes(bytes, off, len),
   hashFunction.newHasher(size).putBytes(bytes, off, len).hash());
}

代码示例来源:origin: LMAX-Exchange/disruptor

@Test(timeout = 1000)
public void shouldShutdownGracefulEvenWithFatalExceptionHandler()
{
  disruptor.start();
  byte[] bytes;
  for (int i = 1; i < 10; i++)
  {
    bytes = new byte[32];
    random.nextBytes(bytes);
    disruptor.publishEvent(new ByteArrayTranslator(bytes));
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testEncodeDecodeSmall() {
  for (int i = 0; i < 12; i++) {
    final byte[] data = new byte[i];
    this.getRandom().nextBytes(data);
    final byte[] enc = Base64.encodeBase64(data);
    assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc));
    final byte[] data2 = Base64.decodeBase64(enc);
    assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2));
  }
}

代码示例来源:origin: google/guava

@Override
 void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
  byte[] value = new byte[random.nextInt(128)];
  random.nextBytes(value);
  for (PrimitiveSink sink : sinks) {
   sink.putBytes(value);
  }
 }
},

代码示例来源:origin: apache/hbase

@Override
 public void run() {
  byte[] value = new byte[100];
  Put[]  in = new Put[1];
  // iterate for the specified number of operations
  for (int i=0; i<numOps; i++) {
   // generate random bytes
   rand.nextBytes(value);
   // put the randombytes and verify that we can read it. This is one
   // way of ensuring that rwcc manipulation in HRegion.put() is fine.
   Put put = new Put(rowkey);
   put.addColumn(fam1, qual1, value);
   in[0] = put;
   try {
    OperationStatus[] ret = region.batchMutate(in);
    assertEquals(1, ret.length);
    assertEquals(OperationStatusCode.SUCCESS, ret[0].getOperationStatusCode());
    assertGet(this.region, rowkey, fam1, qual1, value);
   } catch (IOException e) {
    assertTrue("Thread id " + threadNumber + " operation " + i + " failed.",
          false);
   }
  }
 }
}

代码示例来源:origin: org.apache.spark/spark-core_2.10

private byte[] getRandomByteArray(int numWords) {
 Assert.assertTrue(numWords >= 0);
 final int lengthInBytes = numWords * 8;
 final byte[] bytes = new byte[lengthInBytes];
 rand.nextBytes(bytes);
 return bytes;
}

代码示例来源:origin: google/guava

static void assertHashByteBufferPreservesByteOrder(HashFunction hashFunction) {
 Random rng = new Random(0L);
 byte[] bytes = new byte[rng.nextInt(256) + 1];
 rng.nextBytes(bytes);
 ByteBuffer littleEndian = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
 ByteBuffer bigEndian = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
 assertEquals(hashFunction.hashBytes(littleEndian), hashFunction.hashBytes(littleEndian));
 assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.order());
 assertEquals(ByteOrder.BIG_ENDIAN, littleEndian.order());
}

代码示例来源:origin: gocd/gocd

@Test
  public void shouldComputeMD5ForAGiveString() throws IOException {
    byte[] testData = new byte[1024 * 1024];
    new Random().nextBytes(testData);
    assertThat(DigestUtils.md5Hex(testData),
        is(CachedDigestUtils.md5Hex(new ByteArrayInputStream(testData))));

  }
}

代码示例来源:origin: apache/kylin

@Test
public void testPeekLength() throws IOException {
  HLLCounter hllc = new HLLCounter(10);
  HLLCounter copy = new HLLCounter(10);
  byte[] value = new byte[10];
  for (int i = 0; i < 200000; i++) {
    rand1.nextBytes(value);
    hllc.add(value);
    buf.clear();
    hllc.writeRegisters(buf);
    int len = buf.position();
    buf.position(0);
    assertEquals(len, hllc.peekLength(buf));
    copy.readRegisters(buf);
    assertEquals(len, buf.position());
    assertEquals(hllc, copy);
  }
  buf.clear();
}

代码示例来源:origin: apache/hbase

private void createFiles(FileSystem fs, Path parentDir, int numOfFiles) throws IOException {
 Random random = new Random();
 for (int i = 0; i < numOfFiles; i++) {
  int xMega = 1 + random.nextInt(3); // size of each file is between 1~3M
  try (FSDataOutputStream fsdos = fs.create(new Path(parentDir, "file-" + i))) {
   for (int m = 0; m < xMega; m++) {
    byte[] M = new byte[1024 * 1024];
    random.nextBytes(M);
    fsdos.write(M);
   }
  }
 }
}

代码示例来源:origin: google/guava

private static void assertHashByteBufferEquivalence(HashFunction hashFunction, Random random) {
 int size = random.nextInt(2048);
 byte[] bytes = new byte[size];
 random.nextBytes(bytes);
 assertEquals(
   hashFunction.hashBytes(ByteBuffer.wrap(bytes)),
   hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes)).hash());
 int off = random.nextInt(size);
 int len = random.nextInt(size - off);
 assertEquals(
   hashFunction.hashBytes(ByteBuffer.wrap(bytes, off, len)),
   hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes, off, len)).hash());
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void authentication_with_sha1_with_correct_password_should_work() {
 String password = randomAlphanumeric(60);
 byte[] saltRandom = new byte[20];
 RANDOM.nextBytes(saltRandom);
 String salt = DigestUtils.sha1Hex(saltRandom);
 UserDto user = newUserDto()
  .setHashMethod(SHA1.name())
  .setCryptedPassword(DigestUtils.sha1Hex("--" + salt + "--" + password + "--"))
  .setSalt(salt);
 underTest.authenticate(db.getSession(), user, password, AuthenticationEvent.Method.BASIC);
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  new Random().nextBytes(bytes);
  while (string.length() < StreamUtils.BUFFER_SIZE + 10) {
    string += UUID.randomUUID().toString();
  }
}

代码示例来源:origin: google/guava

@Override
 void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
  byte[] value = new byte[random.nextInt(128)];
  random.nextBytes(value);
  int off = random.nextInt(value.length + 1);
  int len = random.nextInt(value.length - off + 1);
  for (PrimitiveSink sink : sinks) {
   sink.putBytes(value, off, len);
  }
 }
},

代码示例来源:origin: org.apache.spark/spark-core

private byte[] getRandomByteArray(int numWords) {
 Assert.assertTrue(numWords >= 0);
 final int lengthInBytes = numWords * 8;
 final byte[] bytes = new byte[lengthInBytes];
 rand.nextBytes(bytes);
 return bytes;
}

相关文章