1

Currently I'm trying to use a NSMutableURLRequest, setting the HTTPBody to a custom written NSInputStream of mine that provides this chunks.

This would be fine if it were not for the fact that NSMutableURLRequest keeps asking me to implement more and more methods in my NSInputStream class. First it asked me to implement - streamStatus: and that was rather straightforward to implement however now it's asking for _scheduleInCFRunLoop:forMode:...

Basically I'm generating data out of an algorithm and would like to send it to the server via chunked request.

Here's the code:

@interface GERHTTPChunkInputStream : NSInputStream
{
  uint8_t counter_;
}

- (GERHTTPChunkInputStream *)init;
- (void)dealloc;
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len;
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len;
- (BOOL)hasBytesAvailable;
- (NSStreamStatus)streamStatus;

@end

@implementation GERHTTPChunkInputStream

- (GERHTTPChunkInputStream *)init {
  [super init];
  return self;
}

- (void)dealloc {
  assert(NO);
  [super dealloc];
}

- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
  NSLog(@"Getting more bytes!!!");
  for (int i = 0; i < len; ++i) {
    buffer[i] = ++counter_;
  }
  return len;
}

- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
  return NO;
}

- (BOOL)hasBytesAvailable {
  return YES;
}

- (NSStreamStatus)streamStatus {
  return NSStreamStatusNotOpen;
}

@end
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
rui
  • 11,015
  • 7
  • 46
  • 64
  • _NSInputStream_ implements both mentioned methods. Is your stream class really a subclass of _NSInputStream_? It sounds as if you have subclass from _NSStream_ instead of _NSInputStream_. – Codo Nov 05 '10 at 19:10
  • Definitely inheriting from NSInputStream. The - streamStatus: method already existed but when called it said that NSStream's implementation was abstract. The other one doesn't seem to be implemented. It seem like NSUrlRequest excepts a very specific internal NSInputStream. :( – rui Nov 05 '10 at 19:58
  • I have the same need to programatically create an input stream. its proving to be difficult. – nont Nov 22 '10 at 18:51

1 Answers1

3

According to a few discussions on the net, it is difficult to subclass NSInputStream. Have a look at Multipart POST using NSInputStream or NSInputStream subclass asynchronous.

It seems that you indeed need to implement these strange private methods...

Codo
  • 75,595
  • 17
  • 168
  • 206
  • Unbelievable that three years later, we still can't sublass NSInputStream. I guess its just not a priority to Apple? – nont Nov 22 '10 at 19:26