Fix compile errors
[pithos-ios] / Classes / ASIInputStream.m
1 //
2 //  ASIInputStream.m
3 //  Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
4 //
5 //  Created by Ben Copsey on 10/08/2009.
6 //  Copyright 2009 All-Seeing Interactive. All rights reserved.
7 //
8
9 #import "ASIInputStream.h"
10 #import "ASIHTTPRequest.h"
11
12 // Used to ensure only one request can read data at once
13 static NSLock *readLock = nil;
14
15 @implementation ASIInputStream
16
17 + (void)initialize
18 {
19         if (self == [ASIInputStream class]) {
20                 readLock = [[NSLock alloc] init];
21         }
22 }
23
24 + (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)theRequest
25 {
26         ASIInputStream *theStream = [[[self alloc] init] autorelease];
27         [theStream setRequest:theRequest];
28         [theStream setStream:[NSInputStream inputStreamWithFileAtPath:path]];
29         return theStream;
30 }
31
32 + (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)theRequest
33 {
34         ASIInputStream *theStream = [[[self alloc] init] autorelease];
35         [theStream setRequest:theRequest];
36         [theStream setStream:[NSInputStream inputStreamWithData:data]];
37         return theStream;
38 }
39
40 - (void)dealloc
41 {
42         [stream release];
43         [super dealloc];
44 }
45
46 // Called when CFNetwork wants to read more of our request body
47 // When throttling is on, we ask ASIHTTPRequest for the maximum amount of data we can read
48 - (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len
49 {
50         [readLock lock];
51         unsigned long toRead = len;
52         if ([ASIHTTPRequest isBandwidthThrottled]) {
53                 toRead = [ASIHTTPRequest maxUploadReadLength];
54                 if (toRead > len) {
55                         toRead = len;
56                 } else if (toRead == 0) {
57                         toRead = 1;
58                 }
59                 [request performThrottling];
60         }
61         [ASIHTTPRequest incrementBandwidthUsedInLastSecond:toRead];
62         [readLock unlock];
63         return [stream read:buffer maxLength:toRead];
64 }
65
66 /*
67  * Implement NSInputStream mandatory methods to make sure they are implemented
68  * (necessary for MacRuby for example) and avoid the overhead of method
69  * forwarding for these common methods.
70  */
71 - (void)open
72 {
73     [stream open];
74 }
75
76 - (void)close
77 {
78     [stream close];
79 }
80
81 - (id)delegate
82 {
83     return [stream delegate];
84 }
85
86 - (void)setDelegate:(id)delegate
87 {
88     [stream setDelegate:delegate];
89 }
90
91 - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode
92 {
93     [stream scheduleInRunLoop:aRunLoop forMode:mode];
94 }
95
96 - (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode
97 {
98     [stream removeFromRunLoop:aRunLoop forMode:mode];
99 }
100
101 - (id)propertyForKey:(NSString *)key
102 {
103     return [stream propertyForKey:key];
104 }
105
106 - (BOOL)setProperty:(id)property forKey:(NSString *)key
107 {
108     return [stream setProperty:property forKey:key];
109 }
110
111 - (NSStreamStatus)streamStatus
112 {
113     return [stream streamStatus];
114 }
115
116 - (NSError *)streamError
117 {
118     return [stream streamError];
119 }
120
121 // If we get asked to perform a method we don't have (probably internal ones),
122 // we'll just forward the message to our stream
123
124 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
125 {
126         return [stream methodSignatureForSelector:aSelector];
127 }
128          
129 - (void)forwardInvocation:(NSInvocation *)anInvocation
130 {
131         [anInvocation invokeWithTarget:stream];
132 }
133
134 @synthesize stream;
135 @synthesize request;
136 @end