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