What's new? | Help | Directory | Sign in
Google
gears
Improving Your Web Browser
  
  
  
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2008, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import <cassert>

#import "gears/blob/blob_input_stream_sf.h"
#import "gears/blob/blob_interface.h"

@implementation BlobInputStream

#pragma mark Public Instance methods
//------------------------------------------------------------------------------
// BlobInputStream implementation
//------------------------------------------------------------------------------

- (id)initFromBlob:(BlobInterface *)blob {
self = [super init];
if (self) {
blob->Ref();
blob_ = blob;

[self setDelegate:self];

// We use a dummy input stream to handle all the various undocumented
// messages the system sends to an input stream.

// Contrary to documentation, inputStreamWithData neither copies nor
// retains the data in Mac OS X 10.4, so we must retain it.
dummy_data_ = [[NSData alloc] initWithBytes:"x" length:1];
dummy_stream_ = [[NSInputStream alloc] initWithData:dummy_data_];
}
return self;
}

- (void)dealloc {
if (blob_) {
blob_->Unref();
}
[dummy_stream_ release];
[dummy_data_ release];
[super dealloc];
}

- (void)reset:(BlobInterface *)blob {
if (blob) {
blob->Ref();
}
if (blob_) {
blob_->Unref();
}
blob_ = blob;
offset_ = 0;

// Create a new dummy stream, in case the old one is in the fully-read state.
if (!dummy_data_) {
dummy_data_ = [[NSData alloc] initWithBytes:"x" length:1];
}
[dummy_stream_ release];
dummy_stream_ = [[NSInputStream alloc] initWithData:dummy_data_];
}

#pragma mark NSInputStream function overrides.
//------------------------------------------------------------------------------
// NSInputStream implementation
//------------------------------------------------------------------------------

- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
if (blob_ == NULL) {
return 0;
}
int64 numBytesRead = blob_->Read(buffer, offset_, len);
assert(numBytesRead >= 0);
if (numBytesRead < 0) {
return 0;
}
// Confirm that BlobInterface::Read returned <= bytes requested
assert(numBytesRead <= static_cast<int64>(len));
offset_ += numBytesRead;
if (numBytesRead == 0) {
// We are at the end our our stream, so we read all of the data on our
// dummy input stream to make sure it is in the "fully read" state.
uint8_t buffer[2];
(void) [dummy_stream_ read:buffer maxLength:sizeof(buffer)];
}
return static_cast<NSInteger>(numBytesRead);
}

- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
// From the NSInputStream documentation:
// "If this method is not appropriate for your type of stream,
// you may return NO."
return NO;
}

- (BOOL)hasBytesAvailable {
#if 1
// There appears to be a bug in OSX 10.4 that requires us to always return
// YES, otherwise the NSURLConnection may not completely read the stream.
return YES;
#else
if (blob_ == NULL) {
return NO;
}
int64 remaining = blob_->Length() - offset_;
if (remaining <= 0) {
return NO;
}
return YES;
#endif
}

#pragma mark -
// Pass other expected messages on to the dummy input stream.

- (void)open {
[dummy_stream_ open];
}

- (void)close {
[dummy_stream_ close];
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
if (delegate_ != self) {
[delegate_ stream:self handleEvent:streamEvent];
}
}

- (id)delegate {
return delegate_;
}

- (void)setDelegate:(id)delegate {
if (delegate == nil) {
delegate_ = self;
[dummy_stream_ setDelegate:nil];
} else {
delegate_ = delegate;
[dummy_stream_ setDelegate:self];
}
}

- (id)propertyForKey:(NSString *)key {
return [dummy_stream_ propertyForKey:key];
}

- (BOOL)setProperty:(id)property forKey:(NSString *)key {
return [dummy_stream_ setProperty:property forKey:key];
}

- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
[dummy_stream_ scheduleInRunLoop:aRunLoop forMode:mode];
}

- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
[dummy_stream_ removeFromRunLoop:aRunLoop forMode:mode];
}

- (NSStreamStatus)streamStatus {
return [dummy_stream_ streamStatus];
}
- (NSError *)streamError {
return [dummy_stream_ streamError];
}

#pragma mark -

// We'll forward all unexpected messages to our dummy stream

+ (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
return [NSInputStream methodSignatureForSelector:selector];
}

+ (void)forwardInvocation:(NSInvocation*)invocation {
[invocation invokeWithTarget:[NSInputStream class]];
}

- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
return [dummy_stream_ methodSignatureForSelector:selector];
}

- (void)forwardInvocation:(NSInvocation*)invocation {
[invocation invokeWithTarget:dummy_stream_];
}

@end
Show details Hide details

Change log

r2364 by gears.daemon on Jul 15, 2008   Diff
[Author: playmobil]

* #include -> #import in all obj-c files.
* Added include guards to all obj-c
headers.
 * Removed a few stale Safari files.

PRESUBMIT=passed
R=dimich
CC=gears-eng@googlegroups.com
DELTA=339  (66 added, 171 deleted, 102
changed)
...
Go to: 
Project members, sign in to write a code review

Older revisions

r2066 by gears.daemon on Jun 23, 2008   Diff
[Author: bgarcia]

Replace all HttpRequest::Send*
functions with a single
Send(BlobInterface *) function.
...
r2012 by gears.daemon on Jun 18, 2008   Diff
[Author: bgarcia]

Fix BlobInputStream on Safari.
Activate all blob testing for Safari.

...
r1936 by gears.daemon on Jun 11, 2008   Diff
[Author: fry]

make blob support official!

PRESUBMIT=passed
...
All revisions of this file

File info

Size: 6230 bytes, 208 lines