Hi
I might need to elaborate on what I'm really trying to do,
I am writing an API to allow simple message passing of arrays between
VM's on different machines. I'd like to use UDP and Datagrams for the
trasfer ( I have my reasons, they may not be valid though ).
I want it to be as light weight and quick as possible:
So just a simple byte tag to identify the type of the array, rather than
serializing the array.
As little memory copying between the array itself and a DatagramPacket
as possible, the minimum being zero if it is feasible ( which I am
beginning to think isn't).
What I really want is to be able to view a primitive array,
as an array of any other primitive type.
ie
byte[16] bytearray int[4] intarray double[2] doublearry
0_____8 0_______8 0_______8
0 |__A__| 0 | A | | A |
1 |__B__| |___B___| | B |
2 |__C__| 1 | C | | C |
3 |__D__| |___D___| |___D___|
4 |__E__| 2 | E | | E |
5 |__F__| |___F___| | F |
6 |__G__| 3 | G | | G |
7 |__H__| |___H___| |___H___|
8 |__I__| 4 | I | | I |
9 |__J__| |___J___| | J |
10|__K__| 5 | K | | K |
11|__L__| |___L___| |___L___|
12|__M__| 6 | M | | M |
13|__N__| |___N___| | N |
14|__O__| 7 | O | | O |
15|__P__| |___P___| |___P___|
all pointing at the same array in memory.
I can use a java.nio.ByteBuffer to put a double into a ByteArray, and
view it as both 8 bytes and 2 integers etc.
but I cannot create an array of one type and then place it straight into
a DatagramPacket constructor as the buffer to use for the data like
this:
sendDouble( double[] data , offset , length , address , port){
//copy double[] data into new byte[] newData
dsocket.send( new DatagramPacket( newData , offset , length,
address , port));
}
without doing a memory copy of some kind to get the data out of the
double array into a byte array even though they contain the same
bytes...
hope that makes sense.
Is java designed to prevent this kind of memory tampering, even if the
view would be read only?
I'll just have to settle for a single memory copy( + the one from the
DatagramPacket to the network interface w3hich I can't do anything
about) I suppose.
Thanks
Neomorph wrote:
On Wed, 06 Aug 2003 11:36:59 +0100, matt melton <ha***********@yahoo.co.uk>
two-finger typed:
Hi there,
I am trying to write a method that accepts an array of any primitive
type and will return the same array without copying memory as an array
of bytes.
You will have to overload the method (i.e. write a different method for
each type of array).
ie. I'd like to be able to do something like:
char[] chars = "Hello!";
You are assigning a String object to an array ?
Use:
String hello = "Hello!" ;
char [] chars = new char [hello.length()];
hello.getChars(0,hello.length(),chars,0);
Or:
char [] chars = getChars("Hello!");
with an extra (local) method:
public char [] getChars(String s) {
char [] chars = new char [s.length()];
hello.getChars(0,s.length(),chars,0);
}
byte[] bytes = (byte[]) chars;
bytes and chars are two different things.
chars are 16 bit (Unicode), bytes are 8 bit.
The resulting bytes out of a string would have to represent an encoding of
the string to support Unicode characters above the codevalue 255.
Have a look at the String API for more information.
Converting doubles and floats into bytes may pose even more of a challenge,
if you want the storage to be according to IEEE standards.
Have you looked at Serialization ?
which obviously won't work.
Is there any way of casting arrays?
Thanks for any help you can give me.
Matt
Cheers.