This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Supposedly, The first 4 bytes would give me either "2001"/ "2002"/ "2003"/ "2004" The second 4 bytes would give me information about the number of bytes contained in the data portion of the message. The 9th bytes to N would give me the data portion of the message.
I've tried to do these lines of codes below to try to get the first 8 bytes
Below is the System.out.print result for the first 8 bytes:
I don't understand what does this mean? I'm not sure whether I do something wrong or not. Does this mean that there's something wrong with the stream. Could someone please advice?
First, the available() method is almost completely useless, and should be ignored here. It doesn't tell you how many bytes will eventually be available; it tells you how many are avaailable right now. Often you just need to wait for the rest of the data to be available. In general, if you want to read something until the end, check the return value of the read() method. If it returns -1, the stream is done. until then, just keep reading. See Available Doesn't Do What You Think It Does for more info.
I don't see anything in your code which is taking the first 4 bytes, ro the first 8 bytes, and doing something special with them. I see code that takes the first 64 bytes, but I don't understand why you would want to do that.
If the first four bytes represent a number, it's important to know how the number was written. Was it written as text? (Probably not, based on the unprintable characters you're seeing.) Or was it written using some binary format? The most likely option is that it was written in the same format that writeInt() and readInt() use - methods on DateInputStream and DataOutputStream. See the API for more info. If that's the case then you should simply call readInt() twice to read the two numbers.
On further study, I don't think that's the case. Have you studied how binary and hexadecimal numbers work? You may need some special tricks to convert these four bytes into a single number. There are a number of ways to do this, but I don't know how much you know about manipulating bits and bytes. You can think of the four bytes as a number in base 256, where each byte is one digit. If the bytes were in order with the most significant byte first, you could just use readInt(). Instead, it looks to me like they're in the reverse order instead. You could try reordering the bytes before you use readInt(). Or you could use some other bit manipulation (e.g. shift operators) to push the bytes around and form a new int out of them. There's also a relatively simple way to read the numbers using a java.nio.ByteBuffer, if you're familiar with those - but if you're not, it may be better to use methods you are more familiar with. Does any of this sound familiar? [ November 09, 2007: Message edited by: Jim Yingst ]