ByteBuffer and arrays

J

Jesper Sahner

Hi!

Using the Buffer-classes in Java NIO you can read blocks/arrays of
data by using allocate() or wrap() on an existing array. Further you
can use a view-buffer to read integers, doubles etc. into a ByteBuffer
as if it was integers, doubles etc. you were reading.

So far so good.

Let's say that you wan't to read a sequence of e.g. integers a, b, c,
d and e. An array x[5] can be read directly using an IntBuffer or
using ByteBuffer.asIntBuffer().

Is it possible to read a, b, c, d and e the same way, meaning in one
read?

If NOT you'll have to:
1) read a, b, c, d and e one by one
or:
2) read into an array x[5] and then put a=x[0],...,e=x[4]

Neither of these methods are elegant or (even worse) fast ways to read
data.

Any suggestions for improvements?

Regards,
Jesper
 
M

Michael Borgwardt

Jesper said:
Let's say that you wan't to

Misusing apostrophes like that makes Baby Jesus cry!
read a sequence of e.g. integers a, b, c,
d and e. An array x[5] can be read directly using an IntBuffer or
using ByteBuffer.asIntBuffer().

Is it possible to read a, b, c, d and e the same way, meaning in one
read?

You mean putting the values into four separate variables?
If NOT you'll have to:
1) read a, b, c, d and e one by one
or:
2) read into an array x[5] and then put a=x[0],...,e=x[4]

Neither of these methods are elegant or (even worse) fast ways to read
data.

Well, having five variables named a, b, c, d and e isn't an elegant way
to *represent* data. And your speed worries are totally unfounded.
Even if the language or API had syntactic sugar to do what you want,
it would end up doing one of these things.
 
J

Jesper Sahner

Michael Borgwardt said:
Jesper said:
Let's say that you wan't to

Misusing apostrophes like that makes Baby Jesus cry!
read a sequence of e.g. integers a, b, c,
d and e. An array x[5] can be read directly using an IntBuffer or
using ByteBuffer.asIntBuffer().

Is it possible to read a, b, c, d and e the same way, meaning in one
read?

You mean putting the values into four separate variables?
If NOT you'll have to:
1) read a, b, c, d and e one by one
or:
2) read into an array x[5] and then put a=x[0],...,e=x[4]

Neither of these methods are elegant or (even worse) fast ways to read
data.

Well, having five variables named a, b, c, d and e isn't an elegant way
to *represent* data. And your speed worries are totally unfounded.
Even if the language or API had syntactic sugar to do what you want,
it would end up doing one of these things.

If you are reading data from a database-like file it is convenient,
that the variables have "real" names instead of array-names.

I do not agree on the last past. When putting a=x[0],...,e=x[4] you
make a full copy of variables already existing in memory which
shouldn't be necessary. It should be possible to "map" between {a, b,
c, d, e} and x[] without making a copy - like you would do, if you
were using objects (multiple references to the same object). Guess the
problem is, that you can't refer to primitive variables with "multiple
names" in Java.

But maybe you are right, that the "copying-procedure" is not very
time-comsuming!
 
M

Michael Borgwardt

Jesper said:
Well, having five variables named a, b, c, d and e isn't an elegant way
to *represent* data. And your speed worries are totally unfounded.
Even if the language or API had syntactic sugar to do what you want,
it would end up doing one of these things.


If you are reading data from a database-like file it is convenient,
that the variables have "real" names instead of array-names.

I do not agree on the last past. When putting a=x[0],...,e=x[4] you
make a full copy of variables already existing in memory which
shouldn't be necessary.

Technically, you usually end up doing *multiple* such copies. It is
not a problem.
It should be possible to "map" between {a, b,
c, d, e} and x[] without making a copy - like you would do, if you
were using objects (multiple references to the same object). Guess the
problem is, that you can't refer to primitive variables with "multiple
names" in Java.

You'd still be making copies of the *references*, which are just
as big as (if not bigger than) the values.
But maybe you are right, that the "copying-procedure" is not very
time-comsuming!

Compared to the time it takes to read the stuff from disk, it's totally
insignificant - we're talking about a factor of ten thousand to one,
if not a million to one.
 
J

Jesper Sahner

Michael Borgwardt said:
Jesper said:
Well, having five variables named a, b, c, d and e isn't an elegant way
to *represent* data. And your speed worries are totally unfounded.
Even if the language or API had syntactic sugar to do what you want,
it would end up doing one of these things.


If you are reading data from a database-like file it is convenient,
that the variables have "real" names instead of array-names.

I do not agree on the last past. When putting a=x[0],...,e=x[4] you
make a full copy of variables already existing in memory which
shouldn't be necessary.

Technically, you usually end up doing *multiple* such copies. It is
not a problem.
It should be possible to "map" between {a, b,
c, d, e} and x[] without making a copy - like you would do, if you
were using objects (multiple references to the same object). Guess the
problem is, that you can't refer to primitive variables with "multiple
names" in Java.

You'd still be making copies of the *references*, which are just
as big as (if not bigger than) the values.
I am not sure about this. Let's say that you have an object with two
references. Any manipulation on the object will take the same amount
of time no matter if you have one or two references, because the
references are just pointers to (the same) memory-address, while the
object itself exist in only one "copy".

Example:
Point p,q;
p=new Point();
q=p;
p.x=5;

- implies q.x=5

Usually it is the object itself that takes up memory, not the
references, I think.

In this small example p and q refer to the same object. It is the same
functionality that I am looking for with primitive variables, but I
don't think this is supported in Java (no pointers).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top