⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_java.html

📁 JNI(java本地接口)之delphi版
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>
<BODY BGCOLOR="#e0e0e0">
<pre class="sourcecode"><code><font color="#003399"><i>/****************************************************************************
 *  
 *  Main.java
 *
 *  This code is used as a driver program to demonstrate the native methods
 *  declared in Native.java.
 *
 *  The countPrimes algorithm was taken from Microsoft's documentation of
 *  their Raw Native Interface (RNI).
 *
 *
*/</i></font>
<b>public</b> <b>class</b> Main {
  <b>static</b> <b>int</b> a, b;
  <b>static</b> <b>final</b> <b>int</b> size = 1000000;
  <b>static</b> <b>final</b> <b>int</b> array_size = 10000;
  <b>static</b> <b>final</b> <b>int</b> func_calls = 1000000;
  <b>static</b> <b>final</b> <b>int</b> rectangles = 500;
  <b>static</b> <b>byte</b>[] c = <b>new</b> <b>byte</b>[size];
  <b>static</b> <b>byte</b>[] d = <b>new</b> <b>byte</b>[array_size];
  <b>public</b> <b>static</b> <b>void</b> main(String[] args)
  {
    String str;
    <b>long</b> start, stop, elapsed;
    <b>int</b> t;
    <b>int</b> x;
    <b>byte</b> value = 123;

      <font color="#003399"><i>// static member</i></font>
    <b>Native</b>.z = 47;

      <font color="#003399"><i>// Create an object from Native.java</i></font>
    <b>Native</b> nat = <b>new</b> <b>Native</b>();

      <font color="#003399"><i>// Print version</i></font>
    nat.printVersion();

      <font color="#003399"><i>// Test all data types and show that Delphi floats won't work correctly with JNI methods</i></font>
      <font color="#003399"><i>// that can accept a variable number of arguments.</i></font>
    System.out.println(<font color="#9933CC">&quot;This should print out the following (but the float field will be incorrect):&quot;</font>);
    System.out.print(<font color="#9933CC">&quot;boolean = true, byte = 11, char = J, int = 123456, &quot;</font>);
    System.out.println(<font color="#9933CC">&quot;long = 987654321, float = 47.56, double = 898.87678 &quot;</font>);
    nat.testAllTypesD(<b>true</b>, (<b>byte</b>)11, <font color="#9933CC">'J'</font>, 123456, 987654321L, 47.56F, 898.87678D);

    System.out.println(<font color="#9933CC">&quot;\n******* Test A: Native.multiplyIntegers(3, 7)&quot;</font>);
    System.out.println(<font color="#9933CC">&quot;Multiply: 3 x 7 = &quot;</font> + nat.multiplyIntegers(3, 7));

      <font color="#003399"><i>// Simple test</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 1: Native.displayHelloWorld&quot;</font>);
    nat.displayHelloWorld();

      <font color="#003399"><i>// Prints out all members of the Java Object by calling</i></font>
      <font color="#003399"><i>// back into the nat object's 'toString' method</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 2: Native.toStringWithPrint&quot;</font>);
    nat.toStringWithPrint();

      <font color="#003399"><i>// Prints a Java string passed in and also returns a Java string</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 3: Native.printLine&quot;</font>);
    str = <font color="#9933CC">&quot;This is from Java&quot;</font>;
    str = nat.printLine(str);
    System.out.println(<font color="#9933CC">&quot;In Java code, string is: &quot;</font> + str);

      <font color="#003399"><i>// Prints out the w, x, y, and z members of the nat object</i></font>
      <font color="#003399"><i>// all from native code</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 4: Native.printWXYZ&quot;</font>);
    nat.printWXYZ();

      <font color="#003399"><i>// An array of Rectangles is created and passed to the native code</i></font>
      <font color="#003399"><i>// to be printed</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 5: Native.printObjectArray(Rectangle[3])&quot;</font>);
    java.awt.Rectangle[] rects = <b>new</b> java.awt.Rectangle[3];
    <b>for</b> (<b>int</b> i = 0; i &lt; rects.length; i++)
      rects[i] = <b>new</b> java.awt.Rectangle(0, 0, 4 * i, 3 * i);
    nat.printObjectArray(rects, <b>true</b>);

      <font color="#003399"><i>// An array of Native objects are passed to Delphi for printing</i></font>
      <font color="#003399"><i>// Demonstrates how the native method works on any 'thing' derived</i></font>
      <font color="#003399"><i>// from a Java Object.</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 6: Native.printObjectArray(Native[2])&quot;</font>);
    <b>Native</b>[] nats = <b>new</b> <b>Native</b>[2];
    <b>for</b> (<b>int</b> i = 0; i &lt; nats.length; i++)
      nats[i] = <b>new</b> <b>Native</b>();
    nat.printObjectArray(nats, <b>true</b>);

      <font color="#003399"><i>// The Delphi code allocates and initializes an array of Rectangles</i></font>
      <font color="#003399"><i>// that get passed back to Java and printed here.</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 7: Native.returnRectArray(3)&quot;</font>);
    java.awt.Rectangle[] rects2 = nat.returnRectArray(3);
    <b>for</b> (<b>int</b> i = 0; i &lt; rects2.length; i++)
      System.out.println(rects2[i].toString());

      <font color="#003399"><i>// Some exception handling examples</i></font>
      <font color="#003399"><i>// Notice that we must catch 'Throwable' objects and not</i></font>
      <font color="#003399"><i>// Exception objects. It turns out that these tests are</i></font>
      <font color="#003399"><i>// actually returning Error objects, but both Error and</i></font>
      <font color="#003399"><i>// Exception derive from Throwable.</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 8: Native.handleException&quot;</font>);
    <b>try</b>
    {
      nat.handleException();
    }
    <b>catch</b> (Throwable e)
    {
      System.out.println(<font color="#9933CC">&quot;Exception in Native code not handled: &quot;</font> + e);
    }

    System.out.println(<font color="#9933CC">&quot;\n******* Test 9: Native.causeException&quot;</font>);
    <b>try</b>
    {
      nat.causeException();
    }
    <b>catch</b> (Throwable e)
    {
      System.out.println(<font color="#9933CC">&quot;Exception in Native code not handled: &quot;</font> + e);
    }


      <font color="#003399"><i>// 12-18-98</i></font>
    System.out.println(<font color="#9933CC">&quot;\n******* Test 10: Native.pass2DByteArray(array2D)&quot;</font>);
    <b>int</b> dim1 = 2, dim2 = 3;
    <b>byte</b> count = 0;
    <b>byte</b>[][] array2D = <b>new</b> <b>byte</b>[dim1][dim2];
    <b>for</b> (<b>int</b> i = 0; i &lt; dim1; i++)
      <b>for</b> (<b>int</b> j = 0; j &lt; dim2; j++)
        array2D[i][j] = ++count;

      <font color="#003399"><i>// Call Native code to initialize an array to a specific value many times</i></font>
    nat.pass2DByteArray(array2D);

    System.out.println(<font color="#9933CC">&quot;\nIn Java printing array that was changed by native method.&quot;</font>);
    <b>for</b> (<b>int</b> i = 0; i &lt; dim1; i++)
      <b>for</b> (<b>int</b> j = 0; j &lt; dim2; j++)
        System.out.println(i + <font color="#9933CC">&quot;,&quot;</font> + j + <font color="#9933CC">&quot; = &quot;</font> + array2D[i][j]);


    System.out.println(<font color="#9933CC">&quot;\n\n********** Timings **********\n&quot;</font>);

    System.out.print(<font color="#9933CC">&quot;Native countPrimes up to &quot;</font> + size + <font color="#9933CC">&quot; ....&quot;</font>);
    start = System.currentTimeMillis();
    nat.countPrimes(c);
    stop = System.currentTimeMillis();
    elapsed = stop - start;
    System.out.println(<font color="#9933CC">&quot; &quot;</font> + elapsed + <font color="#9933CC">&quot; milliseconds&quot;</font>);

    System.out.print(<font color="#9933CC">&quot;  Java countPrimes up to &quot;</font> + size + <font color="#9933CC">&quot; ....&quot;</font>);
    start = System.currentTimeMillis();
    countPrimes(c);
    stop = System.currentTimeMillis();
    elapsed = stop - start;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -