91
General Programming / Re: [Java] Buffer Problems (Again)
« on: July 21, 2008, 01:02:57 pm »
That's alot of extra code ;;
There isn't a shorter way to do this?
There isn't a shorter way to do this?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
You can't use a Writer for binary data. Writers are to Streams as Strings are to byte arrays. You need to use byte arrays for binary data; if you Strings, you can be guaranteed that your code will work inconsistently in different environments.That's it, right? :p
Even if you use a Stream, Print* is still inappropriate for sockets (but not because it won't work). You should use a packet buffer to construct your packets and convert them to a byte[], and then write() that directly to the socket's OutputStream, thus making the write operation atomic, fault-resistant, and thread-safe.
byte []packet = new byte[127];
input.read(packet);
but I have to specify the size which is completely inaccurate and it throws off my data by adding lots of 0x00's. I need a solution to this problem ;( int len1 = (input.read() & 0x000000FF);
int len2 = (input.read() & 0x000000FF) << 8;
int length = len1 | len2;
byte []packet = new byte[length];
for(int i = 0; i < packet.length; i++)
packet[i] = (byte)input.read();
results in my incoming packets not processing for some reason.register.registerIncomingPacketPlugin(this, 1, null);
?