reading & writing binaries(.class files)

folks,how does one go about reading and writing .class files in java. I am able to write 'em only in ASCII format which is not what i want.Thanks!
[168 byte] By [Ralf_Pa] at [2007-11-27 0:46:17]
# 1
.class files are bytecode.
CaptainMorgan08a at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 2
i know.. i want to be able to read-write it and load it gainThanks
Ralf_Pa at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 3

> how does one go about reading and writing .class

> files in java. I am able to write 'em only in ASCII

> format which is not what i want.

>

The IO classes deal with 'text' and 'bytes'.

You are using an IO class that deals with text. You need to pick a different one that only deals in bytes.

jschella at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 4
would ByteArrayInputStream be a good option?thanks!
Ralf_Pa at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 5
> would ByteArrayInputStream be a good option?> Not unless the source is a file rather than a byte array.
jschella at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 6
source is a .class file
Ralf_Pa at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 7

Not my point - I am not going to go look up the API but I am rather certain that if you read the documentation for that class then you will find that the source (where the data comes from) is a byte array.

And that won't help you because a byte array is not a file (or stream.)

But perhaps I am wrong.

jschella at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 8
i think, Serialization API should help achieve this. am i correct?Thanks!
Ralf_Pa at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 9
Ralf!! jschell has already given you plenty of info. He has indicated that you want to use a stream that can read bytes from a file - FileInputStream leaps out if you glance at the javadocs.
Tillermana at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 10

You're much better off using a library, such as BCEL or ASM to read and write to class files.

If you're having trouble simply opening up a binary file, then disassembling a class file is too advanced for you.

Here's the class file format...

http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#74353

regards,

Owen

omcgoverna at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 11
Did anybody say anything about disassembling? Or was that just you? I think the OP is flat out doing binary I/O myself. To which he's been given the answer.
ejpa at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 12

>Did anybody say anything about disassembling?

He wants to read and write class files, it's the title of the post.

I don't appreciate you dismissing my suggestions like that.

There's plenty of gotchas in reading and writing class files, such as Longs & Doubles taking up 2 positions in the constant pool.

Hence the reason why I suggested using an open source library like BCEL or ASM. These are still valid as class readers, even if you don't touch the byte code. ( disassembly )

But what would I know Ejp....sure I only wrote my own Java 1.5 class reader / disassembler.

omcgoverna at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 13

That's nice, and maybe when it comes to disassembling .class files you outrank me from hell to breakfast time, but so what? Does it have anything to do with the OP's problem? He's got as far as suggesting serialization for the task, and realizing that text I/O is wrong. Where exactly did he say anything about assembling or disassembling? Where is the evidence that he wants anything but accurate binary I/O?

ejpa at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 14

Ejp, you haven't contributed anything constructive to this post.

The OP wants to read and write class files.

I gave him the class file format link, essential for the task.

I also suggested the use of certain open source libraries if he doesn't want to reinvent the wheel.

Those libraries can be used as simple class readers ( what the OP wants to do ), or disassemblers for more advanced stuff.

Sometimes posters appreciate a broader answer, than just

"use java.io.DataInputStream".

omcgoverna at 2007-7-11 23:12:18 > top of Java-index,Java Essentials,Java Programming...
# 15

perhaps, i wasn't specific enough.

ok, i read the .class file using the read() method of FileInputStream to read the bytes.

store it in a string.

send it to another location(as a string)

at the destination, getBytes() from the string.

write 'em using OutputStream method write().

the files at the source and destination are same(atleast they appear to be) but still i'm not able to load the class at destination.

i'm a novice in this domain and would appreciate any help. i believe i'm messing with the binary data somewhere.

Thanks!

Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 16

So you've read the file, maybe reading it with a FileInputStream, and storing it in an array of bytes, or writing it to a ByteArrayOutputStream ?

After that you should have a nice array of bytes.

You cannot translate that array of bytes into a string directly.

This is because not all byte sequences are valid unicode character codes.So a random byte array of 0x24, 0x35, 0x44, 0x12 may not make a valid string. In fact, Java tends to change any unknown characters into "?".

So, if you "really" must store binary data into a string, you would typically encode it, either as a hex string, or using Base64 encoding.

