Well i tried to clean these up but now im getting all kinds of weird packets like negative numbers and 2128319 e.c.t. what did i mess up?


Code:
	public boolean process() {
		int avail = 0;

		parseOutgoingPackets();

		if(in == null) return false;

		try {
			avail = in.available();
		} catch(IOException e) {
			return true;
		}

		if(avail == 0) return false;

		if(packetType == -1) {
			try {
				packetType = in.read() & 0xff;
			} catch(IOException e) {
				return true;
			}

			if(inStreamDecryption != null) packetType -= inStreamDecryption.getNextKey() & 0xff;
			try {
				packetSize = packetSizes[java.lang.Math.abs(packetType)];
			} catch(ArrayIndexOutOfBoundsException e) {
				packetType = packetSize = -1;
				return true;
			}
			avail--;
		}

		if(packetSize == -1) {
			if(avail > 0) {

				try {
					packetSize = in.read() & 0xff;
				} catch(IOException e) {
					return true;
				}

				avail--;
			} else return false;
		}
	
		if(avail < packetSize) return false;
	
		try {
			fillInStream(packetSize);
		} catch(IOException e) {
			return true;
		}

		clientHandler.parseIncomingPackets(this, packetType, packetSize);
		packetType = packetSize = -1;

		return true;
	}
Code:
	public void parseOutgoingPackets() {

		if(writePtr != readPtr) {
			offset = readPtr;

			if(writePtr >= readPtr) {
				numBytesInBuffer = writePtr - readPtr;
			} else {
				numBytesInBuffer = BUFFER_SIZE - readPtr;
			}

			if(numBytesInBuffer > 0) {
				try {
					out.write(buffer, offset, numBytesInBuffer);
				} catch(IOException e) {
					disconnected = true;
					return;
				}

				readPtr = (readPtr + numBytesInBuffer) % BUFFER_SIZE;

				if(writePtr == readPtr) {
					try {
						out.flush();
					} catch(IOException e) {
						disconnected = true;
						return;
					}
				}
			}
		}
	}