示例#1
0
  public static void main(String[] args) {
    /*System.out.println(modular_exponent_32(2,36,37));
    System.out.println(modular_exponent_32(2,18,37));
    System.out.println(modular_exponent_32(2,9,37));
    int d = Integer.numberOfTrailingZeros(64);
    System.out.println(d);
    */
    int counter = how_many_primes(1000000);
    System.out.println(counter);

    try {
      RandomAccessFile out = new RandomAccessFile("mrprimes.txt", "rw");
      out.writeBytes("2\n");
      for (int i = 3; i < 1000000; i += 2) {
        if (miller_rabin_32(i)) {
          out.writeBytes(Integer.toString(i) + "\n");
        }
      }
      // for (int i=0; i<counter; i++){
      // out.seek(i*4);
      // System.out.println(","+out.readInt());
      // }
      out.close();
    } catch (IOException e) {
    }
    System.out.println(miller_rabin_32(1105));
  }
示例#2
0
 /**
  * Record a specific string
  *
  * @param s
  */
 public static void record(String s) {
   try {
     if (!recording) return;
     Date now = new Date();
     String ts = stamp.format(now);
     R.writeBytes(ts);
     R.writeBytes(" ");
     R.writeBytes(s);
     R.writeBytes("\n");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
示例#3
0
  /**
   * Add the given text string to the end of a given file.
   *
   * @param inStr The string to be added.
   * @param fileStr the name of the file to be added.
   */
  public static void addStringToFile(String inStr, String fileStr) throws Exception {

    RandomAccessFile raFile = new RandomAccessFile(fileStr, "rw");
    raFile.seek(raFile.length());
    raFile.writeBytes(inStr);
    raFile.close();
  }
  /**
   * Create an index file for the specified column.
   *
   * @param colname the column's name
   * @return the time it took to run this method.
   * @throws Exception
   */
  public long createIndexFile() throws Exception {
    long s = System.currentTimeMillis();
    // fill idx file with hashsize empty records/lines (empty means filled with spaces)

    int pkindex = def.getColPosition(def.getPK());
    if (pkindex == -1) {
      throw new Exception("Primary key does not exist");
    }
    Integer[] size = def.getSizes();
    int recordSize = idxFixedRecordLength() + size[pkindex];

    StringBuffer temp = new StringBuffer();
    for (int i = 0; i < recordSize - 2; i++) {
      temp.append(" ");
    }

    indexFile.seek(0);
    for (int i = 0; i < this.hashsize; i++) {
      indexFile.writeBytes(temp + "\r\n");
    }

    int table_pos = 0;
    // create an index entry for each row present in the tbl-file (for column=colname)
    String line = null;
    tableFile.seek(0);
    while ((line = tableFile.readLine()) != null) {
      // get column value (don't strip the spaces)
      String pkvalue = line.split("#")[pkindex];
      addIndexEntry(table_pos, pkvalue);
      table_pos++;
    }
    long e = System.currentTimeMillis();
    return e - s;
  }
示例#5
0
文件: FLRAF.java 项目: Fernum/csc445
 public void write(String s) {
   try {
     seek(index * BLOCKSIZE);
     super.writeBytes(s);
   } catch (IOException ie) {
   }
   index++;
 }
示例#6
0
  void doExeCommand() {
    JViewport viewport = scrollPane.getViewport();
    String strContent = new String();
    PSlider slider;
    int i;
    File fp = new File(dataFile);

    RandomAccessFile access = null;
    Runtime program = Runtime.getRuntime();
    String cmdTrigger = "trigger";

    boolean delete = fp.delete();

    try {
      fp.createNewFile();
    } catch (IOException ie) {
      System.err.println("Couldn't create the new file " + ie);
      // System.exit(-1);;
    }

    try {
      access = new RandomAccessFile(fp, "rw");
    } catch (IOException ie) {
      System.err.println("Error in accessing the file " + ie);
      // System.exit(-1);;
    }

    for (i = 0; i < COMPONENTS; i++) {
      slider = (PSlider) vSlider.elementAt(i);
      /* Modified on March 20th to satisfy advisors' new request */
      if (slider.isString == true) strContent = strContent + slider.getRealStringValue() + " ";
      else strContent = strContent + slider.getValue() + " ";
    }

    // Get value of the radio button group
    if (firstBox.isSelected() == true) strContent = strContent + "1";
    else strContent = strContent + "0";

    try {
      access.writeBytes(strContent);
      access.close();
    } catch (IOException ie) {
      System.err.println("Error in writing to file " + ie);
      // System.exit(-1);;
    }
    // Trigger the OpenGL program to update with new values
    try {
      Process pid = program.exec(cmdTrigger);
    } catch (IOException ie) {
      System.err.println("Couldn't run " + ie);
      // System.exit(-1);;
    }
    doSourceFileUpdate();
  }
  /**
   * Create an index entry in the idx file of the specified column.
   *
   * @param tablePosition position or line nr in table *.tbl
   * @param pkValue the value of the primary key to create an index entry for
   * @throws Exception
   */
  protected void addIndexEntry(int tablePosition, String pkValue) throws Exception {
    if (!indexExists(def.getPK())) {
      throw new Exception("No index created");
    }
    int pkindex = def.getColPosition(def.getPK());
    if (pkindex == -1) {
      throw new Exception("Primary key does not exist");
    }
    Integer[] size = def.getSizes();
    if (pkValue.length() > TableDefinition.INTEGER_SIZE) {
      throw new Exception("Supplied pkValue too large");
    } else {
      // make sure key value has appropriate length
      StringBuffer temp = new StringBuffer();
      temp.append(pkValue);
      for (int i = 0; i < TableDefinition.INTEGER_SIZE - pkValue.length(); i++) {
        temp.append(' ');
      }
      pkValue = temp.toString();
    }

    // calculate index = hash value
    String s_value = pkValue.trim();
    int indexPosition = hash(s_value);
    int recordSize = idxFixedRecordLength() + size[pkindex];

    indexFile.seek(indexPosition * recordSize);
    String line = indexFile.readLine();

    if (line.substring(0, 1).equals(" ")) {
      // empty record, reset file pointer and fill record
      indexFile.seek(indexPosition * recordSize);

      String indexPositionFormatted = String.format("%-5s", Integer.toString(indexPosition));
      String tablePositionFormatted = String.format("%-5s", Integer.toString(tablePosition));

      String indexRecord =
          indexPositionFormatted
              + "# #"
              + pkValue
              + "#"
              + tablePositionFormatted
              + "#"
              + "null "
              + "\r\n";

      indexFile.writeBytes(indexRecord);
    } else {
      String[] parts = line.split("#");
      if (parts[1].equals("D")) {
        // Deleted record, reset file pointer, fill record but keep previous link !
        indexFile.seek(indexPosition * recordSize);

        String indexPositionFormatted = String.format("%-5s", Integer.toString(indexPosition));
        String tablePositionFormatted = String.format("%-5s", Integer.toString(tablePosition));

        String indexRecord =
            indexPositionFormatted + "# #" + pkValue + "#" + tablePositionFormatted;

        indexFile.writeBytes(indexRecord);
      } else {
        // Collision found ! a valid record is found, so add new record at EOF
        // Calculate new record number
        int newIndexPosition = (int) (indexFile.length() / recordSize);

        String newIndexPositionFormatted =
            String.format("%-5s", Integer.toString(newIndexPosition));
        String tablePositionFormatted = String.format("%-5s", Integer.toString(tablePosition));

        // reset file pointer and update the current record
        indexFile.seek((indexPosition * recordSize) + (recordSize - 2 - 5));
        indexFile.write(newIndexPositionFormatted.toString().getBytes());

        // move file pointer to EOF and append new record
        indexFile.seek(indexFile.length());
        String indexRecord =
            newIndexPositionFormatted
                + "# #"
                + pkValue
                + "#"
                + tablePositionFormatted
                + "#"
                + "null "
                + "\r\n";

        indexFile.writeBytes(indexRecord);
      }
    }
  }
示例#8
0
  void doSourceFileUpdate() {
    BufferedReader bufferIn = null;
    String str1 = new String();
    String strContent = new String();
    String sliderValue = new String();
    String newLine = System.getProperty("line.separator");
    StringBuffer strBuf = new StringBuffer();
    RandomAccessFile access = null;
    String tempFile = "temp" + round + ".html";
    File fp = new File(tempFile);
    boolean firstChoice = true;
    int posFound = 0;
    int i = 0;
    PSlider slider;

    // round += 1;
    // Read the original source file to input buffer
    try {
      bufferIn = new BufferedReader(new FileReader(exampleSource));
    } catch (FileNotFoundException fe) {
      System.err.println("Example Source File not found " + fe);
      // System.exit(-1);;
    }
    // get the first line of the buffer.
    try {
      str1 = bufferIn.readLine();
    } catch (IOException ie) {
      System.err.println("Error reading line from the buffer " + ie);
      // System.exit(-1);;
    }
    // Transfer the whole content of the input buffer to the string
    try {
      do strContent += str1 + newLine;
      while ((str1 = bufferIn.readLine()) != null);
    } catch (IOException ie) {
      System.err.println("Error readding content of the input buffer " + ie);
      // System.exit(-1);;
    }
    // do the replacement.
    // First having to update the code part that is active in this section before
    // doing variables updated. Look at the current active radiobutton to make decision
    if (secondBox.isSelected() == true) firstChoice = false;
    if (firstChoice == true) {
      String tempStr = new String();
      strBuf = new StringBuffer(strContent);
      posFound = strContent.indexOf(textStr2);
      tempStr =
          "<font color=blue>/***********" + newLine + textStr2 + newLine + "***********/</font>";
      strBuf.replace(posFound, posFound + textStr2.length(), tempStr);
      strContent = new String(strBuf);
    } else {
      String tempStr = new String();
      strBuf = new StringBuffer(strContent);
      posFound = strContent.indexOf(textStr1);
      tempStr =
          "<font color=blue>/***********" + newLine + textStr1 + newLine + "***********/</font>";
      strBuf.replace(posFound, posFound + textStr1.length(), tempStr);
      strContent = new String(strBuf);
    }
    for (i = COMPONENTS; i > 0; i--) {
      // get the current value of slider
      slider = (PSlider) vSlider.elementAt(i - 1);
      sliderValue = slider.getValue();
      // construct the search string
      str1 = "JPOT$" + i;
      // get the position of the search string in the content string.
      while ((posFound = strContent.indexOf(str1)) != -1) {
        strBuf = new StringBuffer(strContent);
        strBuf.replace(posFound, posFound + str1.length(), sliderValue);
        strContent = new String(strBuf);
      }
    }

    boolean delete = fp.delete();

    try {
      fp.createNewFile();
    } catch (IOException ie) {
      System.err.println("Couldn't create the new file " + ie);
      // System.exit(-1);;
    }

    try {
      access = new RandomAccessFile(fp, "rw");
    } catch (IOException ie) {
      System.err.println("Error in accessing the file " + ie);
      // System.exit(-1);;
    }

    try {
      access.writeBytes(strContent);
      access.close();
    } catch (IOException ie) {
      System.err.println("Error in writing to file " + ie);
      // System.exit(-1);;
    }
    try {
      textPane.getDocument().putProperty(Document.StreamDescriptionProperty, null);
      URL url = new URL("file:" + tempFile + "#gohere");
      textPane.setPage(url);
      delete = fp.delete();
    } catch (IOException ie) {
      System.err.println("Can not get the example html file " + ie);
      // System.exit(-1);;
    }
  }