MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: byte[ ] of J/Link

  • To: mathgroup at
  • Subject: [mg30437] Re: byte[ ] of J/Link
  • From: tgayley at (Todd Gayley)
  • Date: Sat, 18 Aug 2001 04:05:00 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <9lih6b$isq$>
  • Sender: owner-wri-mathgroup at

On Fri, 17 Aug 2001 07:30:19 +0000 (UTC), "Junzo Sato" <jsato at>

>I'm trying to create byte[] with specified length from Mathematica.
>How to implement the following Java code in Mathematica?
>byte[] data = null;
>RandomAccessFile rf = null;
>	rf=new RandomAccessFile("","r");
>	data=new byte[(int)rf.length()];
>	rf.readFully(data);
>}catch(IOException e){
>	e.printStackTrace();
>data= <???>


There is code very similar to yours in the GetURL example in JLink/Examples/Part1, and the
code is discussed in section 1.3.4 of the J/Link User Guide (Part 1/Example
Programs/Reading a URL). People who want a fuller explanation for this array trick can
read that section.

The actual Java class name of a 1-D array of bytes is "[B", so this is what you specify as
the class name to JavaNew:

    data = JavaNew["[B", rf@length[]]

The cryptic class names for arrays of primitive types can be found in the Java
documentation (I know they are listed somewhere in the JNI docs), but you can also write a
simple Java program to get them. For example:

    byte[] foo = new byte[1];
    System.out.println(foo.getClass().getName());  // This prints out "[B"

--Todd Gayley
Wolfram Research

  • Prev by Date: Re: How to do a simple loop?
  • Next by Date: Re: Mathlink and BorlandC++
  • Previous by thread: byte[ ] of J/Link
  • Next by thread: Optica - freeforms with xyz-points