From 7acb526434d06f49998e7fe218c7ee384b005621 Mon Sep 17 00:00:00 2001 From: Andrew McGeachie Date: Wed, 10 Feb 2010 00:56:09 +0000 Subject: [PATCH] THRIFT-686. Adding TMemoryBuffer implementation to Cocoa git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@908301 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cocoa/src/transport/TMemoryBuffer.h | 28 ++++++++++++ lib/cocoa/src/transport/TMemoryBuffer.m | 59 +++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 lib/cocoa/src/transport/TMemoryBuffer.h create mode 100644 lib/cocoa/src/transport/TMemoryBuffer.m diff --git a/lib/cocoa/src/transport/TMemoryBuffer.h b/lib/cocoa/src/transport/TMemoryBuffer.h new file mode 100644 index 00000000..45a5e178 --- /dev/null +++ b/lib/cocoa/src/transport/TMemoryBuffer.h @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import +#import "TTransport.h" + +@interface TMemoryBuffer : NSObject { + NSMutableData *mBuffer; + NSUInteger mOffset; +} +- (id)initWithData:(NSData *)data; +@end diff --git a/lib/cocoa/src/transport/TMemoryBuffer.m b/lib/cocoa/src/transport/TMemoryBuffer.m new file mode 100644 index 00000000..7912bcd7 --- /dev/null +++ b/lib/cocoa/src/transport/TMemoryBuffer.m @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TMemoryBuffer.h" +#import "TTransportException.h" + +#define GARBAGE_BUFFER_SIZE 4096 // 4KiB + +@implementation TMemoryBuffer +- (id)initWithData:(NSData *)data { + if (self = [super init]) { + mBuffer = [data mutableCopy]; + mOffset = 0; + } + return self; +} + +- (int)readAll:(uint8_t *)buf offset:(int)off length:(int)len { + if ([mBuffer length] - mOffset < len) { + @throw [TTransportException exceptionWithReason:@"Not enough bytes remain in buffer"]; + } + [mBuffer getBytes:buf range:NSMakeRange(mOffset, len)]; + mOffset += len; + if (mOffset >= GARBAGE_BUFFER_SIZE) { + [mBuffer replaceBytesInRange:NSMakeRange(0, mOffset) withBytes:NULL length:0]; + mOffset = 0; + } + return len; +} + +- (void)write:(const uint8_t *)data offset:(unsigned int)offset length:(unsigned int)length { + [mBuffer appendBytes:data+offset length:length]; +} + +- (void)flush { + // noop +} + +- (void)dealloc { + [mBuffer release]; + [super dealloc]; +} +@end -- 2.17.1