D
David N. Welton
Hello,
I am writing a classloader for a project of mine, where the load portion
of the code takes a string as input. Since defineClass takes a byte[]
as an argument, my first instinct was to use String.getBytes, but then I
realized that that *encodes* the bytes into some sort of format (such as
UTF-8) possibly mangling them, whereas it's getChars that just gives you
back what the string contains with no fuss. Encoding discussions still
make my head spin just a bit, but what ended up working for me was this:
String sdata = argv[2].toString();
int len = sdata.length();
char[] chars = new char[len];
byte[] bytes = new byte[len];
sdata.getChars(0, len, chars, 0);
for (int i = 0; i < chars.length; i++) {
bytes = (byte)chars;
}
and then shipping those bytes off to defineClass, which works just fine.
I got the chars off the disk in the first place, byte by byte, so going
back from char to byte ought to be ok...right? Is there a cleaner way
of doing this, though? Data is read into the String like so:
fis = new BufferedInputStream(new FileInputStream(realfn));
int total;
int ch;
for (total = 0; (ch = fis.read()) != -1; total ++) {
data.append((char)ch);
}
Thankyou for your time,
--
David N. Welton
- http://www.dedasys.com/davidw/
Linux, Open Source Consulting
- http://www.dedasys.com/
I am writing a classloader for a project of mine, where the load portion
of the code takes a string as input. Since defineClass takes a byte[]
as an argument, my first instinct was to use String.getBytes, but then I
realized that that *encodes* the bytes into some sort of format (such as
UTF-8) possibly mangling them, whereas it's getChars that just gives you
back what the string contains with no fuss. Encoding discussions still
make my head spin just a bit, but what ended up working for me was this:
String sdata = argv[2].toString();
int len = sdata.length();
char[] chars = new char[len];
byte[] bytes = new byte[len];
sdata.getChars(0, len, chars, 0);
for (int i = 0; i < chars.length; i++) {
bytes = (byte)chars;
}
and then shipping those bytes off to defineClass, which works just fine.
I got the chars off the disk in the first place, byte by byte, so going
back from char to byte ought to be ok...right? Is there a cleaner way
of doing this, though? Data is read into the String like so:
fis = new BufferedInputStream(new FileInputStream(realfn));
int total;
int ch;
for (total = 0; (ch = fis.read()) != -1; total ++) {
data.append((char)ch);
}
Thankyou for your time,
--
David N. Welton
- http://www.dedasys.com/davidw/
Linux, Open Source Consulting
- http://www.dedasys.com/