1 module hunt.http.codec.http.decode.HeaderBlockFragments;
2 
3 import hunt.io.ByteBuffer;
4 
5 import hunt.http.codec.http.frame.PriorityFrame;
6 import hunt.io.BufferUtils;
7 
8 class HeaderBlockFragments {
9 	private PriorityFrame priorityFrame;
10 	private bool endStream;
11 	private int streamId;
12 	private ByteBuffer storage;
13 
14 	void storeFragment(ByteBuffer fragment, int length, bool last) {
15 		if (storage is null) {
16 			int space = last ? length : length * 2;
17 			storage = BufferUtils.allocate(space);
18 		}
19 
20 		// Grow the storage if necessary.
21 		if (storage.remaining() < length) {
22 			int space = last ? length : length * 2;
23 			int capacity = storage.position() + space;
24 			ByteBuffer newStorage = BufferUtils.allocate(capacity);
25 			storage.flip();
26 			newStorage.put(storage);
27 			storage = newStorage;
28 		}
29 
30 		// Copy the fragment into the storage.
31 		int limit = fragment.limit();
32 		fragment.limit(fragment.position() + length);
33 		storage.put(fragment);
34 		fragment.limit(limit);
35 	}
36 
37 	PriorityFrame getPriorityFrame() {
38 		return priorityFrame;
39 	}
40 
41 	void setPriorityFrame(PriorityFrame priorityFrame) {
42 		this.priorityFrame = priorityFrame;
43 	}
44 
45 	bool isEndStream() {
46 		return endStream;
47 	}
48 
49 	void setEndStream(bool endStream) {
50 		this.endStream = endStream;
51 	}
52 
53 	ByteBuffer complete() {
54 		ByteBuffer result = storage;
55 		storage = null;
56 		result.flip();
57 		return result;
58 	}
59 
60 	int getStreamId() {
61 		return streamId;
62 	}
63 
64 	void setStreamId(int streamId) {
65 		this.streamId = streamId;
66 	}
67 }