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