Showing posts with label initWithBytesNoCopy. Show all posts
Showing posts with label initWithBytesNoCopy. Show all posts

Saturday, June 1, 2013

NSData initWithBytesNoCopy example in Objective C (iOS).


NSData initWithBytesNoCopy

Returns a data object initialized by adding to it a given number of bytes of data from a given buffer.

- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length

Parameters of [NSData initWithBytesNoCopy]
bytes
A buffer containing data for the new object. bytes must point to a memory block allocated with malloc.
length
The number of bytes to hold from bytes. This value must not exceed the length of bytes.

Return Value
A data object initialized by adding to it length bytes of data from the buffer bytes. The returned object might be different than the original receiver.

Discussion of [NSData initWithBytesNoCopy]
The returned object takes ownership of the bytes pointer and frees it on deallocation. Therefore, bytes must point to a memory block allocated with malloc.

NSData initWithBytesNoCopy example.
int offset = 10;
NSRange dataRange;
dataRange.length = [data length] - offset;
dataRange.location = offset;

void *buffer = malloc(dataRange.length);
[data getBytes:buffer range:dataRange];
self.bodyData = [[NSData alloc] initWithBytesNoCopy:buffer length:dataRange.length];
free(buffer); //Make sure your property has either retain attribute (or strong if using ARC)

Example of [NSData initWithBytesNoCopy].
if (flags == nil) {
   flagPtr *flgH = &flags;
   NSData *flgsDatum =
      [[NSData alloc] initWithBytesNoCopy:flgH length:sizeof(flgH)
freeWhenDone:NO];
   [[NSNotificationCenter defaultCenter] postNotificationName:@"fetchFlags"
object:flgsDatum];
   *flags = (WORKING | (*flags & LOGFILE));
}

NSData initWithBytesNoCopy example.
- (NSString *)hashWithDigest:(AUMessageDigest)digest data:(NSData **)data
{
    u_byte *(*CC_XXX)(const void *, CC_LONG, u_byte *);
    CC_LONG CC_XXX_DIGEST_LEN;
    u_byte *md = NULL;
    NSInteger i = 0;
    NSString *result;
    NSMutableString *buffer;
   
    switch (digest) {
        case AUDigestSHA1:
            CC_XXX = CC_SHA1;
            CC_XXX_DIGEST_LEN = CC_SHA1_DIGEST_LENGTH;
        break;
        case AUDigestSHA256:
            CC_XXX = CC_SHA256;
            CC_XXX_DIGEST_LEN = CC_SHA256_DIGEST_LENGTH;
        break;
        case AUDigestSHA512:
            CC_XXX = CC_SHA512;
            CC_XXX_DIGEST_LEN = CC_SHA512_DIGEST_LENGTH;
        break;
        case AUDigestMD5:
            CC_XXX = CC_MD5;
            CC_XXX_DIGEST_LEN = CC_MD5_DIGEST_LENGTH;
        break;
        default:
            return nil;
    }
   
    result = nil;
    if((md = malloc(CC_XXX_DIGEST_LEN * sizeof(u_byte) + 1))) {
        memset(md, 0, CC_XXX_DIGEST_LEN + 1);
        CC_XXX([self bytes], [self length], md);
        buffer = [NSMutableString string];
        for (i = 0; i< CC_XXX_DIGEST_LEN; i++) {
            [buffer appendFormat:@"%02x", (CC_LONG)(md[i])];
        }
        if (data != nil) {
            *data = [[NSData alloc] initWithBytesNoCopy:md length:CC_XXX_DIGEST_LEN freeWhenDone:YES];
        } else {
            free(md);
        }
        result = [NSString stringWithString:buffer];
    }
    return result;
}

End of NSData initWithBytesNoCopy example article.

Tuesday, May 14, 2013

NSString initWithBytesNoCopy length encoding freeWhenDone example ios


[NSString initWithBytesNoCopy length encoding freeWhenDone]

Returns an initialized NSString object that contains a given number of bytes from a given buffer of bytes interpreted in a given encoding, and optionally frees the buffer.
- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)flag
Parameters of [NSString initWithBytesNoCopy length encoding freeWhenDone]
bytes
A buffer of bytes interpreted in the encoding specified by encoding.
length
The number of bytes to use from bytes.
encoding
The character encoding of bytes.
flag
If YES, the receiver frees the memory when it no longer needs the data; if NO it won’t.
Return Value of [NSString initWithBytesNoCopy length encoding freeWhenDone]
An initialized NSString object containing length bytes from bytes interpreted using the encoding encoding. The returned object may be different from the original receiver.
Special Considerations of [NSString initWithBytesNoCopy length encoding freeWhenDone]
If an error occurs during the creation of the string, then bytes is not freed even if flag is YES. In this case, the caller is responsible for freeing the buffer. This allows the caller to continue trying to create a string with the buffer, without having the buffer deallocated.
Example of [NSString initWithBytesNoCopy length encoding freeWhenDone]
NSString *veryLargeString = ...;
NSUInteger startingIndex = ...;
NSData *veryLargeStringData = [veryLargeString dataUsingEncoding:NSUTF8StringEncoding];

