-
Notifications
You must be signed in to change notification settings - Fork 21
/
MYBuffer.h
61 lines (42 loc) · 2.16 KB
/
MYBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// MYBuffer.h
// MYUtilities
//
// Created by Jens Alfke on 4/5/15.
// Copyright (c) 2015 Jens Alfke. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MYData.h"
@protocol MYWriter <NSObject>
- (BOOL) writeData: (NSData*)data;
- (BOOL) writeSlice: (MYSlice)slice;
/** Adds the contents of the stream. The stream must already be open. This may not read from the
stream right way; instead the writer may keep a reference to the stream and only read from
it on demand to satisfy its own read requests. For this reason you shouldn't read from or close
the stream after this call! The MYWriter instance will close the stream when it's been entirely
read, or when the writer itself is dealloced. */
- (BOOL) writeContentsOfStream: (NSInputStream*)inputStream;
@end
@protocol MYReader <NSObject>
- (ssize_t) readBytes: (void*)buffer maxLength: (size_t)maxLength;
/** If possible, returns a slice (pointer+length) pointing to the reader data read from the
buffer. This memory is only valid until the next call to the buffer; do NOT free or modify it.
You may get back fewer bytes than you asked for; that doesn't mean that the buffer is at EOF.
This may well fail (if reading from a stream) in which case the slice points to NULL. In that
case you should fall back to the regular -readBytes:maxLength: call. */
- (MYSlice) readSliceOfMaxLength: (size_t)maxLength;
@property (readonly) BOOL hasBytesAvailable;
@property (readonly) BOOL atEnd;
@end
/** A growable data buffer that can be written/appended to, and read from.
A stream can be added to a buffer; this effectively adds its entire contents, but they'll be
read on demand instead of being copied into memory all at once. */
@interface MYBuffer : NSObject <MYReader, MYWriter>
- (instancetype) initWithData: (NSData*)data;
@property (readonly) NSUInteger minLength;
@property (readonly) NSUInteger maxLength;
/** Returns the entire (remaining) contents of the buffer as a single NSData.
This doesn't consume any bytes, it just reorganizes the buffer's contents if needed. */
- (NSData*) flattened;
//- (ssize_t) unReadBytes: (void*)buffer maxLength: (size_t)maxLength;
@end