Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

Re: byte[ ] of J/Link

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

On Fri, 17 Aug 2001 07:30:19 +0000 (UTC), "Junzo Sato" <jsato at fc.kuh.kumamoto-u.ac.jp>
wrote:

>Hello.
>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;
>try{
>	rf=new RandomAccessFile("myfile.data","r");
>	data=new byte[(int)rf.length()];
>	rf.readFully(data);
>}catch(IOException e){
>	e.printStackTrace();
>}
>
>---
>
>rf=JavaNew["java.io.RandomAccessFile","myfile.data","r"];
>
>data= <???>
>
>rf@readFully[data];


Junzo,

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