ObjFW  Check-in [efe7be259d]

Overview
Comment:Rename -[OFStream pendingBytes].

It is now called -[numberOfBytesInReadBuffer].

Additionally, this commit renames OFStream's _cache to _readBuffer.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: efe7be259d41fd702bda9b2c1f8b8619c36f2dc98c7bb98ee8b84d3f7d7c249f
User & Date: js on 2013-02-18 21:53:56
Other Links: manifest | tags
Context
2013-02-18
22:00
Rename OFXMLParser's _cache to _buffer. check-in: 265a0928ca user: js tags: trunk
21:53
Rename -[OFStream pendingBytes]. check-in: efe7be259d user: js tags: trunk
12:27
Add -[OFMutableArray initWithCapacity:]. check-in: e0c9168dfc user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [bfff30b129] to [8b8198d6dd].

163
164
165
166
167
168
169
170
171
172

173
174
175
176
177
178
179
}

- (int)fileDescriptorForReading
{
	return [_socket fileDescriptorForReading];
}

- (size_t)pendingBytes
{
	return [super pendingBytes] + [_socket pendingBytes];

}

- (void)close
{
	[_socket close];
}
@end







|

|
>







163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
}

- (int)fileDescriptorForReading
{
	return [_socket fileDescriptorForReading];
}

- (size_t)numberOfBytesInReadBuffer
{
	return [super numberOfBytesInReadBuffer] +
	    [_socket numberOfBytesInReadBuffer];
}

- (void)close
{
	[_socket close];
}
@end

Modified src/OFSeekableStream.m from [b3e35f31cf] to [7b96bce1bd].

30
31
32
33
34
35
36
37
38
39
40
41

- (void)seekToOffset: (off_t)offset
	      whence: (int)whence
{
	[self lowlevelSeekToOffset: offset
			    whence: whence];

	[self freeMemory: _cache];
	_cache = NULL;
	_cacheLength = 0;
}
@end







|
|
|


30
31
32
33
34
35
36
37
38
39
40
41

- (void)seekToOffset: (off_t)offset
	      whence: (int)whence
{
	[self lowlevelSeekToOffset: offset
			    whence: whence];

	[self freeMemory: _readBuffer];
	_readBuffer = NULL;
	_readBufferLength = 0;
}
@end

Modified src/OFStream.h from [57e418e361] to [74effa40b4].

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 *	 the methods that do the actual work. OFStream uses those for all other
 *	 methods and does all the caching and other stuff for you. If you
 *	 override these methods without the lowlevel prefix, you *will* break
 *	 caching and get broken results!
 */
@interface OFStream: OFObject <OFCopying>
{
	char   *_cache;
	char   *_writeBuffer;
	size_t _cacheLength, _writeBufferLength;
	BOOL   _writeBufferEnabled;
	BOOL   _blocking, _waitingForDelimiter;
}

#ifdef OF_HAVE_PROPERTIES
@property (getter=isWriteBufferEnabled) BOOL writeBufferEnabled;
@property (getter=isBlocking) BOOL blocking;
@property (readonly, getter=isAtEndOfStream) BOOL atEndOfStream;
#endif







<
|
|
<
|







53
54
55
56
57
58
59

60
61

62
63
64
65
66
67
68
69
 *	 the methods that do the actual work. OFStream uses those for all other
 *	 methods and does all the caching and other stuff for you. If you
 *	 override these methods without the lowlevel prefix, you *will* break
 *	 caching and get broken results!
 */
@interface OFStream: OFObject <OFCopying>
{

	char *_readBuffer, *_writeBuffer;
	size_t _readBufferLength, _writeBufferLength;

	BOOL _writeBufferEnabled, _blocking, _waitingForDelimiter;
}

#ifdef OF_HAVE_PROPERTIES
@property (getter=isWriteBufferEnabled) BOOL writeBufferEnabled;
@property (getter=isBlocking) BOOL blocking;
@property (readonly, getter=isAtEndOfStream) BOOL atEndOfStream;
#endif
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
 * @param arguments The arguments used in the format string
 * @return The number of bytes written
 */
- (size_t)writeFormat: (OFConstantString*)format
	    arguments: (va_list)arguments;

/*!
 * @brief Returns the number of bytes still present in the internal read cache.
 *
 * @return The number of bytes still present in the internal read cache.
 */
- (size_t)pendingBytes;

/*!
 * @brief Returns whether the stream is in blocking mode.
 *
 * @return Whether the stream is in blocking mode
 */
- (BOOL)isBlocking;







|

|

|







978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
 * @param arguments The arguments used in the format string
 * @return The number of bytes written
 */
