Skip to content

Commit e1d453d

Browse files
Create Compactor.txt
1 parent 3a205bf commit e1d453d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Compactor.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Compactor
2+
{
3+
MemoryStream prev;
4+
MemoryStream stream = new MemoryStream();
5+
public Action<MemoryStream> OnOut;
6+
int remaining = 64000;
7+
public void Push(MemoryStream curr)
8+
{
9+
if (curr.Position >= 64000)
10+
{
11+
Flush();
12+
prev = curr;
13+
Flush();
14+
}
15+
else if(prev == null)
16+
{
17+
prev = curr;
18+
}
19+
else if(curr.Position + prev.Position < remaining)
20+
{
21+
stream.Write(prev.GetBuffer(),0,(int)prev.Position);
22+
remaining -= (int)prev.Position;
23+
prev = curr;
24+
}
25+
else
26+
{
27+
Flush();
28+
prev = curr;
29+
Flush();
30+
}
31+
}
32+
33+
public void Flush()
34+
{
35+
if (stream.Position >0)
36+
{
37+
OnOut(stream);
38+
stream.Position = 0;
39+
remaining = 64000;
40+
}
41+
if(prev != null)
42+
{
43+
OnOut(prev);
44+
prev = null;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)