Home arrow static arrow Java Programming [Archive] - Will this be problematic?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Will this be problematic?
This topic has 27 replies on 2 pages.    1 | 2 | Next »

Posts:343
Registered: 7/13/04
Will this be problematic?  
Jul 16, 2004 1:56 PM



 
public static byte[] ReadData(DataInputStream in) {        byte[] message = null ;        in.skip(2)	try {                for (int i = 0; in.readChar() != 0xA6B1; i ++)      {		int j = 2;		in.read(message, message[i], j);		}	} catch (Exception e) {		System.out.println("Error -- " + e.toString());	}	return message;        }}


first, i'm just reading a huge binary file and A6B1 is the Sync word (in hex) that tells me when the message is over. two things concern me: the fact that I initialize message = null (I did this because I want the array only the size it needs to be), and secondly i dont know if I need the "0x" before the A6B1 or not.

thnx guys
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 2:10 PM (reply 1 of 27)



 
yeah, you'll get - um - what are you doing here? From the docs:

public final int read(byte[] b, int off, int len)

If b is null, a NullPointerException is thrown.

You clearly pass this thing a null buffer, and then you try and reference a non-existent element in the null array that you want to read data into and call that an offset? And you throw away the return value from your readChar method? And you use a for loop when you don't really want to do something for a given number of times?

Either this code makes no sense, or I just got overloaded... Either way, yeah, this is problematic - and yes, be concerned that you set message to null and never make a new byte array, - but yes, you do need the 0x in front of hex numbers.

Good Luck

Lee
 

Posts:441
Registered: 2/25/04
Re: Will this be problematic?  
Jul 16, 2004 2:11 PM (reply 2 of 27)



 
Yes.
message[i]
Will throw a NullPointer exception.

The data inputstream's read method will throw a NullPointer exception if its first arg is null.

You are using
read(byte[] b, int off, int len)
but the value for your offset is undefined, and your length is fixed as 2. If your message length is 2, then create message a array of dimension 2; if you need more than one message, use a dynamic collection.

You also are using the end-of-file exception to signal a normal condition-that the file has finished been read in, and reporting it as an error.

Pete
 

Posts:343
Registered: 7/13/04
Re: Will this be problematic?  
Jul 16, 2004 2:14 PM (reply 3 of 27)



 
is there some way to set the byte array to just the size i need wtihout just guessing how many bytes the file is?
 

Posts:343
Registered: 7/13/04
Re: Will this be problematic?  
Jul 16, 2004 2:18 PM (reply 4 of 27)



 
and I understand how the readChar() part might be confusing, I just did it that way because am using the readChar() method to determine when i need to stop reading this file, the actual bytes i was going to store in the message array
 

Posts:343
Registered: 7/13/04
Re: Will this be problematic?  
Jul 16, 2004 2:27 PM (reply 5 of 27)



 
I modified it taking into account what was said, but it still doesn't look right. (worse, I created a fake binary file, and a println statement to try and test it and nothing prints)

	public static byte[] ReadData() { 		byte[] message = new byte [1024] ;		try {			DataInputStream in = new DataInputStream(new FileInputStream("c:/test.txt"));			for (int i = 0, j = 2; in.read(message, message[i], j)!= 0xA6B1; i +=2){			} 		} catch (Exception e) {		    System.out.println("Error -- " + e.toString());		}		System.out.println("Message is: " + message);		return message; }
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 2:30 PM (reply 6 of 27)



 
and I understand how the readChar() part might be
confusing, I just did it that way because am using the
readChar() method to determine when i need to stop
reading this file, the actual bytes i was going to
store in the message array

But once you call readChar and throw away the return value the file pointer has advanced and you lose that data that was read, so if those are bytes you are interested in they have gone away.

is there some way to set the byte array to just the size
i need wtihout just guessing how many bytes the file is?

Well, no. You could call available() and make an array that large, then read into that, then see if you need to keep going, allocating buffers as you go, then concat them all together at the end... I think that'll work.

Good Luck

Lee
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 2:33 PM (reply 7 of 27)



 
Again, you say this:

in.read(message, message, j)

Message is now an initialized byte buffer, but you are saying you want to always read characters into an offset of message, which - I've no idea what this will be - 0? And at an offset of 2, which is the max numbers of chars to read - that doesn't seem right. And the read method that you've called returns the number of bytes read, not the actual data read, so comparing that to 0xa6b1 likely isn't what you want.
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 2:35 PM (reply 8 of 27)



 
woops - again, this time with feeling:

Again, you say this:
in.read(message, message[i], j)
Message is now an initialized byte buffer, but you are saying you want to always read characters into an offset of message, which - I've no idea what this will be - 0? And at an offset of 2, which is the max numbers of chars to read - that doesn't seem right. And the read method that you've called returns the number of bytes read, not the actual data read, so comparing that to 0xa6b1 likely isn't what you want.
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 2:36 PM (reply 9 of 27)



 
cripes - I can't think/type straight today:

...Message is now an initialized byte buffer, but you are saying you want to always read characters into an offset of message[i], which - I've no idea what this will be - 0? And a length of 2...
 

Posts:343
Registered: 7/13/04
Re: Will this be problematic?  
Jul 16, 2004 2:47 PM (reply 10 of 27)



 
I THOUGHT that the first time through the loop the offset will be 0, and message will read 2 bytes, then i wil increment (should be i+=2, not i++), then the next time through the loop the offset would be 2, and it would read 2 more bytes and so on and so forth. but you're right its not working, "ArrayIndexOutofBounds".

well, do you at least understand what i'm trying to do?
 

Posts:929
Registered: 9/30/02
Re: Will this be problematic?  
Jul 16, 2004 2:59 PM (reply 11 of 27)



 
If you look at the return of in.read, you'll notice that it returns how many bytes are read (or -1). Since you are reading two at a time, you'll always hit an array out of bounds first, since it will never equal 0xA6B1. If you want to check the value of characters read, you'll have to do that in the for loop. You'll still get a problem though any time that the input is more then a kilobyte long. You should store the buffer data elsewhere after it is filled, reset i when it is filled, or resize the buffer to prevent an array out of bounds.
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 3:06 PM (reply 12 of 27)



 
I THOUGHT that the first time through the loop the
offset will be 0, and message will read 2 bytes, then
i wil increment (should be i+=2, not i++), then the
next time through the loop the offset would be 2, and
it would read 2 more bytes and so on and so forth.
but you're right its not working,
"ArrayIndexOutofBounds".

well, do you at least understand what i'm trying to
do?

I do - maybe you should use a BufferedInputStream, and read the thing a buffer at a time, say 1k like you have it, then scan the buffer for your end-o-message sequence, if there trim the buffer to size, concat all the buffers together and return the resulting byte array? That's off the top of my head - there might be a more efficient way to do it.

Lee

 

Posts:343
Registered: 7/13/04
Re: Will this be problematic?  
Jul 16, 2004 4:42 PM (reply 13 of 27)



 
well i guess i better start surfing the internet to find out what the heck a BufferedInputStream is and how to use it cuase i dont have a clue. thanks for your help on giving me a good place to start Lee (or tsith, whichever you perfer) :)

Nick
 

Posts:7,258
Registered: 1/31/02
Re: Will this be problematic?  
Jul 16, 2004 6:31 PM (reply 14 of 27)



 
Nice nick, nick.

You should check out the API docs - they tell you everything you can possibly do with any given class - for instance:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedInputStream.html

I don't know if the solution I outlined is the best way to approach your problem, but it's a place to start.

Good Luck

Lee (or tsith, whatever you prefer)
 
This topic has 27 replies on 2 pages.    1 | 2 | Next »