Then you send that encoded string.

The receiver decodes it, and that decoding process recreates the binary byte array.

Alternatively, maybe you can just "send" the byte array directly ?

regards,

Owen

omcgoverna at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 17
sending bytes would be logical but i'm sending it as a string since i'm using a generic function which uses string argument.thanks!
Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 18

> Ejp, you haven't contributed anything

> constructive to this post.

>

> The OP wants to read and write class files.

> I gave him the class file format link, essential for

> the task.

> I also suggested the use of certain open source

> libraries if he doesn't want to reinvent the wheel.

>

> Those libraries can be used as simple class readers (

> what the OP wants to do ), or disassemblers for more

> advanced stuff.

>

> Sometimes posters appreciate a broader answer, than

> just

> "use java.io.DataInputStream".

Ok. But that isn't what ejp nor I interpreted OP ask asking.

And in fact the last post by the OP suggests that our interpretation is correct - the OP just wants to copy files. And thus the fact that they are class files is irrelevant.

So if you wish to post a more complete solution on how to copy files now is your chance.

jschella at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 19

> sending bytes would be logical but i'm sending it as

> a string since i'm using a generic function which

> uses string argument.

>

Then you are either doing it wrong or you are likely to have a problem.

A string contains characters. A class file contains binary data. Characters are not the same as binary data.

Now if you absolutely must use a string then you are going to have to encode the binary data in to characters. To do that you need an encoding method. One example is base64.

You would have to encode it at one end, then decode it at the other.

jschella at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 20

> And in fact the last post by the OP suggests that our

> interpretation is correct - the OP just wants to copy

> files. And thus the fact that they are class files

> is irrelevant.

i don't think it is irrelevant because i have to load the class files after writing them. i may be wrong.

> So if you wish to post a more complete solution on

> how to copy files now is your chance.

i have been able to copy the files and need no complete solution. its just that i haven't been able to load the copied class files due to the classFormatError

Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 21
sorry, but cant you just copy the files? or are they on different computers?
mkoryaka at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 22
yes . they are on different computers and are to be sent as strings
Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 23

> i don't think it is irrelevant because i have to

> load the class files after writing them. i may be

> wrong.

>

You wouldn't normally use bcel to load a class. The VM does that for you.

>

>

> > So if you wish to post a more complete solution on

> > how to copy files now is your chance.

> i have been able to copy the files and need no

> complete solution. its just that i haven't been able

> to load the copied class files due to the

> classFormatError

Which you would get if you used 'text' rather than 'bytes'.

