yangsu@google.com | 1a2b4c1 | 2011-06-24 17:20:50 +0000 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2010, Tetrark Inc. |
| 3 | */ |
| 4 | |
| 5 | #include "SkStream_NSData.h" |
| 6 | |
| 7 | NSData* NSData_dataWithStream(SkStream* stream) { |
| 8 | size_t length = stream->getLength(); |
| 9 | void* src = malloc(length); |
| 10 | size_t bytes = stream->read(src, length); |
| 11 | SkASSERT(bytes == length); |
| 12 | return [NSData dataWithBytesNoCopy:src length:length freeWhenDone:YES]; |
| 13 | } |
| 14 | |
| 15 | NSData* NSData_dataFromResource(const char cname[], const char csuffix[]) { |
| 16 | NSBundle* bundle = [NSBundle mainBundle]; |
| 17 | NSString* name = [NSString stringWithUTF8String:cname]; |
| 18 | NSString* suffix = [NSString stringWithUTF8String:csuffix]; |
| 19 | NSString* path = [bundle pathForResource:name ofType:suffix]; |
| 20 | return [NSData dataWithContentsOfMappedFile:path]; |
| 21 | } |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | |
| 25 | SkStream_NSData::SkStream_NSData(NSData* data) { |
| 26 | fNSData = data; |
| 27 | [fNSData retain]; |
| 28 | |
| 29 | this->setMemory([fNSData bytes], [fNSData length], false); |
| 30 | } |
| 31 | |
| 32 | SkStream_NSData::~SkStream_NSData() { |
| 33 | [fNSData release]; |
| 34 | } |
| 35 | |
| 36 | SkStream_NSData* SkStream_NSData::CreateFromResource(const char name[], |
| 37 | const char suffix[]) { |
| 38 | NSData* data = NSData_dataFromResource(name, suffix); |
| 39 | return SkNEW_ARGS(SkStream_NSData, (data)); |
| 40 | } |
| 41 | |