ObjFW
OFStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #ifndef __STDC_LIMIT_MACROS
18 # define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 # define __STDC_CONSTANT_MACROS
22 #endif
23 
24 #include <stdarg.h>
25 
26 #import "OFObject.h"
27 #import "OFString.h"
28 #ifdef OF_HAVE_SOCKETS
29 # import "OFKernelEventObserver.h"
30 #endif
31 
32 OF_ASSUME_NONNULL_BEGIN
33 
36 @class OFStream;
37 @class OFData;
38 @class OFException;
39 
40 #if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_BLOCKS)
41 
51 typedef bool (^of_stream_async_read_block_t)(OFStream *stream, void *buffer,
52  size_t length, OFException *_Nullable exception);
53 
65  OFString *_Nullable line, OFException *_Nullable exception);
66 #endif
67 
88 @interface OFStream: OFObject <
89 #ifdef OF_HAVE_SOCKETS
91 #endif
92  OFCopying>
93 {
94 #if !defined(OF_SEEKABLE_STREAM_M) && !defined(OF_TCP_SOCKET_M)
95 @private
96 #endif
97  char *_readBuffer, *_readBufferMemory, *_writeBuffer;
98  size_t _readBufferLength, _writeBufferLength;
99  bool _writeBuffered, _waitingForDelimiter;
100 @protected
101  bool _blocking;
102 }
103 
109 - (bool)isAtEndOfStream;
110 
125 - (size_t)readIntoBuffer: (void *)buffer
126  length: (size_t)length;
127 
143  - (void)readIntoBuffer: (void *)buffer
144  exactLength: (size_t)length;
145 
146 #ifdef OF_HAVE_SOCKETS
147 
174 - (void)asyncReadIntoBuffer: (void *)buffer
175  length: (size_t)length
176  target: (id)target
177  selector: (SEL)selector;
178 
204 - (void)asyncReadIntoBuffer: (void *)buffer
205  exactLength: (size_t)length
206  target: (id)target
207  selector: (SEL)selector;
208 
209 # ifdef OF_HAVE_BLOCKS
210 
233 - (void)asyncReadIntoBuffer: (void *)buffer
234  length: (size_t)length
235  block: (of_stream_async_read_block_t)block;
236 
258  - (void)asyncReadIntoBuffer: (void *)buffer
259  exactLength: (size_t)length
260  block: (of_stream_async_read_block_t)block;
261 # endif
262 #endif
263 
272 - (uint8_t)readInt8;
273 
282 - (uint16_t)readBigEndianInt16;
283 
292 - (uint32_t)readBigEndianInt32;
293 
302 - (uint64_t)readBigEndianInt64;
303 
312 - (float)readBigEndianFloat;
313 
322 - (double)readBigEndianDouble;
323 
336 - (size_t)readBigEndianInt16sIntoBuffer: (uint16_t *)buffer
337  count: (size_t)count;
338 
351 - (size_t)readBigEndianInt32sIntoBuffer: (uint32_t *)buffer
352  count: (size_t)count;
353 
366 - (size_t)readBigEndianInt64sIntoBuffer: (uint64_t *)buffer
367  count: (size_t)count;
368 
381 - (size_t)readBigEndianFloatsIntoBuffer: (float *)buffer
382  count: (size_t)count;
383 
396 - (size_t)readBigEndianDoublesIntoBuffer: (double *)buffer
397  count: (size_t)count;
398 
407 - (uint16_t)readLittleEndianInt16;
408 
417 - (uint32_t)readLittleEndianInt32;
418 
427 - (uint64_t)readLittleEndianInt64;
428 
437 - (float)readLittleEndianFloat;
438 
447 - (double)readLittleEndianDouble;
448 
461 - (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t *)buffer
462  count: (size_t)count;
463 
476 - (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t *)buffer
477  count: (size_t)count;
478 
491 - (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t *)buffer
492  count: (size_t)count;
493 
506 - (size_t)readLittleEndianFloatsIntoBuffer: (float *)buffer
507  count: (size_t)count;
508 
521 - (size_t)readLittleEndianDoublesIntoBuffer: (double *)buffer
522  count: (size_t)count;
523 
534 - (OFData *)readDataWithCount: (size_t)count;
535 
547 - (OFData *)readDataWithItemSize: (size_t)itemSize
548  count: (size_t)count;
549 
557 
572 - (OFString *)readStringWithLength: (size_t)length;
573 
589 - (OFString *)readStringWithLength: (size_t)length
590  encoding: (of_string_encoding_t)encoding;
591 
598 - (nullable OFString *)readLine;
599 
608 - (nullable OFString *)readLineWithEncoding: (of_string_encoding_t)encoding;
609 
610 #ifdef OF_HAVE_SOCKETS
611 
627 - (void)asyncReadLineWithTarget: (id)target
628  selector: (SEL)selector;
629 
647 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
648  target: (id)target
649  selector: (SEL)selector;
650 
651 # ifdef OF_HAVE_BLOCKS
652 
665 - (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block;
666 
681 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
682  block: (of_stream_async_read_line_block_t)block;
683 # endif
684 #endif
685 
693 - (nullable OFString *)tryReadLine;
694 
704 - (nullable OFString *)tryReadLineWithEncoding: (of_string_encoding_t)encoding;
705 
714 - (nullable OFString *)readTillDelimiter: (OFString *)delimiter;
715 
725 - (nullable OFString *)readTillDelimiter: (OFString *)delimiter
726  encoding: (of_string_encoding_t)encoding;
727 
737 - (nullable OFString *)tryReadTillDelimiter: (OFString *)delimiter;
738 
749 - (nullable OFString *)tryReadTillDelimiter: (OFString *)delimiter
750  encoding: (of_string_encoding_t)encoding;
751 
757 - (bool)isWriteBuffered;
758 
764 - (void)setWriteBuffered: (bool)enable;
765 
769 - (void)flushWriteBuffer;
770 
777 - (void)writeBuffer: (const void *)buffer
778  length: (size_t)length;
779 
785 - (void)writeInt8: (uint8_t)int8;
786 
792 - (void)writeBigEndianInt16: (uint16_t)int16;
793 
799 - (void)writeBigEndianInt32: (uint32_t)int32;
800 
806 - (void)writeBigEndianInt64: (uint64_t)int64;
807 
813 - (void)writeBigEndianFloat: (float)float_;
814 
820 - (void)writeBigEndianDouble: (double)double_;
821 
831 - (size_t)writeBigEndianInt16s: (const uint16_t *)buffer
832  count: (size_t)count;
833 
843 - (size_t)writeBigEndianInt32s: (const uint32_t *)buffer
844  count: (size_t)count;
845 
855 - (size_t)writeBigEndianInt64s: (const uint64_t *)buffer
856  count: (size_t)count;
857 
867 - (size_t)writeBigEndianFloats: (const float *)buffer
868  count: (size_t)count;
869 
879 - (size_t)writeBigEndianDoubles: (const double *)buffer
880  count: (size_t)count;
881 
887 - (void)writeLittleEndianInt16: (uint16_t)int16;
888 
894 - (void)writeLittleEndianInt32: (uint32_t)int32;
895 
901 - (void)writeLittleEndianInt64: (uint64_t)int64;
902 
908 - (void)writeLittleEndianFloat: (float)float_;
909 
915 - (void)writeLittleEndianDouble: (double)double_;
916 
926 - (size_t)writeLittleEndianInt16s: (const uint16_t *)buffer
927  count: (size_t)count;
928 
938 - (size_t)writeLittleEndianInt32s: (const uint32_t *)buffer
939  count: (size_t)count;
940 
950 - (size_t)writeLittleEndianInt64s: (const uint64_t *)buffer
951  count: (size_t)count;
952 
962 - (size_t)writeLittleEndianFloats: (const float *)buffer
963  count: (size_t)count;
964 
974 - (size_t)writeLittleEndianDoubles: (const double *)buffer
975  count: (size_t)count;
976 
983 - (size_t)writeData: (OFData *)data;
984 
991 - (size_t)writeString: (OFString *)string;
992 
1001 - (size_t)writeString: (OFString *)string
1002  encoding: (of_string_encoding_t)encoding;
1003 
1010 - (size_t)writeLine: (OFString *)string;
1011 
1020 - (size_t)writeLine: (OFString *)string
1021  encoding: (of_string_encoding_t)encoding;
1022 
1033 - (size_t)writeFormat: (OFConstantString *)format, ...;
1034 
1046 - (size_t)writeFormat: (OFConstantString *)format
1047  arguments: (va_list)arguments;
1048 
1054 - (bool)hasDataInReadBuffer;
1055 
1061 - (bool)isBlocking;
1062 
1071 - (void)setBlocking: (bool)enable;
1072 
1079 
1086 
1087 #ifdef OF_HAVE_SOCKETS
1088 
1091 - (void)cancelAsyncRequests;
1092 #endif
1093 
1115 - (void)unreadFromBuffer: (const void *)buffer
1116  length: (size_t)length;
1117 
1123 - (void)close;
1124 
1137 - (size_t)lowlevelReadIntoBuffer: (void *)buffer
1138  length: (size_t)length;
1139 
1151 - (void)lowlevelWriteBuffer: (const void *)buffer
1152  length: (size_t)length;
1153 
1164 - (bool)lowlevelIsAtEndOfStream;
1165 @end
1166 
1167 OF_ASSUME_NONNULL_END
int fileDescriptorForWriting()
Returns the file descriptor for the write end of the stream.
Definition: OFStream.m:1622
uint32_t readBigEndianInt32()
Reads a uint32_t from the stream which is encoded in big endian.
Definition: OFStream.m:258
void close()
Closes the stream.
Definition: OFStream.m:1651
bool(^ of_stream_async_read_block_t)(OFStream *stream, void *buffer, size_t length, OFException *_Nullable exception)
A block which is called when data was read from the stream.
Definition: OFStream.h:51
A class for handling strings.
Definition: OFString.h:114
uint64_t readBigEndianInt64()
Reads a uint64_t from the stream which is encoded in big endian.
Definition: OFStream.m:268
nullable OFString * readLine()
Reads until a newline, \0 or end of stream occurs.
Definition: OFStream.m:796
uint8_t readInt8()
Reads a uint8_t from the stream.
Definition: OFStream.m:238
uint16_t readBigEndianInt16()
Reads a uint16_t from the stream which is encoded in big endian.
Definition: OFStream.m:248
double readBigEndianDouble()
Reads a double from the stream which is encoded in big endian.
Definition: OFStream.m:288
The base class for all exceptions in ObjFW.
Definition: OFException.h:143
bool isWriteBuffered()
Returns a boolean whether writes are buffered.
Definition: OFStream.m:1018
uint32_t readLittleEndianInt32()
Reads a uint32_t from the stream which is encoded in little endian.
Definition: OFStream.m:413
A protocol for the creation of copies.
Definition: OFObject.h:912
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:37
double readLittleEndianDouble()
Reads a double from the stream which is encoded in little endian.
Definition: OFStream.m:443
float readBigEndianFloat()
Reads a float from the stream which is encoded in big endian.
Definition: OFStream.m:278
float readLittleEndianFloat()
Reads a float from the stream which is encoded in little endian.
Definition: OFStream.m:433
uint64_t readLittleEndianInt64()
Reads a uint64_t from the stream which is encoded in little endian.
Definition: OFStream.m:423
void flushWriteBuffer()
Writes everything in the write buffer to the stream.
Definition: OFStream.m:1028
uint16_t readLittleEndianInt16()
Reads a uint16_t from the stream which is encoded in little endian.
Definition: OFStream.m:403
of_string_encoding_t
The encoding of a string.
Definition: OFString.h:53
bool hasDataInReadBuffer()
Returns whether data is present in the internal read buffer.
Definition: OFStream.m:1542
bool lowlevelIsAtEndOfStream()
Returns whether the lowlevel is at the end of the stream.
Definition: OFStream.m:88
A class for storing arbitrary data in an array.
Definition: OFData.h:34
OFData * readDataUntilEndOfStream()
Returns OFData with all the remaining data of the stream.
Definition: OFStream.m:593
int fileDescriptorForReading()
Returns the file descriptor for the read end of the stream.
Definition: OFStream.m:1617
void cancelAsyncRequests()
Cancels all pending asynchronous requests on the stream.
Definition: OFStream.m:1628
This protocol is implemented by classes which can be observed for readiness for writing by OFKernelEv...
bool isBlocking()
Returns whether the stream is in blocking mode.
Definition: OFStream.m:1547
A base class for different types of streams.
Definition: OFStream.h:88
This protocol is implemented by classes which can be observed for readiness for reading by OFKernelEv...
nullable OFString * tryReadLine()
Tries to read a line from the stream (see readLine) and returns nil if no complete line has been rece...
Definition: OFStream.m:848
bool(^ of_stream_async_read_line_block_t)(OFStream *stream, OFString *_Nullable line, OFException *_Nullable exception)
A block which is called when a line was read from the stream.
Definition: OFStream.h:64
bool isAtEndOfStream()
Returns a boolean whether the end of the stream has been reached.
Definition: OFStream.m:110