const void *bytes = [veryLargeStringData bytes];
const void *subBytes = bytes + startingIndex;
NSUInteger subLength = [veryLargeStringData length] - startingIndex;

NSString *substring = [[NSString alloc] initWithBytesNoCopy:subBytes length:subLength encoding:NSUTF8StringEncoding freeWhenDone:NO];
Example of [NSString initWithBytesNoCopy length encoding freeWhenDone]
char* block = malloc(200);
NSString* string = [[NSString alloc] initWithBytesNoCopy:length:encoding:freeWhenDone];
//use string
memset(block, 0, 200);// overwrite block with 0
[string release];
free(block);
Example of [NSString initWithBytesNoCopy length encoding freeWhenDone]
- (NSString *)base64Encoding;
{
if ([self length] == 0)
    return @"";

char *characters = malloc((([self length] + 2) / 3) * 4);
if (characters == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (i < [self length])
{
    char buffer[3] = {0,0,0};
    short bufferLength = 0;
    while (bufferLength < 3 && i < [self length])
        buffer[bufferLength++] = ((char *)[self bytes])[i++];

    //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
    characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
    characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
    if (bufferLength > 1)
        characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
    else characters[length++] = '=';
    if (bufferLength > 2)
        characters[length++] = encodingTable[buffer[2] & 0x3F];
    else characters[length++] = '=';    
}

return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}

@end

NSString initWithBytesNoCopy example ios


initWithBytesNoCopy: length: encoding: freeWhenDone:

Returns an initialized NSString object that contains a given number of bytes from a given buffer of bytes interpreted in a given encoding, and optionally frees the buffer.
- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)flag
Parameters of [NSString initWithBytesNoCopy]
bytes
A buffer of bytes interpreted in the encoding specified by encoding.
length
The number of bytes to use from bytes.
encoding
The character encoding of bytes.
flag
If YES, the receiver frees the memory when it no longer needs the data; if NO it won’t.
Return Value of [NSString initWithBytesNoCopy]
An initialized NSString object containing length bytes from bytes interpreted using the encoding encoding. The returned object may be different from the original receiver.
Special Considerations of [NSString initWithBytesNoCopy]
If an error occurs during the creation of the string, then bytes is not freed even if flag is YES. In this case, the caller is responsible for freeing the buffer. This allows the caller to continue trying to create a string with the buffer, without having the buffer deallocated.
Example of [NSString initWithBytesNoCopy]
NSString *veryLargeString = ...;
NSUInteger startingIndex = ...;
NSData *veryLargeStringData = [veryLargeString dataUsingEncoding:NSUTF8StringEncoding];

const void *bytes = [veryLargeStringData bytes];
const void *subBytes = bytes + startingIndex;
NSUInteger subLength = [veryLargeStringData length] - startingIndex;

NSString *substring = [[NSString alloc] initWithBytesNoCopy:subBytes length:subLength encoding:NSUTF8StringEncoding freeWhenDone:NO];
Example of [NSString initWithBytesNoCopy]
char* block = malloc(200);
NSString* string = [[NSString alloc] initWithBytesNoCopy:length:encoding:freeWhenDone];
//use string
memset(block, 0, 200);// overwrite block with 0
[string release];
free(block);
Example of [NSString initWithBytesNoCopy]
- (NSString *)base64Encoding;
{
if ([self length] == 0)
    return @"";

char *characters = malloc((([self length] + 2) / 3) * 4);
if (characters == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (i < [self length])
{
    char buffer[3] = {0,0,0};
    short bufferLength = 0;
    while (bufferLength < 3 && i < [self length])
        buffer[bufferLength++] = ((char *)[self bytes])[i++];

    //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
    characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
    characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
    if (bufferLength > 1)
        characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
    else characters[length++] = '=';
    if (bufferLength > 2)
        characters[length++] = encodingTable[buffer[2] & 0x3F];
    else characters[length++] = '=';    
}

return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}

@end