- (size_t)writeFormat: (OFConstantString*)format
	    arguments: (va_list)arguments;

/*!
 * @brief Returns the number of bytes still present in the internal read buffer.
 *
 * @return The number of bytes still present in the internal read buffer.
 */
- (size_t)numberOfBytesInReadBuffer;

/*!
 * @brief Returns whether the stream is in blocking mode.
 *
 * @return Whether the stream is in blocking mode
 */
- (BOOL)isBlocking;

Modified src/OFStream.m from [0bbf55df10] to [826cbee6d8].

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
- copy
{
	return [self retain];
}

- (BOOL)isAtEndOfStream
{
	if (_cacheLength > 0)
		return NO;

	return [self lowlevelIsAtEndOfStream];
}

- (size_t)readIntoBuffer: (void*)buffer
		  length: (size_t)length
{
	if (_cacheLength == 0)
		return [self lowlevelReadIntoBuffer: buffer
					     length: length];

	if (length >= _cacheLength) {
		size_t ret = _cacheLength;
		memcpy(buffer, _cache, _cacheLength);

		[self freeMemory: _cache];
		_cache = NULL;
		_cacheLength = 0;

		return ret;
	} else {


		char *tmp = [self allocMemoryWithSize: _cacheLength - length];
		memcpy(tmp, _cache + length, _cacheLength - length);

		memcpy(buffer, _cache, length);

		[self freeMemory: _cache];
		_cache = tmp;
		_cacheLength -= length;

		return length;
	}
}

- (void)readIntoBuffer: (void*)buffer
	   exactLength: (size_t)length







|








|



|
|
|

|
|
|



>
>
|
|

|

|
|
|







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
- copy
{
	return [self retain];
}

- (BOOL)isAtEndOfStream
{
	if (_readBufferLength > 0)
		return NO;

	return [self lowlevelIsAtEndOfStream];
}

- (size_t)readIntoBuffer: (void*)buffer
		  length: (size_t)length
{
	if (_readBufferLength == 0)
		return [self lowlevelReadIntoBuffer: buffer
					     length: length];

	if (length >= _readBufferLength) {
		size_t ret = _readBufferLength;
		memcpy(buffer, _readBuffer, _readBufferLength);

		[self freeMemory: _readBuffer];
		_readBuffer = NULL;
		_readBufferLength = 0;

		return ret;
	} else {
		char *tmp;

		tmp = [self allocMemoryWithSize: _readBufferLength - length];
		memcpy(tmp, _readBuffer + length, _readBufferLength - length);

		memcpy(buffer, _readBuffer, length);

		[self freeMemory: _readBuffer];
		_readBuffer = tmp;
		_readBufferLength -= length;

		return length;
	}
}

- (void)readIntoBuffer: (void*)buffer
	   exactLength: (size_t)length
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669

670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

696
697
698
699
700
701
702

