Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I map a string buffer to a java class?

 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dean Jones
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you would have to do, of course, is write the method to do it yourself:


[ January 06, 2005: Message edited by: Joel McNary ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another version:
 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic