2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGFilterStream.h"
25 @implementation NGFilterStream
27 + (id)filterWithInputSource:(id<NGInputStream>)_s {
28 return [[(NGFilterStream *)[self alloc] initWithInputSource:_s] autorelease];
30 + (id)filterWithOutputSource:(id<NGOutputStream>)_s {
31 return [[(NGFilterStream *)[self alloc] initWithOutputSource:_s] autorelease];
33 + (id)filterWithSource:(id<NGStream>)_s {
34 return [[(NGFilterStream *)[self alloc] initWithSource:_s] autorelease];
38 return [self initWithSource:nil];
41 - (id)initWithSource:(id<NGStream>)_source {
42 if ((self = [super init])) {
43 self->source = [_source retain];
45 if ([source isKindOfClass:[NSObject class]]) {
46 self->readBytes = (NGIOReadMethodType)
47 [(NSObject *)self->source methodForSelector:@selector(readBytes:count:)];
48 self->writeBytes = (NGIOWriteMethodType)
49 [(NSObject *)self->source methodForSelector:@selector(writeBytes:count:)];
55 - (id)initWithInputSource:(id<NGInputStream>)_source {
56 if ((self = [super init])) {
57 self->source = [_source retain];
59 if ([source isKindOfClass:[NSObject class]]) {
60 self->readBytes = (NGIOReadMethodType)
61 [(NSObject *)self->source methodForSelector:@selector(readBytes:count:)];
66 - (id)initWithOutputSource:(id<NGOutputStream>)_source {
67 if ((self = [super init])) {
68 self->source = [_source retain];
70 if ([source isKindOfClass:[NSObject class]]) {
71 self->writeBytes = (NGIOWriteMethodType)
72 [(NSObject *)self->source methodForSelector:@selector(writeBytes:count:)];
79 [self->source release];
80 self->readBytes = NULL;
81 self->writeBytes = NULL;
87 - (id<NGInputStream>)inputStream {
90 - (id<NGOutputStream>)outputStream {
93 - (id<NGStream>)source {
99 - (NSException *)lastException {
100 return [self->source lastException];
102 - (void)resetLastException {
103 [self->source resetLastException];
105 - (void)setLastException:(NSException *)_exception {
106 [self->source setLastException:_exception];
110 return [self->source isOpen];
113 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
115 return (unsigned)readBytes(self->source, _cmd, _buf, _len);
117 return [self->source readBytes:_buf count:_len];
119 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
120 if (self->writeBytes)
121 return (unsigned)writeBytes(self->source, _cmd, _buf, _len);
123 return [self->source writeBytes:_buf count:_len];
127 return [self->source flush];
130 return [((NGStream *)self->source) close];
133 - (NGStreamMode)mode {
134 return [(NGStream *)self->source mode];
136 - (BOOL)isRootStream {
140 // all other things are forward
142 - (void)forwardInvocation:(NSInvocation *)_invocation {
143 if ([self->source respondsToSelector:[_invocation selector]]) {
144 [_invocation setTarget:self->source];
145 [_invocation invoke];
148 [self doesNotRecognizeSelector:[_invocation selector]];
153 - (NSString *)description {
154 return [NSString stringWithFormat:
155 @"<%@[0x%08X] source=%@ mode=%@>",
156 NSStringFromClass([self class]), (unsigned)self,
157 self->source ? (id)self->source : (id)@"nil",
158 [self modeDescription]];
161 @end /* NGFilterStream */