forked from karelia/KSWriter
/
KSMutableDataWriter.m
61 lines (48 loc) · 1.65 KB
/
KSMutableDataWriter.m
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
//
// KSMutableDataWriter.m
// Sandvox
//
// Created by Mike on 12/03/2011.
// Copyright 2011 Karelia Software. All rights reserved.
//
#import "KSMutableDataWriter.h"
@implementation KSMutableDataWriter
- (id)initWithMutableData:(NSMutableData *)data encoding:(NSStringEncoding)encoding;
{
[self init];
_data = [data retain];
_encoding = encoding;
return self;
}
@synthesize encoding = _encoding;
- (void)writeString:(NSString *)string;
{
CFRange range = CFRangeMake(0, CFStringGetLength((CFStringRef)string));
CFStringEncoding encoding = CFStringConvertNSStringEncodingToEncoding([self encoding]);
CFIndex usedBufLen;
CFIndex chars = CFStringGetBytes((CFStringRef)string,
range,
encoding,
0,
NO,
NULL,
0,
&usedBufLen);
NSAssert(chars == [string length], @"Unexpected number of characters converted");
[_data increaseLengthBy:usedBufLen];
UInt8 *buffer = [_data mutableBytes];
chars = CFStringGetBytes((CFStringRef)string,
range,
encoding,
0,
NO,
buffer + [_data length] - usedBufLen,
usedBufLen,
NULL);
NSAssert(chars == [string length], @"Unexpected number of characters converted");
}
- (void)close;
{
[_data release];
}
@end