Package freenet.support.math

Examples of freenet.support.math.MersenneTwister


    checkBlock(buf, true);
  }
 
  public void testEncodeDecodeFullBlock() throws CHKEncodeException, CHKVerifyException, CHKDecodeException, UnsupportedEncodingException, InvalidCompressionCodecException, IOException {
    byte[] fullBlock = new byte[CHKBlock.DATA_LENGTH];
    MersenneTwister random = new MersenneTwister(42);
    for(int i=0;i<10;i++) {
      random.nextBytes(fullBlock);
      checkBlock(fullBlock, false);
      checkBlock(fullBlock, true);
    }
  }
View Full Code Here


      checkBlock(s.getBytes("UTF-8"), true);
    }
  }
 
  public void testEncodeDecodeRandomLength() throws CHKEncodeException, CHKVerifyException, CHKDecodeException, UnsupportedEncodingException, InvalidCompressionCodecException, IOException
    MersenneTwister random = new MersenneTwister(42);
    for(int i=0;i<10;i++) {
      byte[] buf = new byte[random.nextInt(CHKBlock.DATA_LENGTH+1)];
      random.nextBytes(buf);
      checkBlock(buf, false);
      checkBlock(buf, true);
    }
  }
View Full Code Here

      checkBlock(buf, true);
    }
  }
 
  public void testEncodeDecodeNearlyFullBlock() throws CHKEncodeException, CHKVerifyException, CHKDecodeException, UnsupportedEncodingException, InvalidCompressionCodecException, IOException
    MersenneTwister random = new MersenneTwister(68);
    for(int i=0;i<10;i++) {
      byte[] buf = new byte[CHKBlock.DATA_LENGTH - i];
      random.nextBytes(buf);
      checkBlock(buf, false);
      checkBlock(buf, true);
    }
    for(int i=0;i<10;i++) {
      byte[] buf = new byte[CHKBlock.DATA_LENGTH - (1<<i)];
      random.nextBytes(buf);
      checkBlock(buf, false);
      checkBlock(buf, true);
    }
  }
View Full Code Here

    // API NOTE: java.util.Random generates bytes by generating ints, so the stream is
    // reproducible provided that the chunks are always exact multiples of 4 bytes.
    assertTrue(writeStep % 4 == 0);
    assertTrue(readStep % 4 == 0);
    EncryptingIoAdapter a = createAdapter();
    MersenneTwister dataMT = new MersenneTwister(seed);
    for(int written=0;written<size;) {
      int write = Math.min(writeStep, size-written);
      byte[] data = new byte[write];
      dataMT.nextBytes(data);
      a.write(data);
      written += write;
    }
    dataMT = new MersenneTwister(seed);
    a.seek(0);
    byte[] readBuffer = new byte[readStep];
    for(int read=0;read<size;) {
      int toRead = Math.min(readStep, size-read);
      int readBytes = a.read(readBuffer, toRead);
      assertTrue(readBytes > 0);
      byte[] compareBuf = new byte[readBytes];
      dataMT.nextBytes(compareBuf);
      for(int i=0;i<compareBuf.length;i++)
        assertTrue("Byte "+(i+read)+" is wrong for buffered test size="+size+" write="+writeStep+" read="+readStep+" seed="+seed, readBuffer[i] == compareBuf[i]);
      read += readBytes;
    }
    a.close();
