Permalink
Browse files

Added working example.

  • Loading branch information...
1 parent 3403a60 commit 25aa1dc7eaac54f442df8718cb5f4456eece6fa0 @bjhomer committed Apr 15, 2011
Showing with 45 additions and 3 deletions.
  1. +4 −0 HSCountingInputStream.xcodeproj/project.pbxproj
  2. +41 −3 HSCountingInputStream/main.m
@@ -10,6 +10,7 @@
B7B206F713557E9500C1C6BA /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7B206F613557E9500C1C6BA /* Foundation.framework */; };
B7B206FA13557E9500C1C6BA /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B7B206F913557E9500C1C6BA /* main.m */; };
B7B2070513557ED300C1C6BA /* HSCountingInputStream.m in Sources */ = {isa = PBXBuildFile; fileRef = B7B2070413557ED300C1C6BA /* HSCountingInputStream.m */; };
+ B7B207091358024700C1C6BA /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7B207081358024700C1C6BA /* CoreServices.framework */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@@ -31,13 +32,15 @@
B7B206FC13557E9500C1C6BA /* HSCountingInputStream-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "HSCountingInputStream-Prefix.pch"; sourceTree = "<group>"; };
B7B2070313557ED300C1C6BA /* HSCountingInputStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HSCountingInputStream.h; sourceTree = "<group>"; };
B7B2070413557ED300C1C6BA /* HSCountingInputStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HSCountingInputStream.m; sourceTree = "<group>"; };
+ B7B207081358024700C1C6BA /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
B7B206EF13557E9500C1C6BA /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ B7B207091358024700C1C6BA /* CoreServices.framework in Frameworks */,
B7B206F713557E9500C1C6BA /* Foundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -65,6 +68,7 @@
B7B206F513557E9500C1C6BA /* Frameworks */ = {
isa = PBXGroup;
children = (
+ B7B207081358024700C1C6BA /* CoreServices.framework */,
B7B206F613557E9500C1C6BA /* Foundation.framework */,
);
name = Frameworks;
@@ -7,16 +7,54 @@
//
#import <Foundation/Foundation.h>
+#import "HSCountingInputStream.h"
+
+void downloadFile();
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
- // insert code here...
- NSLog(@"Hello, World!");
+
+ downloadFile();
[pool drain];
return 0;
}
+void downloadFile() {
+
+ // A note: I'm using CFReadStreamCreateForHTTPRequest() here because it's the simplest way I know of
+ // to use an input stream without requiring a web service running. This works just as well with an input
+ // stream used as the message of an HTTP request.
+
+ CFURLRef url = CFURLCreateWithString(NULL, CFSTR("http://bjhomer.blogspot.com"), NULL);
+ CFHTTPMessageRef message = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), url, kCFHTTPVersion1_1);
+
+ CFReadStreamRef readStream = CFReadStreamCreateForHTTPRequest(NULL, message);
+
+ HSCountingInputStream *countingStream = [[HSCountingInputStream alloc] initWithInputStream:(NSInputStream *)readStream];
+ countingStream.characterToCount = 'n';
+
+ CFReadStreamScheduleWithRunLoop((CFReadStreamRef)countingStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
+
+ CFReadStreamOpen(readStream);
+
+ uint8_t buffer[1024];
+ while (CFReadStreamGetStatus(readStream) != kCFStreamStatusAtEnd) {
+ [countingStream read:buffer maxLength:1024];
+ }
+
+ CFReadStreamClose(readStream);
+
+ CFReadStreamUnscheduleFromRunLoop((CFReadStreamRef)countingStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
+
+ NSLog(@"There were %u '%c's in bjhomer.blogspot.com's home page", (int)countingStream.countedCharacters, countingStream.characterToCount);
+
+ CFRelease(url);
+ CFRelease(message);
+ CFRelease(readStream);
+ [countingStream release];
+
+}
+

0 comments on commit 25aa1dc

Please sign in to comment.