views:

440

answers:

1
+2  A: 

The problem is that the data keeps writing to the oSream again and again. How to stop it when all the data is sent?

See, this is the danger of thinking in the passive voice. Reword the question in the active voice:

The problem is that the data keeps writing to the oSream again and again. How to stop it when I have sent all the data?

And you have almost answered your own question.

A large part of the problem is your HasSpaceAvailable handler. Here's what you're doing:

  1. Creating a complete JPEG representation of the image (every time)
  2. Sending the length of the representation (every time)
  3. Sending the first portion of the representation (every time)

These are three problems and you need to solve all three.

  1. Create the JPEG representation once.
  2. Send the length of the representation only once.
  3. Keep track of what you have already sent and only send the first part of what you haven't.

To do all three, put the length into an NSMutableData and append the JPEG representation to it. Then, retain this new data object, and have additional instance variables for the bytes pointer and the length remaining. You'll do all this before you get to the HasSpaceAvailable state.

Then, when you do get to HasSpaceAvailable, don't separately send the length, because you've put it into the data object. You need only send the bytes from your pointer instance variable with the length remaining as the max length. After you check that the number of bytes sent was not 0 or -1 (see the documentation), add it to the pointer and subtract it from the length remaining.

When the length remaining reaches zero, you have sent everything. Assuming you don't want to follow the JPEG data up with something, you can then close the stream.

Peter Hosey