View Full Code Here

    a.close();
  }

  public void checkLinearBuffered(int size, int writeStep, int readStep, long seed) {
    EncryptingIoAdapter a = createAdapter();
    MersenneTwister dataMT = new MersenneTwister(seed);
    byte[] bigBuffer = new byte[size];
    dataMT.nextBytes(bigBuffer);
    for(int written=0;written<size;) {
      int write = Math.min(writeStep, size-written);
      byte[] data = new byte[write];
      System.arraycopy(bigBuffer,written,data,0,write);
      a.write(data);
View Full Code Here

  }

  private void realRun() {
    Random random;
    if(KILL_BLOCKS != 0)
      random = new MersenneTwister();
    else
      random = null;
    Key[] keys = null;
    SendableGet getter = null;
    ClientRequestScheduler sched = null;
View Full Code Here

        File dir = new File("bootstrap-pull-test");
        FileUtil.removeAll(dir);
        RandomSource random = NodeStarter.globalTestInit(dir.getPath(), false, LogLevel.ERROR, "", false);
        byte[] seed = new byte[64];
        random.nextBytes(seed);
        MersenneTwister fastRandom = new MersenneTwister(seed);
        File seednodes = new File("seednodes.fref");
        if(!seednodes.exists() || seednodes.length() == 0 || !seednodes.canRead()) {
          System.err.println("Unable to read seednodes.fref, it doesn't exist, or is empty");
          System.exit(EXIT_NO_SEEDNODES);
        }
        File secondInnerDir = new File(dir, Integer.toString(DARKNET_PORT));
        secondInnerDir.mkdir();
        FileInputStream fis = new FileInputStream(seednodes);
        FileUtil.writeTo(fis, new File(secondInnerDir, "seednodes.fref"));
        fis.close();

        // Create the test data
        System.out.println("Creating test data.");
        File dataFile = File.createTempFile("testdata", ".tmp", dir);
        OutputStream os = new FileOutputStream(dataFile);
        byte[] buf = new byte[4096];
        for(long written = 0; written < TEST_SIZE;) {
          fastRandom.nextBytes(buf);
          int toWrite = (int) Math.min(TEST_SIZE - written, buf.length);
          os.write(buf, 0, toWrite);
          written += toWrite;
        }
        os.close();
View Full Code Here

   * @return the paded bucket
   */
  public static Bucket pad(Bucket oldBucket, int blockLength, BucketFactory bf, int length) throws IOException {
    byte[] hash = BucketTools.hash(oldBucket);
    Bucket b = bf.makeBucket(blockLength);
    MersenneTwister mt = new MersenneTwister(hash);
    OutputStream os = b.getOutputStreamUnbuffered();
    try {
      BucketTools.copyTo(oldBucket, os, length);
      byte[] buf = new byte[BUFFER_SIZE];
      for(int x=length;x<blockLength;) {
        int remaining = blockLength - x;
        int thisCycle = Math.min(remaining, buf.length);
        mt.nextBytes(buf); // FIXME??
        os.write(buf, 0, thisCycle);
        x += thisCycle;
      }
      os.close();
      os = null;
View Full Code Here

            long currentLength = raf.length();
            if(forceLength >= 0 && forceLength != currentLength) {
                if(readOnly) throw new IOException("Read only but wrong length");
                // Preallocate space. We want predictable disk usage, not minimal disk usage, especially for downloads.
                raf.seek(0);
                MersenneTwister mt = null;
                if(seedRandom != null)
                    mt = new MersenneTwister(seedRandom.nextLong());
                byte[] buf = new byte[4096];
                for(long l = 0; l < forceLength; l+=4096) {
                    if(mt != null)
                        mt.nextBytes(buf);
                    int maxWrite = (int)Math.min(4096, forceLength - l);
                    raf.write(buf, 0, maxWrite);
                }
                assert(raf.getFilePointer() == forceLength);
                assert(raf.length() == forceLength);
View Full Code Here

      try {
        keywordHash = md256.digest(keyword.getBytes("UTF-8"));
      } catch (UnsupportedEncodingException e) {
        throw new Error("Impossible: JVM doesn't support UTF-8: " + e, e);
      }
      MersenneTwister mt = new MersenneTwister(keywordHash);
      DSAPrivateKey privKey = new DSAPrivateKey(Global.DSAgroupBigA, mt);
      DSAPublicKey pubKey = new DSAPublicKey(Global.DSAgroupBigA, privKey);
      byte[] pubKeyHash = md256.digest(pubKey.asBytes());
      try {
        return new ClientKSK(keyword, pubKeyHash, pubKey, privKey, keywordHash);
View Full Code Here

TOP

Related Classes of freenet.support.math.MersenneTwister

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.