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