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 *_Nullable _readBuffer, *_Nullable _readBufferMemory;
98  char *_Nullable _writeBuffer;
99  size_t _readBufferLength, _writeBufferLength;
100  bool _writeBuffered, _waitingForDelimiter;
101 @protected
102  bool _blocking;
103 }
104 
110 - (bool)isAtEndOfStream;
111 
126 - (size_t)readIntoBuffer: (void *)buffer
127  length: (size_t)length;
128 
144  - (void)readIntoBuffer: (void *)buffer
145  exactLength: (size_t)length;
146 
147 #ifdef OF_HAVE_SOCKETS
148 
175 - (void)asyncReadIntoBuffer: (void *)buffer
176  length: (size_t)length
177  target: (id)target
178  selector: (SEL)selector;
179 
205 - (void)asyncReadIntoBuffer: (void *)buffer
206  exactLength: (size_t)length
207  target: (id)target
208  selector: (SEL)selector;
209 
210 # ifdef OF_HAVE_BLOCKS
211 
234 - (void)asyncReadIntoBuffer: (void *)buffer
235  length: (size_t)length
236  block: (of_stream_async_read_block_t)block;
237 
259  - (void)asyncReadIntoBuffer: (void *)buffer
260  exactLength: (size_t)length
261  block: (of_stream_async_read_block_t)block;
262 # endif
263 #endif
264 
273 - (uint8_t)readInt8;
274 
283 - (uint16_t)readBigEndianInt16;
284 
293 - (uint32_t)readBigEndianInt32;
294 
303 - (uint64_t)readBigEndianInt64;
304 
313 - (float)readBigEndianFloat;
314 
323 - (double)readBigEndianDouble;
324 
337 - (size_t)readBigEndianInt16sIntoBuffer: (uint16_t *)buffer
338  count: (size_t)count;
339 
352 - (size_t)readBigEndianInt32sIntoBuffer: (uint32_t *)buffer
353  count: (size_t)count;
354 
367 - (size_t)readBigEndianInt64sIntoBuffer: (uint64_t *)buffer
368  count: (size_t)count;
369 
382 - (size_t)readBigEndianFloatsIntoBuffer: (float *)buffer
383  count: (size_t)count;
384 
397 - (size_t)readBigEndianDoublesIntoBuffer: (double *)buffer
398  count: (size_t)count;
399 
408 - (uint16_t)readLittleEndianInt16;
409 
418 - (uint32_t)readLittleEndianInt32;
419 
428 - (uint64_t)readLittleEndianInt64;
429 
438 - (float)readLittleEndianFloat;
439 
448 - (double)readLittleEndianDouble;
449 
462 - (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t *)buffer
463  count: (size_t)count;
464 
477 - (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t *)buffer
478  count: (size_t)count;
479 
492 - (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t *)buffer
493  count: (size_t)count;
494 
507 - (size_t)readLittleEndianFloatsIntoBuffer: (float *)buffer
508  count: (size_t)count;
509 
522 - (size_t)readLittleEndianDoublesIntoBuffer: (double *)buffer
523  count: (size_t)count;
524 
535 - (OFData *)readDataWithCount: (size_t)count;
536 
548 - (OFData *)readDataWithItemSize: (size_t)itemSize
549  count: (size_t)count;
550 
558 
573 - (OFString *)readStringWithLength: (size_t)length;
574 
590 - (OFString *)readStringWithLength: (size_t)length
591  encoding: (of_string_encoding_t)encoding;
592 
599 - (nullable OFString *)readLine;
600 
609 - (nullable OFString *)readLineWithEncoding: (of_string_encoding_t)encoding;
610 
611 #ifdef OF_HAVE_SOCKETS
612 
628 - (void)asyncReadLineWithTarget: (id)target
629  selector: (SEL)selector;
630 
648 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
649  target: (id)target
650  selector: (SEL)selector;
651 
652 # ifdef OF_HAVE_BLOCKS
653 
666 - (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block;
667 
682 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
683  block: (of_stream_async_read_line_block_t)block;
684 # endif
685 #endif
686 
694 - (nullable OFString *)tryReadLine;
695 
705 - (nullable OFString *)tryReadLineWithEncoding: (of_string_encoding_t)encoding;
706 
715 - (nullable OFString *)readTillDelimiter: (OFString *)delimiter;
716 
726 - (nullable OFString *)readTillDelimiter: (OFString *)delimiter
727  encoding: (of_string_encoding_t)encoding;
728 
738 - (nullable OFString *)tryReadTillDelimiter: (OFString *)delimiter;
739 
750 - (nullable OFString *)tryReadTillDelimiter: (OFString *)delimiter
751  encoding: (of_string_encoding_t)encoding;
752 
758 - (bool)isWriteBuffered;
759 
765 - (void)setWriteBuffered: (bool)enable;
766 
770 - (void)flushWriteBuffer;
771 
778 - (void)writeBuffer: (const void *)buffer
779  length: (size_t)length;
780 
786 - (void)writeInt8: (uint8_t)int8;
787 
793 - (void)writeBigEndianInt16: (uint16_t)int16;
794 
800 - (void)writeBigEndianInt32: (uint32_t)int32;
801 
807 - (void)writeBigEndianInt64: (uint64_t)int64;
808 
814 - (void)writeBigEndianFloat: (float)float_;
815 
821 - (void)writeBigEndianDouble: (double)double_;
822 
832 - (size_t)writeBigEndianInt16s: (const uint16_t *)buffer
833  count: (size_t)count;
834 
844 - (size_t)writeBigEndianInt32s: (const uint32_t *)buffer
845  count: (size_t)count;
846 
856 - (size_t)writeBigEndianInt64s: (const uint64_t *)buffer
857  count: (size_t)count;
858 
868 - (size_t)writeBigEndianFloats: (const float *)buffer
869  count: (size_t)count;
870 
880 - (size_t)writeBigEndianDoubles: (const double *)buffer
881  count: (size_t)count;
882 
888 - (void)writeLittleEndianInt16: (uint16_t)int16;
889 
895 - (void)writeLittleEndianInt32: (uint32_t)int32;
896 
902 - (void)writeLittleEndianInt64: (uint64_t)int64;
903 
909 - (void)writeLittleEndianFloat: (float)float_;
910 
916 - (void)writeLittleEndianDouble: (double)double_;
917 
927 - (size_t)writeLittleEndianInt16s: (const uint16_t *)buffer
928  count: (size_t)count;
929 
939 - (size_t)writeLittleEndianInt32s: (const uint32_t *)buffer
940  count: (size_t)count;
941 
951 - (size_t)writeLittleEndianInt64s: (const uint64_t *)buffer
952  count: (size_t)count;
953 
963 - (size_t)writeLittleEndianFloats: (const float *)buffer
964  count: (size_t)count;
965 
975 - (size_t)writeLittleEndianDoubles: (const double *)buffer
976  count: (size_t)count;
977 
984 - (size_t)writeData: (OFData *)data;
985 
992 - (size_t)writeString: (OFString *)string;
993 
1002 - (size_t)writeString: (OFString *)string
1003  encoding: (of_string_encoding_t)encoding;
1004 
1011 - (size_t)writeLine: (OFString *)string;
1012 
1021 - (size_t)writeLine: (OFString *)string
1022  encoding: (of_string_encoding_t)encoding;
1023 
1034 - (size_t)writeFormat: (OFConstantString *)format, ...;
1035 
1047 - (size_t)writeFormat: (OFConstantString *)format
1048  arguments: (va_list)arguments;
1049 
1055 - (bool)hasDataInReadBuffer;
1056 
1062 - (bool)isBlocking;
1063 
1072 - (void)setBlocking: (bool)enable;
1073 
1080 
1087 
1088 #ifdef OF_HAVE_SOCKETS
1089 
1092 - (void)cancelAsyncRequests;
1093 #endif
1094 
1116 - (void)unreadFromBuffer: (const void *)buffer
1117  length: (size_t)length;
1118 
1124 - (void)close;
1125 
1138 - (size_t)lowlevelReadIntoBuffer: (void *)buffer
1139  length: (size_t)length;
1140 
1152 - (void)lowlevelWriteBuffer: (const void *)buffer
1153  length: (size_t)length;
1154 
1165 - (bool)lowlevelIsAtEndOfStream;
1166 @end
1167 
1168 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:914
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