1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket;
20
21 import java.io.IOException;
22 import java.util.Map;
23
24 public class FragmentExtension extends AbstractExtension
25 {
26 private int _maxLength=-1;
27 private int _minFragments=1;
28
29 public FragmentExtension()
30 {
31 super("fragment");
32 }
33
34 @Override
35 public boolean init(Map<String, String> parameters)
36 {
37 if(super.init(parameters))
38 {
39 _maxLength=getInitParameter("maxLength",_maxLength);
40 _minFragments=getInitParameter("minFragments",_minFragments);
41 return true;
42 }
43 return false;
44 }
45
46 @Override
47 public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException
48 {
49 if (getConnection().isControl(opcode))
50 {
51 super.addFrame(flags,opcode,content,offset,length);
52 return;
53 }
54
55 int fragments=1;
56
57 while (_maxLength>0 && length>_maxLength)
58 {
59 fragments++;
60 super.addFrame((byte)(flags&~getConnection().finMask()),opcode,content,offset,_maxLength);
61 length-=_maxLength;
62 offset+=_maxLength;
63 opcode=getConnection().continuationOpcode();
64 }
65
66 while (fragments<_minFragments)
67 {
68 int frag=length/2;
69 fragments++;
70 super.addFrame((byte)(flags&0x7),opcode,content,offset,frag);
71 length-=frag;
72 offset+=frag;
73 opcode=getConnection().continuationOpcode();
74 }
75
76 super.addFrame((byte)(flags|getConnection().finMask()),opcode,content,offset,length);
77 }
78
79
80 }