2006-02-15 17:34:47 +00:00
|
|
|
/** Implementation for NSStream for GNUStep
|
|
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Derek Zhou <derekzhou@gmail.com>
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Date: 2006
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2006-02-15 17:34:47 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-02-15 17:34:47 +00:00
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
*/
|
2010-02-19 08:12:46 +00:00
|
|
|
|
2014-06-18 19:33:26 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#if defined(HAVE_FCNTL_H)
|
|
|
|
# include <fcntl.h>
|
|
|
|
#elif defined(HAVE_SYS_FCNTL_H)
|
|
|
|
# include <sys/fcntl.h>
|
|
|
|
#endif
|
2010-02-19 08:12:46 +00:00
|
|
|
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "Foundation/NSData.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSDictionary.h"
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
#import "Foundation/NSRunLoop.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "Foundation/NSError.h"
|
|
|
|
#import "Foundation/NSValue.h"
|
|
|
|
#import "Foundation/NSHost.h"
|
|
|
|
#import "Foundation/NSByteOrder.h"
|
2017-03-27 09:44:13 +00:00
|
|
|
#import "Foundation/NSURL.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "GNUstepBase/NSObject+GNUstepBase.h"
|
|
|
|
|
|
|
|
#import "../GSPrivate.h"
|
|
|
|
#import "../GSStream.h"
|
|
|
|
#import "../GSSocketStream.h"
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete subclass of NSInputStream that reads from a file
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSFileInputStream : GSInputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
@private
|
|
|
|
NSString *_path;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalInputStream : GSSocketInputStream
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete subclass of NSOutputStream that writes to a file
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSFileOutputStream : GSOutputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
@private
|
|
|
|
NSString *_path;
|
|
|
|
BOOL _shouldAppend;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalOutputStream : GSSocketOutputStream
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalServerStream : GSSocketServerStream
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSFileInputStream
|
|
|
|
|
|
|
|
- (id) initWithFileAtPath: (NSString *)path
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
ASSIGN(_path, path);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
DESTROY(_path);
|
2006-02-15 17:34:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSInteger) read: (uint8_t *)buffer maxLength: (NSUInteger)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
int readLen;
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
if (buffer == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"null pointer for buffer"];
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"zero byte read write requested"];
|
|
|
|
}
|
|
|
|
|
2006-08-10 09:15:30 +00:00
|
|
|
_events &= ~NSStreamEventHasBytesAvailable;
|
2006-08-11 13:27:10 +00:00
|
|
|
|
|
|
|
if ([self streamStatus] == NSStreamStatusClosed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
readLen = read((intptr_t)_loopID, buffer, len);
|
2006-02-16 19:19:30 +00:00
|
|
|
if (readLen < 0 && errno != EAGAIN && errno != EINTR)
|
2007-03-16 17:54:16 +00:00
|
|
|
{
|
|
|
|
[self _recordError];
|
|
|
|
readLen = -1;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
else if (readLen == 0)
|
2007-03-16 17:54:16 +00:00
|
|
|
{
|
|
|
|
[self _setStatus: NSStreamStatusAtEnd];
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
return readLen;
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (BOOL) getBuffer: (uint8_t **)buffer length: (NSUInteger *)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasBytesAvailable
|
|
|
|
{
|
|
|
|
if ([self _isOpened] && [self streamStatus] != NSStreamStatusAtEnd)
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
|
|
|
{
|
|
|
|
off_t offset = 0;
|
|
|
|
|
|
|
|
if ([self _isOpened])
|
2006-03-21 15:33:05 +00:00
|
|
|
offset = lseek((intptr_t)_loopID, 0, SEEK_CUR);
|
2006-02-15 17:34:47 +00:00
|
|
|
return [NSNumber numberWithLong: offset];
|
|
|
|
}
|
|
|
|
return [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open([_path fileSystemRepresentation], O_RDONLY|O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
[self _recordError];
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
_loopID = (void*)(intptr_t)fd;
|
2006-02-15 17:34:47 +00:00
|
|
|
[super open];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
int closeReturn = close((intptr_t)_loopID);
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
if (closeReturn < 0)
|
|
|
|
[self _recordError];
|
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
|
2006-08-08 16:23:46 +00:00
|
|
|
- (void) _dispatch
|
|
|
|
{
|
|
|
|
if ([self streamStatus] == NSStreamStatusOpen)
|
|
|
|
{
|
|
|
|
[self _sendEvent: NSStreamEventHasBytesAvailable];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-09 08:28:27 +00:00
|
|
|
NSLog(@"_dispatch with unexpected status %"PRIuPTR, [self streamStatus]);
|
2006-08-08 16:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
@implementation GSLocalInputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
2008-01-09 16:11:10 +00:00
|
|
|
if ([self _setSocketAddress: addr port: 0 family: AF_UNIX] == NO)
|
2008-01-04 06:59:49 +00:00
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSFileOutputStream
|
|
|
|
|
|
|
|
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
ASSIGN(_path, path);
|
2006-02-15 17:34:47 +00:00
|
|
|
// so that unopened access will fail
|
2008-01-04 06:59:49 +00:00
|
|
|
_shouldAppend = shouldAppend;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
2008-01-04 06:59:49 +00:00
|
|
|
RELEASE(_path);
|
2006-02-15 17:34:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSInteger) write: (const uint8_t *)buffer maxLength: (NSUInteger)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
int writeLen;
|
2006-08-08 13:31:50 +00:00
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
if (buffer == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"null pointer for buffer"];
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"zero byte length write requested"];
|
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
_events &= ~NSStreamEventHasSpaceAvailable;
|
2007-05-11 08:26:59 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
if ([self streamStatus] == NSStreamStatusClosed)
|
2007-05-11 08:26:59 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
return 0;
|
2007-05-11 08:26:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
writeLen = write((intptr_t)_loopID, buffer, len);
|
|
|
|
if (writeLen < 0 && errno != EAGAIN && errno != EINTR)
|
|
|
|
[self _recordError];
|
|
|
|
return writeLen;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (BOOL) hasSpaceAvailable
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ([self _isOpened])
|
|
|
|
return YES;
|
|
|
|
return NO;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (void) open
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
int fd;
|
|
|
|
int flag = O_WRONLY | O_NONBLOCK | O_CREAT;
|
|
|
|
mode_t mode = 0666;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
if (_shouldAppend)
|
|
|
|
flag = flag | O_APPEND;
|
|
|
|
else
|
|
|
|
flag = flag | O_TRUNC;
|
|
|
|
fd = open([_path fileSystemRepresentation], flag, mode);
|
|
|
|
if (fd < 0)
|
|
|
|
{ // make an error
|
|
|
|
[self _recordError];
|
|
|
|
return;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
2008-01-04 06:59:49 +00:00
|
|
|
_loopID = (void*)(intptr_t)fd;
|
|
|
|
[super open];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
2007-05-11 08:26:59 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (void) close
|
2007-05-11 08:26:59 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
int closeReturn = close((intptr_t)_loopID);
|
|
|
|
if (closeReturn < 0)
|
|
|
|
[self _recordError];
|
|
|
|
[super close];
|
|
|
|
}
|
2007-05-11 08:26:59 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
2007-05-11 08:26:59 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
off_t offset = 0;
|
|
|
|
|
|
|
|
if ([self _isOpened])
|
|
|
|
offset = lseek((intptr_t)_loopID, 0, SEEK_CUR);
|
|
|
|
return [NSNumber numberWithLong: offset];
|
2007-05-11 08:26:59 +00:00
|
|
|
}
|
2008-01-04 06:59:49 +00:00
|
|
|
return [super propertyForKey: key];
|
2007-05-11 08:26:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (void) _dispatch
|
2006-02-27 09:35:19 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ([self streamStatus] == NSStreamStatusOpen)
|
|
|
|
{
|
|
|
|
[self _sendEvent: NSStreamEventHasSpaceAvailable];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-09 08:28:27 +00:00
|
|
|
NSLog(@"_dispatch with unexpected status %"PRIuPTR, [self streamStatus]);
|
2008-01-04 06:59:49 +00:00
|
|
|
}
|
2006-02-27 09:35:19 +00:00
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
@implementation GSLocalOutputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
2008-01-09 16:11:10 +00:00
|
|
|
if ([self _setSocketAddress: addr port: 0 family: AF_UNIX] == NO)
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSStream
|
|
|
|
|
|
|
|
+ (void) getStreamsToHost: (NSHost *)host
|
2009-02-23 20:42:32 +00:00
|
|
|
port: (NSInteger)port
|
2006-02-15 17:34:47 +00:00
|
|
|
inputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
2007-05-11 15:47:06 +00:00
|
|
|
NSString *address = host ? (id)[host address] : (id)@"127.0.0.1";
|
2011-07-24 13:09:22 +00:00
|
|
|
id ins = nil;
|
|
|
|
id outs = nil;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
// try ipv4 first
|
|
|
|
ins = AUTORELEASE([[GSInetInputStream alloc]
|
2006-02-16 19:19:30 +00:00
|
|
|
initToAddr: address port: port]);
|
2006-02-15 17:34:47 +00:00
|
|
|
outs = AUTORELEASE([[GSInetOutputStream alloc]
|
2006-02-16 19:19:30 +00:00
|
|
|
initToAddr: address port: port]);
|
2006-02-15 17:34:47 +00:00
|
|
|
if (!ins)
|
|
|
|
{
|
2006-02-28 12:42:51 +00:00
|
|
|
#if defined(PF_INET6)
|
2007-05-11 15:47:06 +00:00
|
|
|
ins = AUTORELEASE([[GSInet6InputStream alloc]
|
|
|
|
initToAddr: address port: port]);
|
|
|
|
outs = AUTORELEASE([[GSInet6OutputStream alloc]
|
|
|
|
initToAddr: address port: port]);
|
2006-02-28 12:42:51 +00:00
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
if (inputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[ins _setSibling: outs];
|
|
|
|
*inputStream = (NSInputStream*)ins;
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[outs _setSibling: ins];
|
|
|
|
*outputStream = (NSOutputStream*)outs;
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) getLocalStreamsToPath: (NSString *)path
|
|
|
|
inputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
2011-07-24 13:09:22 +00:00
|
|
|
id ins = nil;
|
|
|
|
id outs = nil;
|
2006-02-16 19:19:30 +00:00
|
|
|
|
|
|
|
ins = AUTORELEASE([[GSLocalInputStream alloc] initToAddr: path]);
|
|
|
|
outs = AUTORELEASE([[GSLocalOutputStream alloc] initToAddr: path]);
|
2006-02-15 17:34:47 +00:00
|
|
|
if (inputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[ins _setSibling: outs];
|
|
|
|
*inputStream = (NSInputStream*)ins;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[outs _setSibling: ins];
|
|
|
|
*outputStream = (NSOutputStream*)outs;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
+ (void) pipeWithInputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
2011-07-24 13:09:22 +00:00
|
|
|
id ins = nil;
|
|
|
|
id outs = nil;
|
2006-02-16 19:19:30 +00:00
|
|
|
int fds[2];
|
|
|
|
int pipeReturn;
|
|
|
|
|
|
|
|
// the type of the stream does not matter, since we are only using the fd
|
|
|
|
ins = AUTORELEASE([GSLocalInputStream new]);
|
|
|
|
outs = AUTORELEASE([GSLocalOutputStream new]);
|
|
|
|
pipeReturn = pipe(fds);
|
|
|
|
|
|
|
|
NSAssert(pipeReturn >= 0, @"Cannot open pipe");
|
2006-03-21 15:33:05 +00:00
|
|
|
[ins _setLoopID: (void*)(intptr_t)fds[0]];
|
|
|
|
[outs _setLoopID: (void*)(intptr_t)fds[1]];
|
2006-02-16 19:19:30 +00:00
|
|
|
// no need to connect
|
2008-01-04 06:59:49 +00:00
|
|
|
[ins _setPassive: YES];
|
|
|
|
[outs _setPassive: YES];
|
2006-02-16 19:19:30 +00:00
|
|
|
if (inputStream)
|
2008-01-04 06:59:49 +00:00
|
|
|
*inputStream = (NSInputStream*)ins;
|
2006-02-16 19:19:30 +00:00
|
|
|
if (outputStream)
|
2008-01-04 06:59:49 +00:00
|
|
|
*outputStream = (NSOutputStream*)outs;
|
2006-02-16 19:19:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDelegate: (id)delegate
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) delegate
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) setProperty: (id)property forKey: (NSString *)key
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode;
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSError *) streamError
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSStreamStatus) streamStatus
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSInputStream
|
|
|
|
|
|
|
|
+ (id) inputStreamWithData: (NSData *)data
|
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
return AUTORELEASE([[GSDataInputStream alloc] initWithData: data]);
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) inputStreamWithFileAtPath: (NSString *)path
|
|
|
|
{
|
|
|
|
return AUTORELEASE([[GSFileInputStream alloc] initWithFileAtPath: path]);
|
|
|
|
}
|
|
|
|
|
2017-03-27 09:44:13 +00:00
|
|
|
+ (id) inputStreamWithURL: (NSURL *)url
|
|
|
|
{
|
|
|
|
if ([url isFileURL])
|
|
|
|
{
|
|
|
|
return [self inputStreamWithFileAtPath: [url path]];
|
|
|
|
}
|
|
|
|
return [self inputStreamWithData: [url resourceDataUsingCache: YES]];
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (BOOL) getBuffer: (uint8_t **)buffer length: (NSUInteger *)len
|
2006-08-11 13:27:10 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasBytesAvailable
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
- (id) initWithData: (NSData *)data
|
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2006-08-11 13:27:10 +00:00
|
|
|
return [[GSDataInputStream alloc] initWithData: data];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithFileAtPath: (NSString *)path
|
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2006-02-15 17:34:47 +00:00
|
|
|
return [[GSFileInputStream alloc] initWithFileAtPath: path];
|
|
|
|
}
|
|
|
|
|
2017-03-27 09:44:13 +00:00
|
|
|
- (id) initWithURL: (NSURL *)url
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
if ([url isFileURL])
|
|
|
|
{
|
|
|
|
return [[GSFileInputStream alloc] initWithFileAtPath: [url path]];
|
|
|
|
}
|
|
|
|
return [[GSDataInputStream alloc]
|
|
|
|
initWithData: [url resourceDataUsingCache: YES]];
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSInteger) read: (uint8_t *)buffer maxLength: (NSUInteger)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSOutputStream
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
+ (id) outputStreamToBuffer: (uint8_t *)buffer capacity: (NSUInteger)capacity
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
return AUTORELEASE([[GSBufferOutputStream alloc]
|
2006-02-15 17:34:47 +00:00
|
|
|
initToBuffer: buffer capacity: capacity]);
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) outputStreamToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
|
|
|
{
|
|
|
|
return AUTORELEASE([[GSFileOutputStream alloc]
|
|
|
|
initToFileAtPath: path append: shouldAppend]);
|
|
|
|
}
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
+ (id) outputStreamToMemory
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
return AUTORELEASE([[GSDataOutputStream alloc] init]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasSpaceAvailable
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (id) initToBuffer: (uint8_t *)buffer capacity: (NSUInteger)capacity
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2006-08-11 13:27:10 +00:00
|
|
|
return [[GSBufferOutputStream alloc] initToBuffer: buffer capacity: capacity];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2006-02-15 17:34:47 +00:00
|
|
|
return [[GSFileOutputStream alloc] initToFileAtPath: path
|
|
|
|
append: shouldAppend];
|
|
|
|
}
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
- (id) initToMemory
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2006-08-11 13:27:10 +00:00
|
|
|
return [[GSDataOutputStream alloc] init];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSInteger) write: (const uint8_t *)buffer maxLength: (NSUInteger)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
2006-08-11 13:27:10 +00:00
|
|
|
return -1;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
@implementation GSLocalServerStream
|
|
|
|
|
|
|
|
- (Class) _inputStreamClass
|
|
|
|
{
|
|
|
|
return [GSLocalInputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) _outputStreamClass
|
|
|
|
{
|
|
|
|
return [GSLocalOutputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ((self = [super init]) != nil)
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
2008-01-09 16:11:10 +00:00
|
|
|
if ([self _setSocketAddress: addr port: 0 family: AF_UNIX] == NO)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
DESTROY(self);
|
|
|
|
}
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|