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