• Welcome to Valhalla Legends Archive.
 

Java: CopyMemory equivalent

Started by Tuberload, April 17, 2003, 12:01 PM

Previous topic - Next topic

Tuberload

Is there an equivalent library in java to the CopyMemory API?
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

tA-Kane

#1
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.

Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
 int i;
 for (i=0;i++;i<length)
   *dest[i] = *src[i];
}


Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

TyC-Pros

This should come in handy for me as well, in Java it would become something like this:

public void CopyMemory(char[] src, char[] dest, int length)
{
 int i;
 for(i = 0; i < length; i++)
 {
    dest = src;
 }
}

Banana fanna fo fanna

Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.

Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
 int i;
 for (i=0;i++;i<length)
   *dest[i] = *src[i];
}


Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.

Too bad Java doesn't have pointers.

Tuberload: get the 1.4 SDK and take a look at java.nio.ByteBuffer. It should have everything you need except for string ops, which are pretty easy to code yourself. Remember: bytebuffer.order(ByteOrder.LITTLE_ENDIAN), or you'll find yourself IP banned. BTW, they work natively with SocketChannels, which are also nonblocking.

Hope this helps.

Etheran

Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.

Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
 int i;
 for (i=0;i++;i<length)
   *dest[i] = *src[i];
}


Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.

You should use void pointers instead :p

tA-Kane

Quote from: Etheran on April 18, 2003, 07:03 PMYou should use void pointers instead :p
I thought about that, but since I don't know how to access/set a void pointer without typecasting, I would just typecast back to a char and do it like it already is.

Being that that could confuse him, I figured I shouldn't do such.
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

Skywing

Quote from: tA-Kane on April 18, 2003, 08:30 PM
Quote from: Etheran on April 18, 2003, 07:03 PMYou should use void pointers instead :p
I thought about that, but since I don't know how to access/set a void pointer without typecasting, I would just typecast back to a char and do it like it already is.

Being that that could confuse him, I figured I shouldn't do such.
You can't.  What one would do is put have the prototype take void* parameters, and inside the function body, create seperate identifiers which are cast to char*.  That way, your callers don't need to cast every time.

Eibro

Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.

Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
 int i;
 for (i=0;i++;i<length)
   *dest[i] = *src[i];
}

Eh, that wouldn't work. Either use dest = src or *dest = *src. Using both together you're actually derefrencing the pointers twice.
Eibro of Yeti Lovers.

iago

Quote from: St0rm.iD on April 17, 2003, 08:50 PM
Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.

Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
 int i;
 for (i=0;i++;i<length)
   *dest[i] = *src[i];
}


Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.

Too bad Java doesn't have pointers.

Tuberload: get the 1.4 SDK and take a look at java.nio.ByteBuffer. It should have everything you need except for string ops, which are pretty easy to code yourself. Remember: bytebuffer.order(ByteOrder.LITTLE_ENDIAN), or you'll find yourself IP banned. BTW, they work natively with SocketChannels, which are also nonblocking.

Hope this helps.


That's a lie, Java has pointers, they're everywhere.  They're just hidden under a cute little layer saying "Look that's not a pointer! That's a reference! Look over there [runs]"


Anyway, Eibro's right, you are dereferencing the pointer twice in that c++ code and it won't work.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Banana fanna fo fanna

A reference is an auto-dereferenced pointer. All objects in Java are pointers that you cannot get the address of. Thus, all objects in Java are references.

$t0rm: 2
iago: 0

Camel

Quote from: St0rm.iD on April 20, 2003, 03:54 PM
A reference is an auto-dereferenced pointer. All objects in Java are pointers that you cannot get the address of. Thus, all objects in Java are references.

$t0rm: 2
iago: 0

sort of like vb, except you can do varptr/objptr/strptr/byval -_-

Banana fanna fo fanna

BTW, if you need "real" copy memory, look into System.arraycopy() and java.lang.Cloneable.

tA-Kane

Obviuously I've still got a lot to learn regarding C/C++. But, I think I've got the basic concept down.
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

Banana fanna fo fanna

Well, you wouldn't actually NEED CopyMemory in C, because you have memcpy. I suppose it would go like this:

void copymemory(void* src, void* dest, int len) {
for (int i = 0; i < len; i++) {
*dest = *src;
src++;
dest++;
}

Camel

Quote from: St0rm.iD on April 21, 2003, 06:20 PM
I suppose it would go like this:

void copymemory(void* src, void* dest, int len) {
for (int i = 0; i < len; i++) {
*dest = *src;
src++;
dest++;
}


or like this:
#define copymemory memcpy