And that tells you that the class file (that you copied) is just wrong. (Noting that bcel isn't going to help with that.)

jschella at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 24

> You wouldn't normally use bcel to load a class. The

> VM does that for you.

i have a written a customized file loader which works fine in normal cases. it doesn't work for this case where i'm trying to write the class file.

> And that tells you that the class file (that you

> copied) is just wrong. (Noting that bcel isn't going

> to help with that.)

that is exactly what i'm trying to figure out :-) (how to copy it correct)

thanks

Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 25

> > And that tells you that the class file (that you

> > copied) is just wrong. (Noting that bcel isn't going

> > to help with that.)

> that is exactly what i'm trying to figure out :-)

>(how to copy it correct)

> thanks

No you have already moved something - a file exists on the destinatin correct? And it isn't empty?

The problem, again, is that you are using 'text' and not 'bytes'.

One or all of the following is true.

1. You are reading the file using strings (text)

2. You are 'moving' the data via strings.

3. You are writing the file using strings.

You can't do that.

jschella at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 26
> 2. You are 'moving' the data via strings.i have to do this, so i used base64 encoding as suggested. i was about to run the program and goterror loading : ......\jre1.5.XX\bin\client\jvm.dllhaha! i'm doomed
Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 27
ok.got it up again. but still getting the ClassFormatError even after using Base64. this time it says 'Truncated Class File'
Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 28
> ok.got it up again. but still getting the> ClassFormatError even after using Base64. this time> it says 'Truncated Class File'Something is too short. Either you didn't read everything or you didn't write everything.
jschella at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 29

> You cannot translate that array of bytes into a

> string directly.

> This is because not all byte sequences are valid

> unicode character codes.So a random byte array of

> 0x24, 0x35, 0x44, 0x12 may not make a valid string.

> In fact, Java tends to change any unknown characters

> into "?".

>

> So, if you "really" must store binary data into a

> string, you would typically encode it, either as a

> hex string, or using Base64 encoding.

>

> Then you send that encoded string.

> The receiver decodes it, and that decoding process

> recreates the binary byte array.

>

> Alternatively, maybe you can just "send" the byte

> array directly ?

>

> regards,

> Owen

Base64 worked!!

Sincere thanks to those who were patient enough to understand the problem and not arrogant to a novice.

Ralf_Pa at 2007-7-21 19:52:05 > top of Java-index,Java Essentials,Java Programming...
# 30

> Ejp, you haven't contributed anything

> constructive to this post.

That would just makes us even at best. There is nothing constructive about irrelevance. There is arguably something positive about pointing out irrelevance and keeping the discussion on-topic.

And there is nothing that is even logical about appealing to yourself as an authority.

ejpa at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 31

> Sincere thanks to those who were patient enough to

> understand the problem and not arrogant to a novice.

It's hard to understand the problems when a novice knows how to load classes dynamically, but doesn't know how to send data from one computer to another (and "must" do it as a String?).

It would be quite like helping a mathematician with addition and subtraction.

We need to assume a lot of things from many posts, because they're rarely expressed clearly enough for everyone to get the proper idea.

-Kayaman-a at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 32

If that refers to this thread I don't think that any of it applies actually. The OP said he wanted to read and write .class files, and that's the question that got answered, and not by anybody making any assumptions. The people that did the assuming were the people who made the mistakes actually.

Personally I think it's more educational, and therefore ultimately more helpful and useful, to answer the question that is being asked. It may well turn out to be the wrong question, but that's educational too. If I don't understand the question I generally ask for more information rather than make assumptions. Assumptions cause bugs. A large part of this job is learning to ask the right question, or learning how to discover what the right question really is, and learning how to narrow the focus, and identify and reject false assumptions, until the answer appears.

ejpa at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 33

>The people that did the assuming were the people who made the mistakes actually

If the OP said he wanted to read and write Excel files, it would be fair to point him to a library like POI or JExcel.

He said he wanted to read and write class files, so it was fair to assume he wanted to read and interpret the contents.

Nobody is wrong, when the question is not specific.

Ejp, maybe you should stick to your authoring job instead.

omcgoverna at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 34
You're now making uninformed and unwarranted assumptions about my job, which you certainly know nothing about.Don't you think it's time you stopped jumping to conclusions?
ejpa at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 35

> It's hard to understand the problems when a novice

> knows how to load classes dynamically, but doesn't

> know how to send data from one computer to another

> (and "must" do it as a String?).

>

> It would be quite like helping a mathematician with

> addition and subtraction.

i was told that i had to use a customized class loader. Although I don't have a decent experience in Java, i figured out how i'd do that. no biggie..But as a novice(without any experience in Java file handling), i didn't know that a class file cannot be directly sent as a string (and that was a requirement). how would it strike a novice to use BASE 64 encoding?

Also, i think, folks should stop blaming each other for wrong suggestions. Why do you guyz wanna make an issue when i have already found the solution(thanks to ya all). i hope the forum is more cooperative in future and i also look forward to contributing when i have decent experience

Thanks!

Ralf_Pa at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 36

> But as a novice(without any experience in Java file

> handling), i didn't know that a class file cannot be

> directly sent as a string (and that was a

> requirement). how would it strike a novice to use

> BASE 64 encoding?

It's good you have a solution to this problem, but let me just point out that the requirement for sending binary data as a string is, um, to put it politely, unusual. All of the standard methods for transferring files just copy the files, byte by byte, from source to target. Converting from format A to format B, transmitting, and converting from format B to format A is just an invitation for things to go wrong.

I am sure you were just given these requirements and didn't think them up yourself. I just wanted to make sure you didn't think they were the world's best requirements and that when you get around to designing such things you don't do that.

DrClapa at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...
# 37
It's also a completely pointless overhead at both ends, and on the network.
ejpa at 2007-7-21 19:52:10 > top of Java-index,Java Essentials,Java Programming...