703
704
705
706
707
708
709
710
711
712

	return ret;
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (!_waitingForDelimiter && _cache != NULL) {
		for (i = 0; i < _cacheLength; i++) {
			if OF_UNLIKELY (_cache[i] == '\n' ||
			    _cache[i] == '\0') {
				retLength = i;

				if (i > 0 && _cache[i - 1] == '\r')
					retLength--;

				ret = [OFString stringWithCString: _cache
							 encoding: encoding
							   length: retLength];

				newCache = [self
				    allocMemoryWithSize: _cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, _cache + i + 1,
					    _cacheLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a newline or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_cache == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			retLength = _cacheLength;

			if (retLength > 0 && _cache[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: _cache
						 encoding: encoding
						   length: retLength];

			[self freeMemory: _cache];
			_cache = NULL;
			_cacheLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a newline or \0 */
		for (i = 0; i < bufferLength; i++) {
			if OF_UNLIKELY (buffer[i] == '\n' ||
			    buffer[i] == '\0') {
				retLength = _cacheLength + i;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (_cache != NULL)
					memcpy(retCString, _cache,
					    _cacheLength);
				memcpy(retCString + _cacheLength, buffer, i);


				if (retLength > 0 &&
				    retCString[retLength - 1] == '\r')
					retLength--;

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @catch (id e) {
					/*
					 * Append data to cache to prevent loss
					 * of data due to wrong encoding.
					 */
					_cache = [self
					    resizeMemory: _cache
						    size: _cacheLength +
							  bufferLength];

					if (_cache != NULL)
						memcpy(_cache + _cacheLength,

						    buffer, bufferLength);

					_cacheLength += bufferLength;

					@throw e;
				} @finally {
					[self freeMemory: retCString];
				}

				newCache = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* There was no newline or \0 */
		_cache = [self resizeMemory: _cache

				       size: _cacheLength + bufferLength];

		/*
		 * It's possible that _cacheLength + bufferLength is 0 and thus
		 * _cache was set to NULL by resizeMemory:size:.
		 */
		if (_cache != NULL)

			memcpy(_cache + _cacheLength, buffer, bufferLength);

		_cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}







|


|
|
|
|
|


|


|



|
|
|
|
|

|
|
|













|




|

|


|



|
|
|












|



|
|
|
|
>















|
|

|
|
|


|
|
>


|






|
|
|
|


|
|
|







|
>
|


|
|

|
>
|

|







564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718

	return ret;
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *readBuffer;
	OFString *ret;

	/* Look if there's a line or \0 in our buffer */
	if (!_waitingForDelimiter && _readBuffer != NULL) {
		for (i = 0; i < _readBufferLength; i++) {
			if OF_UNLIKELY (_readBuffer[i] == '\n' ||
			    _readBuffer[i] == '\0') {
				retLength = i;

				if (i > 0 && _readBuffer[i - 1] == '\r')
					retLength--;

				ret = [OFString stringWithCString: _readBuffer
							 encoding: encoding
							   length: retLength];

				readBuffer = [self allocMemoryWithSize:
				    _readBufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, _readBuffer + i + 1,
					    _readBufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a newline or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_readBuffer == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			retLength = _readBufferLength;

			if (retLength > 0 && _readBuffer[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: _readBuffer
						 encoding: encoding
						   length: retLength];

			[self freeMemory: _readBuffer];
			_readBuffer = NULL;
			_readBufferLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a newline or \0 */
		for (i = 0; i < bufferLength; i++) {
			if OF_UNLIKELY (buffer[i] == '\n' ||
			    buffer[i] == '\0') {
				retLength = _readBufferLength + i;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (_readBuffer != NULL)
					memcpy(retCString, _readBuffer,
					    _readBufferLength);
				memcpy(retCString + _readBufferLength,
				    buffer, i);

				if (retLength > 0 &&
				    retCString[retLength - 1] == '\r')
					retLength--;

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @catch (id e) {
					/*
					 * Append data to readBuffer to prevent
					 * loss of data due to wrong encoding.
					 */
					_readBuffer = [self
					    resizeMemory: _readBuffer
						    size: _readBufferLength +
							  bufferLength];

					if (_readBuffer != NULL)
						memcpy(_readBuffer +
						    _readBufferLength,
						    buffer, bufferLength);

					_readBufferLength += bufferLength;

					@throw e;
				} @finally {
					[self freeMemory: retCString];
				}

				readBuffer = [self
				    allocMemoryWithSize: bufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* There was no newline or \0 */
		_readBuffer = [self resizeMemory: _readBuffer
					    size: _readBufferLength +
						  bufferLength];

		/*
		 * It's possible that _readBufferLength + bufferLength is 0 and
		 * thus _readBuffer was set to NULL by resizeMemory:size:.
		 */
		if (_readBuffer != NULL)
			memcpy(_readBuffer + _readBufferLength,
			    buffer, bufferLength);

		_readBufferLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856

857
858
859
860
861

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894

895
896
897
898
899
900
901

902
903
904
905
906
907
908
909
910
911
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
			 encoding: (of_string_encoding_t)encoding
{
	const char *delimiterCString;
	size_t i, j, delimiterLength, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	delimiterCString = [delimiter cStringWithEncoding: encoding];
	delimiterLength = [delimiter cStringLengthWithEncoding: encoding];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	/* Look if there's something in our cache */
	if (!_waitingForDelimiter && _cache != NULL) {
		for (i = 0; i < _cacheLength; i++) {
			if (_cache[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || _cache[i] == '\0') {
				if (_cache[i] == '\0')
					delimiterLength = 1;

				ret = [OFString
				    stringWithCString: _cache
					     encoding: encoding
					      length: i + 1 - delimiterLength];

				newCache = [self allocMemoryWithSize:
				    _cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, _cache + i + 1,
					    _cacheLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a delimiter or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_cache == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: _cache
						 encoding: encoding
						   length: _cacheLength];

			[self freeMemory: _cache];
			_cache = NULL;
			_cacheLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a delimiter or \0 */
		for (i = 0; i < bufferLength; i++) {
			if (buffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 1;

				retLength = _cacheLength + i + 1 -
				    delimiterLength;
				retCString = [self
				    allocMemoryWithSize: retLength];


				if (_cache != NULL && _cacheLength <= retLength)
					memcpy(retCString, _cache,
					    _cacheLength);
				else if (_cache != NULL)
					memcpy(retCString, _cache, retLength);

				if (i >= delimiterLength)
					memcpy(retCString + _cacheLength,
					    buffer, i + 1 - delimiterLength);

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @finally {
					[self freeMemory: retCString];
				}

				newCache = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		_cache = [self resizeMemory: _cache

				       size: _cacheLength + bufferLength];

		/*
		 * It's possible that _cacheLength + bufferLength is 0 and thus
		 * _cache was set to NULL by resizeMemory:size:.
		 */
		if (_cache != NULL)

			memcpy(_cache + _cacheLength, buffer, bufferLength);

		_cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}







|











|
|
|
|


|
|



|



|
|
|
|
|

|
|
|













|




|

|

|
|
|

















|




>
|
|
|
|
|
>

|














|

|
|


|
|
|







|
>
|


|
|

|
>
|

|







773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
			 encoding: (of_string_encoding_t)encoding
{
	const char *delimiterCString;
	size_t i, j, delimiterLength, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *readBuffer;
	OFString *ret;

	delimiterCString = [delimiter cStringWithEncoding: encoding];
	delimiterLength = [delimiter cStringLengthWithEncoding: encoding];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	/* Look if there's something in our buffer */
	if (!_waitingForDelimiter && _readBuffer != NULL) {
		for (i = 0; i < _readBufferLength; i++) {
			if (_readBuffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || _readBuffer[i] == '\0') {
				if (_readBuffer[i] == '\0')
					delimiterLength = 1;

				ret = [OFString
				    stringWithCString: _readBuffer
					     encoding: encoding
					      length: i + 1 - delimiterLength];

				readBuffer = [self allocMemoryWithSize:
				    _readBufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, _readBuffer + i + 1,
					    _readBufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a delimiter or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_readBuffer == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: _readBuffer
						 encoding: encoding
						   length: _readBufferLength];

			[self freeMemory: _readBuffer];
			_readBuffer = NULL;
			_readBufferLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a delimiter or \0 */
		for (i = 0; i < bufferLength; i++) {
			if (buffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 1;

				retLength = _readBufferLength + i + 1 -
				    delimiterLength;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (_readBuffer != NULL &&
				    _readBufferLength <= retLength)
					memcpy(retCString, _readBuffer,
					    _readBufferLength);
				else if (_readBuffer != NULL)
					memcpy(retCString, _readBuffer,
					    retLength);
				if (i >= delimiterLength)
					memcpy(retCString + _readBufferLength,
					    buffer, i + 1 - delimiterLength);

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @finally {
					[self freeMemory: retCString];
				}

				readBuffer = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		_readBuffer = [self resizeMemory: _readBuffer
					    size: _readBufferLength +
						  bufferLength];

		/*
		 * It's possible that _readBufferLength + bufferLength is 0 and
		 * thus _readBuffer was set to NULL by resizeMemory:size:.
		 */
		if (_readBuffer != NULL)
			memcpy(_readBuffer + _readBufferLength,
			    buffer, bufferLength);

		_readBufferLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
	} @finally {
		free(UTF8String);
	}

	return length;
}

- (size_t)pendingBytes
{
	return _cacheLength;
}

- (BOOL)isBlocking
{
	return _blocking;
}








|

|







1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
	} @finally {
		free(UTF8String);
	}

	return length;
}

- (size_t)numberOfBytesInReadBuffer
{
	return _readBufferLength;
}

- (BOOL)isBlocking
{
	return _blocking;
}

Modified src/OFStreamObserver.m from [67edca5fb3] to [6df17e709d].

392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
- (BOOL)OF_processCache
{
	OFStream **objects = [_readStreams objects];
	size_t i, count = [_readStreams count];
	BOOL foundInCache = NO;

	for (i = 0; i < count; i++) {
		if ([objects[i] pendingBytes] > 0 &&
		    ![objects[i] OF_isWaitingForDelimiter]) {
			void *pool = objc_autoreleasePoolPush();

			if ([_delegate respondsToSelector:
			    @selector(streamIsReadyForReading:)])
				[_delegate streamIsReadyForReading: objects[i]];








|







392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
- (BOOL)OF_processCache
{
	OFStream **objects = [_readStreams objects];
	size_t i, count = [_readStreams count];
	BOOL foundInCache = NO;

	for (i = 0; i < count; i++) {
		if ([objects[i] numberOfBytesInReadBuffer] > 0 &&
		    ![objects[i] OF_isWaitingForDelimiter]) {
			void *pool = objc_autoreleasePoolPush();

			if ([_delegate respondsToSelector:
			    @selector(streamIsReadyForReading:)])
				[_delegate streamIsReadyForReading: objects[i]];