aspose file tools*
The moose likes Beginning Java and the fly likes Can I map a string buffer to a java class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I map a string buffer to a java class?" Watch "Can I map a string buffer to a java class?" New topic
Author

Can I map a string buffer to a java class?

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 382
Like in C++...

struct DATA{
char companyID[10];
char filler1[10];
char InvoiceDate[10];
}data;

char buf[]= {"ccccccccccffffffffffiiiiiiiiii"};

memcpy(&data, buf, sizeof DATA); //map the buffer to the struct
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The state variables of a StringBuffer object are about what you would expect:

You will note the access modifier private. That means that you can't copy or change the state of a StringBuffer object without using the public methods and constructors documented in the API.

What would happen if you accessed these private members directly and Sun changed them next year to add features or take advantage of better hardware?

So the short answer to your question is no.


Mike Gershman
SCJP 1.4, SCWCD in process
Dean Jones
Greenhorn

Joined: Jan 04, 2005
Posts: 8
Remember, Java doesn't really allow you to work closely with memory. What memcpy() essentially does is accepts two pointers to memory addresses and then copies memory starting from address b, over to an area starting at address a. Since Java is so high level, there isn't a way to manipulate memory like this. Hence why pointers in C are so powerful and yet, also become such a problem to people who really don't know what they are.
[ January 05, 2005: Message edited by: Dean Joness ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
What you would have to do, of course, is write the method to do it yourself:


[ January 06, 2005: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Here is another version:
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 382
Yes, I ended up doing a bunch of string copies. To bad there is no memcpy() in Java like in C
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
There is a System.arraycopy method that takes two arrays, a start and end point for each and copies the data over. I'm not sure if this is whay you're looking for, but It would be better than looping through a array.
Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
Originally posted by M Burke:
Yes, I ended up doing a bunch of string copies. To bad there is no memcpy() in Java like in C


If that's really a problem then you're using the wrong language: use C.

I suspect however it's more the case that you're not yet up to speed with Java and you haven't taken a couple of steps back and asked "what am I actually trying to achieve here" and from there "what's the best way to do that in Java that fits Java's paradigm: not C's".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I map a string buffer to a java class?
 
Similar Threads
How to use C++ created binary files
Is NIO API really worth a try?
NIO and performance question
How to use excel file in C program for file handeling?
How do you read packets off a stream?